| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace kalanis\kw_files\Processing\Storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use kalanis\kw_files\FilesException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use kalanis\kw_files\Interfaces; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use kalanis\kw_files\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use kalanis\kw_paths\ArrayPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use kalanis\kw_paths\Extras\TPathTransform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use kalanis\kw_storage\Interfaces\IStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use kalanis\kw_storage\StorageException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Class ProcessFileStream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package kalanis\kw_files\Processing\Storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Process files in many ways | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class ProcessFileStream implements Interfaces\IProcessFileStreams | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     use Traits\TLang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     use Traits\TToStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     use Traits\TToString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     use TPathTransform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected IStorage $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 24 |  |     public function __construct(IStorage $storage, ?Interfaces\IFLTranslations $lang = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 24 |  |         $this->setFlLang($lang); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 24 |  |         $this->storage = $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 24 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 10 |  |     public function saveFileStream(array $entry, $content, int $mode = 0, bool $throwErrorForParent = true): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 10 |  |         $this->checkSupportedModes($mode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 9 |  |         $path = $this->getStorageSeparator() . $this->filledName($this->compactName($entry, $this->getStorageSeparator())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 9 |  |             $dstArr = new ArrayPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 9 |  |             $dstArr->setArray($entry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 9 |  |             $tgt = $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 9 |  |             if (!empty($tgt) && !$this->storage->exists($this->getStorageSeparator() . $tgt)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 // parent dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |                 if ($throwErrorForParent) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |                     throw new FilesException($this->getFlLang()->flCannotSaveFile($path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |                     return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 7 |  |             if (FILE_APPEND == $mode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 // put it somewhere, left the rest intact | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |                 if ($this->storage->exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |                     $target = $this->storage->read($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |                     $target = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |                 return $this->storage->write($path, $target . $this->toString($path, $content)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 6 |  |                 return $this->storage->write($path, $this->toString($path, $content)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |         } catch (StorageException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |             throw new FilesException($this->getFlLang()->flCannotSaveFile($path), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 7 |  |     public function readFileStream(array $entry) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 7 |  |         $path = $this->getStorageSeparator() . $this->filledName($this->compactName($entry, $this->getStorageSeparator())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 7 |  |             return $this->toStream($path, $this->storage->read($path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 3 |  |         } catch (StorageException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |             throw new FilesException($this->getFlLang()->flCannotLoadFile($path), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 7 |  |     public function copyFileStream(array $source, array $dest): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 7 |  |         $srcPath = $this->getStorageSeparator() . $this->filledName($this->compactName($source, $this->getStorageSeparator())); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 7 |  |         $dstPath = $this->getStorageSeparator() . $this->filledName($this->compactName($dest, $this->getStorageSeparator())); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 7 |  |             if ($this->storage->exists($dstPath)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 6 |  |                 return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |         } catch (StorageException $ex) { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 1 |  |             throw new FilesException($this->getFlLang()->flCannotCopyFile($srcPath, $dstPath), $ex->getCode(), $ex); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 4 |  |         $stream = $this->readFileStream($source); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 3 |  |         if (!@rewind($stream)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             // @codeCoverageIgnoreStart | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             throw new FilesException($this->getFlLang()->flCannotSeekFile($this->compactName($source, $this->getStorageSeparator()))); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         // @codeCoverageIgnoreEnd | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 3 |  |         return $this->saveFileStream($dest, $stream,0, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 5 |  |     public function moveFileStream(array $source, array $dest): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 5 |  |         $srcPath = $this->getStorageSeparator() . $this->filledName($this->compactName($source, $this->getStorageSeparator())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 5 |  |         $dstPath = $this->getStorageSeparator() . $this->filledName($this->compactName($dest, $this->getStorageSeparator())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 5 |  |             $r1 = $this->copyFileStream($source, $dest); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 4 |  |             $r2 = $this->storage->remove($srcPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 3 |  |             return $r1 && $r2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |         } catch (StorageException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |             throw new FilesException($this->getFlLang()->flCannotMoveFile($srcPath, $dstPath), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param int<0, max> $mode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @throws FilesException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 10 |  |     protected function checkSupportedModes(int $mode): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 10 |  |         if (!in_array($mode, [0, FILE_APPEND])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |             throw new FilesException($this->getFlLang()->flBadMode($mode)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 16 |  |     protected function getStorageSeparator(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 16 |  |         return DIRECTORY_SEPARATOR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 16 |  |     protected function filledName(string $path): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 16 |  |         $sepLen = mb_strlen($this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 16 |  |         $beginning = mb_substr($path, 0, $sepLen); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 16 |  |         return ($this->getStorageSeparator() == $beginning) ? mb_substr($path, $sepLen) : $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @codeCoverageIgnore only when path fails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     protected function noDirectoryDelimiterSet(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         return $this->getFlLang()->flNoDirectoryDelimiterSet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |  |