| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace kalanis\kw_files\Processing\Storage\Files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use kalanis\kw_files\FilesException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use kalanis\kw_files\Interfaces; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use kalanis\kw_files\Traits\TCheckModes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use kalanis\kw_files\Traits\TLang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use kalanis\kw_paths\ArrayPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use kalanis\kw_paths\Extras\TPathTransform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use kalanis\kw_storage\Interfaces\IStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use kalanis\kw_storage\StorageException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Class AFiles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @package kalanis\kw_files\Processing\Storage\Files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Process files in storages - deffer when you can access them directly or must be a middleman there | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | abstract class AFiles implements Interfaces\IProcessFiles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     use TCheckModes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     use TLang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     use TPathTransform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected IStorage $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 38 |  |     public function __construct(IStorage $storage, ?Interfaces\IFLTranslations $lang = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 38 |  |         $this->storage = $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 38 |  |         $this->setFlLang($lang); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 38 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 17 |  |     public function saveFile(array $targetName, string $content, ?int $offset = null, int $mode = 0): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 17 |  |         $this->checkSupportedModes($mode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 17 |  |         $path = $this->getStorageSeparator() . $this->filledName($this->compactName($targetName, $this->getStorageSeparator())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 17 |  |             $dstArr = new ArrayPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 17 |  |             $dstArr->setArray($targetName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 17 |  |             $tgt = $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 17 |  |             if (!empty($tgt) && !$this->storage->exists($this->getStorageSeparator() . $tgt)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 // parent dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                 throw new FilesException($this->getFlLang()->flCannotSaveFile($path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 16 |  |             $target = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 16 |  |             if (FILE_APPEND == $mode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |                 if ($this->storage->exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |                     $target = $this->storage->read($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 16 |  |             if (!is_null($offset)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 // put it somewhere, left the rest intact | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  |                 $target = str_pad(substr($target, 0, $offset), $offset, "\0"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  |                 return $this->storage->write($path, $target . $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 14 |  |                 return $this->storage->write($path, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |         } catch (StorageException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |             throw new FilesException($this->getFlLang()->flCannotSaveFile($path), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 12 |  |     public function readFile(array $entry, ?int $offset = null, ?int $length = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 12 |  |         $path = $this->getStorageSeparator() . $this->filledName($this->compactName($entry, $this->getStorageSeparator())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 12 |  |             $content = $this->storage->read($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             // shit with substr... that needed undefined params was from some java dude?! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 8 |  |             if (!is_null($length)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |                 return mb_substr($content, intval($offset), $length); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 8 |  |             if (!is_null($offset)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |                 return mb_substr($content, $offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 8 |  |             return $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 4 |  |         } catch (StorageException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |             throw new FilesException($this->getFlLang()->flCannotLoadFile($path), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 6 |  |     public function deleteFile(array $entry): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 6 |  |         $path = $this->getStorageSeparator() . $this->filledName($this->compactName($entry, $this->getStorageSeparator())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 6 |  |             return $this->storage->remove($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |         } catch (StorageException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |             throw new FilesException($this->getFlLang()->flCannotRemoveFile($path), $ex->getCode(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 35 |  |     protected function getStorageSeparator(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 35 |  |         return DIRECTORY_SEPARATOR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 33 |  |     protected function filledName(string $path): string | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 33 |  |         $sepLen = mb_strlen($this->getStorageSeparator()); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 33 |  |         $beginning = mb_substr($path, 0, $sepLen); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 33 |  |         return ($this->getStorageSeparator() == $beginning) ? mb_substr($path, $sepLen) : $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  |