| 1 | # ----------------------------------------------------------------------------- | ||
| 2 | |||
| 3 | =encoding utf8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Quiq::PhotoStorage - Foto-Speicher | ||
| 8 | |||
| 9 | =head1 BASE CLASS | ||
| 10 | |||
| 11 | L<Quiq::Hash> | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Ein Objekt der Klasse repräsentiert einen Speicher für Fotos. Der Speicher | ||
| 16 | besitzt folgende Eigenschaften: | ||
| 17 | |||
| 18 | =over 2 | ||
| 19 | |||
| 20 | =item * | ||
| 21 | |||
| 22 | Der Name der Foto-Datei bleibt als Bestandteil erhalten | ||
| 23 | (nach Anwendung von: s/[^-_a-zA-Z0-9]/_/g) | ||
| 24 | |||
| 25 | =item * | ||
| 26 | |||
| 27 | Jedes Foto erhält eine fortlaufende, eindeutige Zahl als Präfix | ||
| 28 | |||
| 29 | =item * | ||
| 30 | |||
| 31 | Es wird der SHA1-Hash der Datei gebildet und gespeichert | ||
| 32 | |||
| 33 | =item * | ||
| 34 | |||
| 35 | Jede Datei wird nur einmal gespeichert, d.h. Dubletten werden | ||
| 36 | zurückgewiesen | ||
| 37 | |||
| 38 | =item * | ||
| 39 | |||
| 40 | Andere Bildformate als JPEG werden nach JPEG konvertiert | ||
| 41 | |||
| 42 | =back | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | # ----------------------------------------------------------------------------- | ||
| 47 | |||
| 48 | package Quiq::PhotoStorage; | ||
| 49 | 1 | ▶ | use base qw/Quiq::Hash/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 50 | |||
| 51 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 52 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 53 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 54 | |||
| 55 | our $VERSION = '1.240'; | ||
| 56 | |||
| 57 | 1 | ▶ | use Quiq::Path; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 58 | 1 | ▶ | use Quiq::LockedCounter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 59 | 0 | ▶ | use Quiq::Hash::Db; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 60 | use Quiq::Shell; | ||
| 61 | |||
| 62 | # ----------------------------------------------------------------------------- | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | =head2 Klassenmethoden | ||
| 67 | |||
| 68 | =head3 new() - Konstruktor | ||
| 69 | |||
| 70 | =head4 Synopsis | ||
| 71 | |||
| 72 | $pst = $class->new($dir); | ||
| 73 | |||
| 74 | =head4 Description | ||
| 75 | |||
| 76 | Öffne den Fotospeicher in Verzeichnis $dir. | ||
| 77 | |||
| 78 | Das Verzeichnis hat den Aufbau: | ||
| 79 | |||
| 80 | $dir/pic/<NNNNNNN>-<NAME>.jpg # Verzeichnis mit den Bildern | ||
| 81 | cnt.txt # Stand der Nummer NNNNNNN | ||
| 82 | sha1.hash # die SHA1-Hashes der Bilddateien | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | # ----------------------------------------------------------------------------- | ||
| 87 | |||
| 88 | sub new { | ||
| 89 | my ($class,$dir) = @_; | ||
| 90 | |||
| 91 | my $p = Quiq::Path->new; | ||
| 92 | |||
| 93 | if (!$p->exists($dir)) { | ||
| 94 | $class->throw( | ||
| 95 | 'PHOTOSTORAGE-00099: Directory does not exist', | ||
| 96 | Dir => $dir, | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | |||
| 100 | $p->mkdir("$dir/dup"); | ||
| 101 | $p->mkdir("$dir/pic"); | ||
| 102 | my $cnt = Quiq::LockedCounter->new("$dir/cnt.txt"); | ||
| 103 | my $h = Quiq::Hash::Db->new("$dir/sha1.hash",'rw'); | ||
| 104 | |||
| 105 | return $class->SUPER::new( | ||
| 106 | dir => $dir, | ||
| 107 | cnt => $cnt, | ||
| 108 | h => $h, | ||
| 109 | ); | ||
| 110 | } | ||
| 111 | |||
| 112 | # ----------------------------------------------------------------------------- | ||
| 113 | |||
| 114 | =head2 Objektmethoden | ||
| 115 | |||
| 116 | =head3 add() - Füge Fotodatei zum Speicher hinzu | ||
| 117 | |||
| 118 | =head4 Synopsis | ||
| 119 | |||
| 120 | $path = $pst->add($file,@opt); | ||
| 121 | |||
| 122 | =head4 Arguments | ||
| 123 | |||
| 124 | =over 4 | ||
| 125 | |||
| 126 | =item $file | ||
| 127 | |||
| 128 | Pfad der Fotodatei. | ||
| 129 | |||
| 130 | =back | ||
| 131 | |||
| 132 | =head4 Options | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item -removeName = $bool (Default: 0) | ||
| 137 | |||
| 138 | Entferne den urprünglichen Dateinamen. Wenn nicht gesetzt, wird der | ||
| 139 | ursprüngliche Dateiname (in Kleinschreibung und ohne "Sonderzeichen") | ||
| 140 | mit "-" getrennt an den Zähler angefügt. | ||
| 141 | |||
| 142 | =back | ||
| 143 | |||
| 144 | =head4 Returns | ||
| 145 | |||
| 146 | (String) Pfad der Datei im Speicher. | ||
| 147 | |||
| 148 | =head4 Description | ||
| 149 | |||
| 150 | Füge Fotodatei $file zum Speicher hinzu und liefere den Pfad der Datei | ||
| 151 | im Speicher zurück. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | # ----------------------------------------------------------------------------- | ||
| 156 | |||
| 157 | sub add { | ||
| 158 | my $self = shift; | ||
| 159 | # @_: $file,@opt | ||
| 160 | |||
| 161 | my ($dir,$cnt,$h) = $self->get(qw/dir cnt h/); | ||
| 162 | |||
| 163 | # Optionen und Argumente | ||
| 164 | |||
| 165 | my $removeName = 0; | ||
| 166 | |||
| 167 | my $argA = $self->parameters(1,1,\@_, | ||
| 168 | -removeName => \$removeName, | ||
| 169 | ); | ||
| 170 | my $file = Quiq::Path->expandTilde(shift @$argA); | ||
| 171 | |||
| 172 | my $p = Quiq::Path->new; | ||
| 173 | my $sha1 = $p->sha1($file); | ||
| 174 | |||
| 175 | my $doublet = $h->get($sha1)? 1: 0; | ||
| 176 | |||
| 177 | my $basename = $p->basename($file); | ||
| 178 | my $ext = lc $p->extension($file); | ||
| 179 | if ($ext eq 'jpg' || $ext eq 'jpeg') { | ||
| 180 | # Bild ist JPEG | ||
| 181 | } | ||
| 182 | elsif ($ext eq 'png' || $ext eq 'webp' || $ext eq 'gif') { | ||
| 183 | # Bilddatei nach JPEG wandeln | ||
| 184 | |||
| 185 | my $sh = Quiq::Shell->new; | ||
| 186 | $sh->exec(qq/convert "$file" "$file.jpg"/); | ||
| 187 | $p->delete($file); | ||
| 188 | $file = "$file.jpg"; | ||
| 189 | } | ||
| 190 | else { | ||
| 191 | $self->throw( | ||
| 192 | 'PHOTOSTORAGE-00099: Unknown file format', | ||
| 193 | File => $file, | ||
| 194 | ); | ||
| 195 | } | ||
| 196 | |||
| 197 | my $n = $cnt->increment->count; | ||
| 198 | my $destFile; | ||
| 199 | if ($removeName) { | ||
| 200 | $destFile = sprintf '%s/%s/%07d.jpg',$dir,$doublet? 'dup': 'pic',$n; | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | $basename =~ s/[^-_a-zA-Z0-9]/_/g; | ||
| 204 | $destFile = sprintf '%s/%s/%07d-%s.jpg',$dir,$doublet? 'dup': 'pic', | ||
| 205 | $n,$basename; | ||
| 206 | } | ||
| 207 | $p->duplicate('move',$file,$destFile); | ||
| 208 | say $destFile; | ||
| 209 | $h->{$sha1}++; | ||
| 210 | |||
| 211 | return $destFile; | ||
| 212 | } | ||
| 213 | |||
| 214 | # ----------------------------------------------------------------------------- | ||
| 215 | |||
| 216 | =head3 addAllByTime() - Füge Fotodateien zum Fotospeicher hinzu | ||
| 217 | |||
| 218 | =head4 Synopsis | ||
| 219 | |||
| 220 | @paths = $pst->addAllByTime(@files,@opt); | ||
| 221 | |||
| 222 | =head4 Arguments | ||
| 223 | |||
| 224 | =over 4 | ||
| 225 | |||
| 226 | =item $file | ||
| 227 | |||
| 228 | Pfad der Fotodatei. | ||
| 229 | |||
| 230 | =back | ||
| 231 | |||
| 232 | =head4 Options | ||
| 233 | |||
| 234 | =over 4 | ||
| 235 | |||
| 236 | =item -removeName = $bool (Default: 0) | ||
| 237 | |||
| 238 | Entferne den urprünglichen Dateinamen. Wenn nicht gesetzt, wird der | ||
| 239 | ursprüngliche Dateiname (in Kleinschreibung und ohne "Sonderzeichen") | ||
| 240 | mit "-" getrennt an den Zähler angefügt. | ||
| 241 | |||
| 242 | =back | ||
| 243 | |||
| 244 | =head4 Returns | ||
| 245 | |||
| 246 | (Array of Strings) Liste der Pfade der Dateien im Speicher. | ||
| 247 | |||
| 248 | =head4 Description | ||
| 249 | |||
| 250 | Füge die Fotodateien @files in der Reihenfolge ihrer mtime | ||
| 251 | zum Speicher hinzu und liefere die Pfade der Dateien | ||
| 252 | im Speicher zurück. | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | # ----------------------------------------------------------------------------- | ||
| 257 | |||
| 258 | sub addAllByTime { | ||
| 259 | my $self = shift; | ||
| 260 | # @_: @files,@opt | ||
| 261 | |||
| 262 | my $p = Quiq::Path->new; | ||
| 263 | |||
| 264 | # Optionen und Argumente | ||
| 265 | |||
| 266 | my $removeName = 0; | ||
| 267 | |||
| 268 | my $argA = $self->parameters(0,undef,\@_, | ||
| 269 | -removeName => \$removeName, | ||
| 270 | ); | ||
| 271 | my @files = map {$p->expandTilde($_)} @$argA; | ||
| 272 | |||
| 273 | # Führe Operation aus | ||
| 274 | |||
| 275 | @files = sort {$p->mtime($a) <=> $p->mtime($b)} @files; | ||
| 276 | |||
| 277 | my @paths; | ||
| 278 | for my $file (@files) { | ||
| 279 | push @paths,$self->add($file,-removeName=>$removeName); | ||
| 280 | } | ||
| 281 | |||
| 282 | return @paths; | ||
| 283 | } | ||
| 284 | |||
| 285 | # ----------------------------------------------------------------------------- | ||
| 286 | |||
| 287 | =head1 VERSION | ||
| 288 | |||
| 289 | 1.240 | ||
| 290 | |||
| 291 | =head1 AUTHOR | ||
| 292 | |||
| 293 | Frank Seitz, L<http://fseitz.de/> | ||
| 294 | |||
| 295 | =head1 COPYRIGHT | ||
| 296 | |||
| 297 | Copyright (C) 2026 Frank Seitz | ||
| 298 | |||
| 299 | =head1 LICENSE | ||
| 300 | |||
| 301 | This code is free software; you can redistribute it and/or modify | ||
| 302 | it under the same terms as Perl itself. | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | # ----------------------------------------------------------------------------- | ||
| 307 | |||
| 308 | 1; | ||
| 309 | |||
| 310 | # eof | ||