88use League \Flysystem \FilesystemOperator ;
99use League \Flysystem \MountManager ;
1010use Psr \Log \LoggerInterface ;
11+ use Psr \Log \NullLogger ;
1112use RZ \Roadiz \Documents \Models \DocumentInterface ;
1213use RZ \Roadiz \Documents \Models \FileHashInterface ;
1314use RZ \Roadiz \Documents \Models \FolderInterface ;
2223 */
2324abstract class AbstractDocumentFactory
2425{
26+ private LoggerInterface $ logger ;
2527 private ?File $ file = null ;
2628 private ?FolderInterface $ folder = null ;
29+ private FilesystemOperator $ documentsStorage ;
30+ private DocumentFinderInterface $ documentFinder ;
2731
2832 public function __construct (
29- protected readonly FilesystemOperator $ documentsStorage ,
30- protected readonly DocumentFinderInterface $ documentFinder ,
31- protected readonly LoggerInterface $ logger ,
33+ FilesystemOperator $ documentsStorage ,
34+ DocumentFinderInterface $ documentFinder ,
35+ ? LoggerInterface $ logger = null ,
3236 ) {
3337 if (!$ documentsStorage instanceof MountManager) {
3438 trigger_error ('Document Storage must be a MountManager to address public and private files. ' , E_USER_WARNING );
3539 }
40+ $ this ->documentsStorage = $ documentsStorage ;
41+ $ this ->documentFinder = $ documentFinder ;
42+ $ this ->logger = $ logger ?? new NullLogger ();
3643 }
3744
3845 public function getFile (): File
@@ -126,10 +133,10 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
126133 if (false !== $ fileHash && !$ allowDuplicates ) {
127134 $ existingDocument = $ this ->documentFinder ->findOneByHashAndAlgorithm ($ fileHash , $ this ->getHashAlgorithm ());
128135 if (null !== $ existingDocument ) {
129- /*
130- * If existing document is a RAW, serve its downscaled version
131- */
132- if ( null !== $ existingDownscaledDocument = $ existingDocument -> getDownscaledDocument () ) {
136+ if (
137+ $ existingDocument -> isRaw ()
138+ && null !== $ existingDownscaledDocument = $ existingDocument -> getDownscaledDocument ()
139+ ) {
133140 $ existingDocument = $ existingDownscaledDocument ;
134141 }
135142 if (null !== $ this ->folder ) {
@@ -139,10 +146,7 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
139146 $ this ->logger ->info (sprintf (
140147 'File %s already exists with same checksum, do not upload it twice. ' ,
141148 $ existingDocument ->getFilename ()
142- ), [
143- 'path ' => $ existingDocument ->getMountPath (),
144- ]);
145- (new Filesystem ())->remove ($ file ->getPathname ());
149+ ));
146150
147151 return $ existingDocument ;
148152 }
@@ -213,7 +217,7 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
213217 }
214218 }
215219
216- $ document ->setFolder (DocumentFolderGenerator:: generateFolderName ( ));
220+ $ document ->setFolder (\mb_substr ( hash ( ' crc32b ' , date ( ' YmdHi ' )), 0 , 12 ));
217221 }
218222
219223 $ document ->setFilename ($ this ->getFileName ());
0 commit comments