File indexing completed on 2024-06-16 05:26:40

0001 <?php
0002 /**
0003  * file server - part of Opendesktop.org platform project <https://www.opendesktop.org>.
0004  *
0005  * Copyright (c) 2016 pling GmbH.
0006  *
0007  * This program is free software: you can redistribute it and/or modify
0008  * it under the terms of the GNU Affero General Public License as
0009  * published by the Free Software Foundation, either version 3 of the
0010  * License, or (at your option) any later version.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU Affero General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Affero General Public License
0018  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0019  */
0020 
0021 namespace Ocs\Storage;
0022 
0023 class FilesystemAdapter implements AdapterInterface
0024 {
0025 
0026     private object $appConfig;
0027 
0028     public function __construct(object $appConfig)
0029     {
0030         $this->appConfig = $appConfig;
0031     }
0032 
0033     /**
0034      * @inheritDoc
0035      */
0036     public function moveUploadedFile(string $from, string $to): bool
0037     {
0038         return move_uploaded_file($from, $to);
0039     }
0040 
0041     public function fixFilename(string $name, string $collectionPath): string
0042     {
0043         if (is_file($collectionPath . DIRECTORY_SEPARATOR . $name)) {
0044             $fix = date('YmdHis');
0045             if (preg_match("/^([^.]+)(\..+)/", $name, $matches)) {
0046                 $name = $matches[1] . '-' . $fix . $matches[2];
0047             } else {
0048                 $name = $name . '-' . $fix;
0049             }
0050         }
0051 
0052         return $name;
0053     }
0054 
0055     public function prepareCollectionPath(string $collectionName, string $filePath): bool
0056     {
0057         return !is_dir($filePath . DIRECTORY_SEPARATOR . $collectionName) && mkdir($filePath . DIRECTORY_SEPARATOR . $collectionName);
0058     }
0059 
0060     /** Return true if dir exists and is writeable, otherwise it tries to create the path
0061      *
0062      * @param string $dir
0063      * @return bool
0064      */
0065     public function testAndCreate(string $dir): bool
0066     {
0067         if (is_dir($dir)) {
0068             if (!is_writable($dir)) {
0069                 return chmod($dir, 0755);
0070             }
0071 
0072             return true;
0073         }
0074 
0075         return mkdir($dir, 0755, true);
0076     }
0077 
0078     public function moveFile($from, $to): bool
0079     {
0080         return is_file($from) && $this->testAndCreate(dirname($to)) && copy($from, $to) && unlink($from);
0081     }
0082 
0083     public function copyFile($from, $to): bool
0084     {
0085         return is_file($from) && $this->testAndCreate(dirname($to)) && copy($from, $to);
0086     }
0087 
0088     public function isFile($from): bool
0089     {
0090         return is_file($from);
0091     }
0092 
0093     public function isDir($from) {
0094         return is_dir($from);
0095     }
0096 
0097     public function renameDir($from, $to) {
0098         return is_dir($from) && is_dir(dirname($to)) && rename($from, $to);
0099     }
0100 
0101 }