芝麻web文件管理V1.00
编辑当前文件:/home/sditechnicalteam/public_html/app/Helper/Files.php
'local']); $uploadedFile = $image; $folder = $dir . '/'; if (!$uploadedFile->isValid()) { throw new \Exception('File was not uploaded correctly'); } if ($uploadedFile->getClientOriginalExtension() === 'php') { throw new \Exception('You are not allowed to upload the php file on server'); } $newName = self::generateNewFileName($uploadedFile->getClientOriginalName()); $tempPath = public_path(self::UPLOAD_FOLDER . '/temp/' . $newName); /** Check if folder exits or not. If not then create the folder */ self::createDirectoryIfNotExist($folder); $newPath = $folder . '/' . $newName; $uploadedFile->storeAs('temp', $newName); if (!empty($crop)) { // Crop image if (isset($crop[0])) { // To store the multiple images for the copped ones foreach ($crop as $cropped) { $image = Image::make($tempPath); if (isset($cropped['resize']['width']) && isset($cropped['resize']['height'])) { $image->crop(floor($cropped['width']), floor($cropped['height']), floor($cropped['x']), floor($cropped['y'])); $fileName = str_replace('.', '_' . $cropped['resize']['width'] . 'x' . $cropped['resize']['height'] . '.', $newName); $tempPathCropped = public_path(self::UPLOAD_FOLDER . '/temp') . '/' . $fileName; $newPathCropped = $folder . '/' . $fileName; $image->save($tempPathCropped); Storage::put($newPathCropped, File::get($tempPathCropped), ['public']); // Deleting cropped temp file File::delete($tempPathCropped); } } } else { $image = Image::make($tempPath); $image->crop(floor($crop['width']), floor($crop['height']), floor($crop['x']), floor($crop['y'])); $image->save(); } } if (($width || $height) && \File::extension($uploadedFile->getClientOriginalName()) !== 'svg') { // Crop image $image = Image::make($tempPath); $image->resize($width, $height, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); $image->save(); } Storage::put($newPath, File::get($tempPath), ['public']); // Deleting temp file File::delete($tempPath); return $newName; } public static function generateNewFileName($currentFileName) { $ext = strtolower(File::extension($currentFileName)); $newName = md5(microtime()); return ($ext === '') ? $newName : $newName . '.' . $ext; } /** * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @throws \Exception */ public static function uploadLocalOrS3($uploadedFile, $dir) { if (!$uploadedFile->isValid()) { throw new \Exception('File was not uploaded correctly'); } if (config('filesystems.default') === 'local') { $newName = self::upload($uploadedFile, $dir, false, false, false); // Add data to file_storage table return self::fileStore($uploadedFile, $dir, $newName); } // Add data to file_storage table $newName = self::fileStore($uploadedFile, $dir); // We have given 2 options of upload for now s3 and local Storage::disk(config('filesystems.default'))->putFileAs($dir, $uploadedFile, $newName); try { // Upload files to aws s3 or digitalocean Storage::disk(config('filesystems.default'))->missing($dir . '/' . $newName); } catch (\Exception) { throw new \Exception('File not uploaded successfully'); } return $newName; } public static function fileStore($file, $folder, $generateNewName = '') { // Keep $generateNewName empty if you do not want to generate new name $newName = ($generateNewName == '') ? self::generateNewFileName($file->getClientOriginalName()) : $generateNewName; $setting = StorageSetting::where('status', 'enabled')->first(); $storageLocation = $setting->filesystem; $fileStorage = new FileStorage(); $fileStorage->filename = $newName; $fileStorage->size = $file->getSize(); $fileStorage->type = $file->getClientMimeType(); $fileStorage->path = $folder; $fileStorage->storage_location = $storageLocation; $fileStorage->save(); return $newName; } public static function deleteFile($filename, $folder) { $dir = trim($folder, '/'); $fileExist = FileStorage::where('filename', $filename)->first(); if ($fileExist) { $fileExist->delete(); } // Delete from Cloud if (in_array(config('filesystems.default'), ['s3', 'digitalocean'])) { if (Storage::disk(config('filesystems.default'))->exists($dir . '/' . $filename)) { Storage::disk(config('filesystems.default'))->delete($dir . '/' . $filename); } return true; } // Delete from Local $path = Files::UPLOAD_FOLDER . '/' . $dir . '/' . $filename; if (!File::exists(public_path($path))) { return true; } if (File::exists(public_path($path))) { try { Storage::delete($path); } catch (\Throwable) { return true; } } } public static function deleteDirectory($folder) { $dir = trim($folder); Storage::deleteDirectory($dir); return true; } public static function copy($from, $to) { Storage::disk(config('filesystems.default'))->copy($from, $to); } public static function createDirectoryIfNotExist($folder) { /** Check if folder exits or not. If not then create the folder */ if (!File::exists(public_path(self::UPLOAD_FOLDER . '/' . $folder))) { File::makeDirectory(public_path(self::UPLOAD_FOLDER . '/' . $folder), 0775, true); } } }