88use League \Flysystem \FilesystemOperator ;
99use League \Flysystem \MountManager ;
1010use Psr \Log \LoggerInterface ;
11- use Psr \Log \NullLogger ;
1211use RZ \Roadiz \Documents \Models \DocumentInterface ;
1312use RZ \Roadiz \Documents \Models \FileHashInterface ;
1413use RZ \Roadiz \Documents \Models \FolderInterface ;
2322 */
2423abstract class AbstractDocumentFactory
2524{
26- private LoggerInterface $ logger ;
2725 private ?File $ file = null ;
2826 private ?FolderInterface $ folder = null ;
29- private FilesystemOperator $ documentsStorage ;
30- private DocumentFinderInterface $ documentFinder ;
3127
3228 public function __construct (
33- FilesystemOperator $ documentsStorage ,
34- DocumentFinderInterface $ documentFinder ,
35- ? LoggerInterface $ logger = null ,
29+ protected readonly FilesystemOperator $ documentsStorage ,
30+ protected readonly DocumentFinderInterface $ documentFinder ,
31+ protected readonly LoggerInterface $ logger ,
3632 ) {
3733 if (!$ documentsStorage instanceof MountManager) {
3834 trigger_error ('Document Storage must be a MountManager to address public and private files. ' , E_USER_WARNING );
3935 }
40- $ this ->documentsStorage = $ documentsStorage ;
41- $ this ->documentFinder = $ documentFinder ;
42- $ this ->logger = $ logger ?? new NullLogger ();
4336 }
4437
4538 public function getFile (): File
@@ -133,10 +126,10 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
133126 if (false !== $ fileHash && !$ allowDuplicates ) {
134127 $ existingDocument = $ this ->documentFinder ->findOneByHashAndAlgorithm ($ fileHash , $ this ->getHashAlgorithm ());
135128 if (null !== $ existingDocument ) {
136- if (
137- $ existingDocument -> isRaw ()
138- && null !== $ existingDownscaledDocument = $ existingDocument -> getDownscaledDocument ()
139- ) {
129+ /*
130+ * If existing document is a RAW, serve its downscaled version
131+ */
132+ if ( null !== $ existingDownscaledDocument = $ existingDocument -> getDownscaledDocument () ) {
140133 $ existingDocument = $ existingDownscaledDocument ;
141134 }
142135 if (null !== $ this ->folder ) {
@@ -146,7 +139,10 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
146139 $ this ->logger ->info (sprintf (
147140 'File %s already exists with same checksum, do not upload it twice. ' ,
148141 $ existingDocument ->getFilename ()
149- ));
142+ ), [
143+ 'path ' => $ existingDocument ->getMountPath (),
144+ ]);
145+ (new Filesystem ())->remove ($ file ->getPathname ());
150146
151147 return $ existingDocument ;
152148 }
@@ -217,7 +213,7 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
217213 }
218214 }
219215
220- $ document ->setFolder (\mb_substr ( hash ( ' crc32b ' , date ( ' YmdHi ' )), 0 , 12 ));
216+ $ document ->setFolder (DocumentFolderGenerator:: generateFolderName ( ));
221217 }
222218
223219 $ document ->setFilename ($ this ->getFileName ());
0 commit comments