@@ -45,7 +45,7 @@ |
||
| 45 | 45 | |
| 46 | 46 | public static function boot() |
| 47 | 47 | { |
| 48 | - Route::group(['namespace' => 'Epesi\FileStorage'], function(){ |
|
| 48 | + Route::group(['namespace' => 'Epesi\FileStorage'], function() { |
|
| 49 | 49 | Route::get('file', 'FileStorageController@get')->middleware('web', FileStorageAccess::class); |
| 50 | 50 | }); |
| 51 | 51 | |
@@ -13,111 +13,111 @@ |
||
| 13 | 13 | |
| 14 | 14 | public $timestamps = false; |
| 15 | 15 | |
| 16 | - protected $table = 'filestorage_contents'; |
|
| 17 | - protected static $unguarded = true; |
|
| 16 | + protected $table = 'filestorage_contents'; |
|
| 17 | + protected static $unguarded = true; |
|
| 18 | 18 | |
| 19 | - protected $appends = ['path']; |
|
| 19 | + protected $appends = ['path']; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * One content can have many files associated with |
|
| 23 | - * The actual content is stored only once based on the content hash |
|
| 24 | - * |
|
| 25 | - * @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
| 26 | - */ |
|
| 27 | - public function files() |
|
| 28 | - { |
|
| 29 | - return $this->hasMany(File::class, 'content_id'); |
|
| 30 | - } |
|
| 21 | + /** |
|
| 22 | + * One content can have many files associated with |
|
| 23 | + * The actual content is stored only once based on the content hash |
|
| 24 | + * |
|
| 25 | + * @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
| 26 | + */ |
|
| 27 | + public function files() |
|
| 28 | + { |
|
| 29 | + return $this->hasMany(File::class, 'content_id'); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Accessor method for retrieving of file content path when using the model |
|
| 34 | - * Having the $appends property in the File model listing the 'path' makes sure the value is also exported to arrays |
|
| 35 | - * |
|
| 36 | - * @return string |
|
| 37 | - */ |
|
| 38 | - public function getPathAttribute() |
|
| 39 | - { |
|
| 40 | - return self::storage()->path($this->getStoragePath($this->hash)); |
|
| 41 | - } |
|
| 32 | + /** |
|
| 33 | + * Accessor method for retrieving of file content path when using the model |
|
| 34 | + * Having the $appends property in the File model listing the 'path' makes sure the value is also exported to arrays |
|
| 35 | + * |
|
| 36 | + * @return string |
|
| 37 | + */ |
|
| 38 | + public function getPathAttribute() |
|
| 39 | + { |
|
| 40 | + return self::storage()->path($this->getStoragePath($this->hash)); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Accessor method for retrieving of file contents |
|
| 45 | - * |
|
| 46 | - * @return string |
|
| 47 | - */ |
|
| 48 | - public function getDataAttribute() |
|
| 49 | - { |
|
| 50 | - return self::storage()->get($this->storage_path); |
|
| 51 | - } |
|
| 43 | + /** |
|
| 44 | + * Accessor method for retrieving of file contents |
|
| 45 | + * |
|
| 46 | + * @return string |
|
| 47 | + */ |
|
| 48 | + public function getDataAttribute() |
|
| 49 | + { |
|
| 50 | + return self::storage()->get($this->storage_path); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Accessor method for file relative storage path |
|
| 55 | - * |
|
| 56 | - * @return string |
|
| 57 | - */ |
|
| 58 | - public function getStoragePathAttribute() |
|
| 59 | - { |
|
| 60 | - return $this->getStoragePath($this->hash); |
|
| 61 | - } |
|
| 53 | + /** |
|
| 54 | + * Accessor method for file relative storage path |
|
| 55 | + * |
|
| 56 | + * @return string |
|
| 57 | + */ |
|
| 58 | + public function getStoragePathAttribute() |
|
| 59 | + { |
|
| 60 | + return $this->getStoragePath($this->hash); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - protected static function getStoragePath($hash) |
|
| 64 | - { |
|
| 65 | - return implode(DIRECTORY_SEPARATOR, array_merge(str_split(substr($hash, 0, 5)), [substr($hash, 5)])); |
|
| 66 | - } |
|
| 63 | + protected static function getStoragePath($hash) |
|
| 64 | + { |
|
| 65 | + return implode(DIRECTORY_SEPARATOR, array_merge(str_split(substr($hash, 0, 5)), [substr($hash, 5)])); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Returns the storage where file contents are saved based on config settings |
|
| 70 | - * |
|
| 71 | - * @return \Illuminate\Contracts\Filesystem\Filesystem |
|
| 72 | - */ |
|
| 73 | - public static function storage() |
|
| 74 | - { |
|
| 75 | - return Storage::disk(config('epesi.filestorage', 'local')); |
|
| 76 | - } |
|
| 68 | + /** |
|
| 69 | + * Returns the storage where file contents are saved based on config settings |
|
| 70 | + * |
|
| 71 | + * @return \Illuminate\Contracts\Filesystem\Filesystem |
|
| 72 | + */ |
|
| 73 | + public static function storage() |
|
| 74 | + { |
|
| 75 | + return Storage::disk(config('epesi.filestorage', 'local')); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Add file to the filestorage |
|
| 80 | - * |
|
| 81 | - * @param string $file File path to save |
|
| 82 | - * |
|
| 83 | - * @return int File id in the database |
|
| 84 | - */ |
|
| 85 | - public static function putFromFile($file) |
|
| 86 | - { |
|
| 87 | - return self::put(file_get_contents($file)); |
|
| 88 | - } |
|
| 78 | + /** |
|
| 79 | + * Add file to the filestorage |
|
| 80 | + * |
|
| 81 | + * @param string $file File path to save |
|
| 82 | + * |
|
| 83 | + * @return int File id in the database |
|
| 84 | + */ |
|
| 85 | + public static function putFromFile($file) |
|
| 86 | + { |
|
| 87 | + return self::put(file_get_contents($file)); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Add content to the filestorage |
|
| 92 | - * |
|
| 93 | - * @param string $content Content to save |
|
| 94 | - * |
|
| 95 | - * @return int File id in the database |
|
| 96 | - */ |
|
| 97 | - public static function put($content) |
|
| 98 | - { |
|
| 99 | - $hash = self::hash($content); |
|
| 90 | + /** |
|
| 91 | + * Add content to the filestorage |
|
| 92 | + * |
|
| 93 | + * @param string $content Content to save |
|
| 94 | + * |
|
| 95 | + * @return int File id in the database |
|
| 96 | + */ |
|
| 97 | + public static function put($content) |
|
| 98 | + { |
|
| 99 | + $hash = self::hash($content); |
|
| 100 | 100 | |
| 101 | - $path = self::getStoragePath($hash); |
|
| 101 | + $path = self::getStoragePath($hash); |
|
| 102 | 102 | |
| 103 | - if (! self::storage()->exists($path)) { |
|
| 104 | - self::storage()->put($path, $content); |
|
| 105 | - } |
|
| 103 | + if (! self::storage()->exists($path)) { |
|
| 104 | + self::storage()->put($path, $content); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - return self::firstOrCreate(compact('hash'), [ |
|
| 108 | - 'size' => self::storage()->size($path), |
|
| 109 | - 'type' => self::storage()->mimeType($path) |
|
| 110 | - ])->id; |
|
| 111 | - } |
|
| 107 | + return self::firstOrCreate(compact('hash'), [ |
|
| 108 | + 'size' => self::storage()->size($path), |
|
| 109 | + 'type' => self::storage()->mimeType($path) |
|
| 110 | + ])->id; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Get the hash of the content using hash method defined as constant to the class |
|
| 115 | - * |
|
| 116 | - * @param string $content |
|
| 117 | - * @return string |
|
| 118 | - */ |
|
| 119 | - public static function hash($content) |
|
| 120 | - { |
|
| 121 | - return hash(self::HASH_METHOD, $content); |
|
| 122 | - } |
|
| 113 | + /** |
|
| 114 | + * Get the hash of the content using hash method defined as constant to the class |
|
| 115 | + * |
|
| 116 | + * @param string $content |
|
| 117 | + * @return string |
|
| 118 | + */ |
|
| 119 | + public static function hash($content) |
|
| 120 | + { |
|
| 121 | + return hash(self::HASH_METHOD, $content); |
|
| 122 | + } |
|
| 123 | 123 | } |
| 124 | 124 | \ No newline at end of file |
@@ -100,7 +100,7 @@ |
||
| 100 | 100 | |
| 101 | 101 | $path = self::getStoragePath($hash); |
| 102 | 102 | |
| 103 | - if (! self::storage()->exists($path)) { |
|
| 103 | + if (!self::storage()->exists($path)) { |
|
| 104 | 104 | self::storage()->put($path, $content); |
| 105 | 105 | } |
| 106 | 106 | |
@@ -8,7 +8,7 @@ |
||
| 8 | 8 | |
| 9 | 9 | public $timestamps = false; |
| 10 | 10 | |
| 11 | - protected $table = 'filestorage_access_log'; |
|
| 12 | - protected static $unguarded = true; |
|
| 11 | + protected $table = 'filestorage_access_log'; |
|
| 12 | + protected static $unguarded = true; |
|
| 13 | 13 | |
| 14 | 14 | } |
| 15 | 15 | \ No newline at end of file |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | try { |
| 106 | 106 | $file = is_numeric($idOrMeta) ? self::get($idOrMeta) : $idOrMeta; |
| 107 | 107 | |
| 108 | - if (! file_exists($file->content->path)) { |
|
| 108 | + if (!file_exists($file->content->path)) { |
|
| 109 | 109 | throw new FileNotFound('Exception - file not found: ' . $file->content->path); |
| 110 | 110 | } |
| 111 | 111 | } catch (\Exception $exception) { |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | { |
| 131 | 131 | $ids = []; |
| 132 | 132 | foreach ((array) $files as $filePath) { |
| 133 | - if (! is_numeric($filePath)) { |
|
| 133 | + if (!is_numeric($filePath)) { |
|
| 134 | 134 | $ids[] = self::put($filePath); |
| 135 | 135 | |
| 136 | 136 | continue; |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | public static function put($fileOrPath, $content = null) |
| 164 | 164 | { |
| 165 | 165 | $file = $fileOrPath; |
| 166 | - if (! $content && is_string($fileOrPath)) { |
|
| 166 | + if (!$content && is_string($fileOrPath)) { |
|
| 167 | 167 | $content = file_get_contents($fileOrPath); |
| 168 | 168 | |
| 169 | 169 | $file = [ |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | |
| 197 | 197 | public function getThumbnailAttribute() |
| 198 | 198 | { |
| 199 | - if (! $this->thumbnailPossible()) return false; |
|
| 199 | + if (!$this->thumbnailPossible()) return false; |
|
| 200 | 200 | |
| 201 | 201 | $image = new \Imagick($this->content->path . '[0]'); |
| 202 | 202 | |
@@ -42,7 +42,9 @@ discard block |
||
| 42 | 42 | { |
| 43 | 43 | static $cache = []; |
| 44 | 44 | |
| 45 | - if (is_object($idOrLink)) return $idOrLink; |
|
| 45 | + if (is_object($idOrLink)) { |
|
| 46 | + return $idOrLink; |
|
| 47 | + } |
|
| 46 | 48 | |
| 47 | 49 | $id = self::getIdByLink($idOrLink, true, true); |
| 48 | 50 | |
@@ -73,7 +75,9 @@ discard block |
||
| 73 | 75 | { |
| 74 | 76 | static $cache = []; |
| 75 | 77 | |
| 76 | - if (is_numeric($link)) return $link; |
|
| 78 | + if (is_numeric($link)) { |
|
| 79 | + return $link; |
|
| 80 | + } |
|
| 77 | 81 | |
| 78 | 82 | if (!$useCache || !isset($cache[$link])) { |
| 79 | 83 | $cache[$link] = self::where('link', $link)->value('id'); |
@@ -116,10 +120,11 @@ discard block |
||
| 116 | 120 | throw new FileNotFound('Exception - file not found: ' . $file->content->path); |
| 117 | 121 | } |
| 118 | 122 | } catch (\Exception $exception) { |
| 119 | - if ($throwException) |
|
| 120 | - throw $exception; |
|
| 121 | - else |
|
| 122 | - return false; |
|
| 123 | + if ($throwException) { |
|
| 124 | + throw $exception; |
|
| 125 | + } else { |
|
| 126 | + return false; |
|
| 127 | + } |
|
| 123 | 128 | } |
| 124 | 129 | |
| 125 | 130 | return true; |
@@ -203,7 +208,9 @@ discard block |
||
| 203 | 208 | |
| 204 | 209 | public function getThumbnailAttribute() |
| 205 | 210 | { |
| 206 | - if (! $this->thumbnailPossible()) return false; |
|
| 211 | + if (! $this->thumbnailPossible()) { |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 207 | 214 | |
| 208 | 215 | $image = new \Imagick($this->content->path . '[0]'); |
| 209 | 216 | |
@@ -14,215 +14,215 @@ |
||
| 14 | 14 | { |
| 15 | 15 | use SoftDeletes; |
| 16 | 16 | |
| 17 | - protected $table = 'filestorage_files'; |
|
| 18 | - protected static $unguarded = true; |
|
| 17 | + protected $table = 'filestorage_files'; |
|
| 18 | + protected static $unguarded = true; |
|
| 19 | 19 | |
| 20 | - public function content() |
|
| 21 | - { |
|
| 22 | - return $this->belongsTo(FileContent::class, 'content_id'); |
|
| 23 | - } |
|
| 20 | + public function content() |
|
| 21 | + { |
|
| 22 | + return $this->belongsTo(FileContent::class, 'content_id'); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - public function links() |
|
| 26 | - { |
|
| 27 | - return $this->hasMany(FileRemoteAccess::class, 'file_id'); |
|
| 28 | - } |
|
| 25 | + public function links() |
|
| 26 | + { |
|
| 27 | + return $this->hasMany(FileRemoteAccess::class, 'file_id'); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - public function userActiveLinks() |
|
| 31 | - { |
|
| 32 | - return $this->links()->where('created_by', Auth::id())->where('expires_at', '>', date('Y-m-d H:i:s')); |
|
| 33 | - } |
|
| 30 | + public function userActiveLinks() |
|
| 31 | + { |
|
| 32 | + return $this->links()->where('created_by', Auth::id())->where('expires_at', '>', date('Y-m-d H:i:s')); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Retrieve the file |
|
| 37 | - * |
|
| 38 | - * @param int|string $idOrLink Filestorage ID or unique link string |
|
| 39 | - * @param bool $useCache Use cache or not |
|
| 40 | - * |
|
| 41 | - * @return static |
|
| 42 | - * |
|
| 43 | - * @throws FileNotFound |
|
| 44 | - */ |
|
| 45 | - public static function get($idOrLink, $useCache = true) |
|
| 46 | - { |
|
| 47 | - static $cache = []; |
|
| 48 | - |
|
| 49 | - if (is_object($idOrLink)) return $idOrLink; |
|
| 35 | + /** |
|
| 36 | + * Retrieve the file |
|
| 37 | + * |
|
| 38 | + * @param int|string $idOrLink Filestorage ID or unique link string |
|
| 39 | + * @param bool $useCache Use cache or not |
|
| 40 | + * |
|
| 41 | + * @return static |
|
| 42 | + * |
|
| 43 | + * @throws FileNotFound |
|
| 44 | + */ |
|
| 45 | + public static function get($idOrLink, $useCache = true) |
|
| 46 | + { |
|
| 47 | + static $cache = []; |
|
| 48 | + |
|
| 49 | + if (is_object($idOrLink)) return $idOrLink; |
|
| 50 | 50 | |
| 51 | - $id = self::getIdByLink($idOrLink, true, true); |
|
| 51 | + $id = self::getIdByLink($idOrLink, true, true); |
|
| 52 | 52 | |
| 53 | - if ($useCache && isset($cache[$id])) { |
|
| 54 | - return $cache[$id]; |
|
| 55 | - } |
|
| 53 | + if ($useCache && isset($cache[$id])) { |
|
| 54 | + return $cache[$id]; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - $file = self::with('content')->findOrFail($id); |
|
| 57 | + $file = self::with('content')->findOrFail($id); |
|
| 58 | 58 | |
| 59 | - if (empty($file->content['hash'])) { |
|
| 60 | - throw new FileNotFound('File object does not have corresponding content'); |
|
| 61 | - } |
|
| 59 | + if (empty($file->content['hash'])) { |
|
| 60 | + throw new FileNotFound('File object does not have corresponding content'); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - return $cache[$id] = $file; |
|
| 64 | - } |
|
| 63 | + return $cache[$id] = $file; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Get Filestorage ID by link |
|
| 68 | - * |
|
| 69 | - * @param string $link Unique link |
|
| 70 | - * @param bool $useCache Use cache or not |
|
| 71 | - * @param bool $throwException Throw exception if link is not found |
|
| 72 | - * |
|
| 73 | - * @return int Filestorage ID |
|
| 74 | - * @throws LinkNotFound |
|
| 75 | - */ |
|
| 76 | - public static function getIdByLink($link, $useCache = true, $throwException = false) |
|
| 77 | - { |
|
| 78 | - static $cache = []; |
|
| 79 | - |
|
| 80 | - if (is_numeric($link)) return $link; |
|
| 81 | - |
|
| 82 | - if (!$useCache || !isset($cache[$link])) { |
|
| 83 | - $cache[$link] = self::where('link', $link)->value('id'); |
|
| 66 | + /** |
|
| 67 | + * Get Filestorage ID by link |
|
| 68 | + * |
|
| 69 | + * @param string $link Unique link |
|
| 70 | + * @param bool $useCache Use cache or not |
|
| 71 | + * @param bool $throwException Throw exception if link is not found |
|
| 72 | + * |
|
| 73 | + * @return int Filestorage ID |
|
| 74 | + * @throws LinkNotFound |
|
| 75 | + */ |
|
| 76 | + public static function getIdByLink($link, $useCache = true, $throwException = false) |
|
| 77 | + { |
|
| 78 | + static $cache = []; |
|
| 79 | + |
|
| 80 | + if (is_numeric($link)) return $link; |
|
| 81 | + |
|
| 82 | + if (!$useCache || !isset($cache[$link])) { |
|
| 83 | + $cache[$link] = self::where('link', $link)->value('id'); |
|
| 84 | 84 | |
| 85 | - if (!$cache[$link] && $throwException) { |
|
| 86 | - throw new LinkNotFound($link); |
|
| 87 | - } |
|
| 88 | - } |
|
| 85 | + if (!$cache[$link] && $throwException) { |
|
| 86 | + throw new LinkNotFound($link); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - return $cache[$link]; |
|
| 91 | - } |
|
| 90 | + return $cache[$link]; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Mark file as deleted. Does not remove any content! |
|
| 95 | - * |
|
| 96 | - * @param int|string $idOrLink Filestorage ID or unique link |
|
| 97 | - */ |
|
| 98 | - public static function unlink($idOrLink) |
|
| 99 | - { |
|
| 93 | + /** |
|
| 94 | + * Mark file as deleted. Does not remove any content! |
|
| 95 | + * |
|
| 96 | + * @param int|string $idOrLink Filestorage ID or unique link |
|
| 97 | + */ |
|
| 98 | + public static function unlink($idOrLink) |
|
| 99 | + { |
|
| 100 | 100 | if ($id = self::getIdByLink($idOrLink, false)) { |
| 101 | 101 | self::find($id)->delete(); |
| 102 | - } |
|
| 103 | - } |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Check if file exists |
|
| 107 | - * |
|
| 108 | - * @param int|static $idOrMeta Filestorage ID or file object |
|
| 109 | - * @param bool $throwException Throw exception on missing file or return false |
|
| 110 | - * |
|
| 111 | - * @return bool True if file exists, false otherwise |
|
| 112 | - * @throws FileNotFound May be thrown if $throwException set to true |
|
| 113 | - */ |
|
| 114 | - public static function exists($idOrMeta, $throwException = false) |
|
| 115 | - { |
|
| 116 | - try { |
|
| 117 | - $file = is_numeric($idOrMeta) ? self::get($idOrMeta) : $idOrMeta; |
|
| 105 | + /** |
|
| 106 | + * Check if file exists |
|
| 107 | + * |
|
| 108 | + * @param int|static $idOrMeta Filestorage ID or file object |
|
| 109 | + * @param bool $throwException Throw exception on missing file or return false |
|
| 110 | + * |
|
| 111 | + * @return bool True if file exists, false otherwise |
|
| 112 | + * @throws FileNotFound May be thrown if $throwException set to true |
|
| 113 | + */ |
|
| 114 | + public static function exists($idOrMeta, $throwException = false) |
|
| 115 | + { |
|
| 116 | + try { |
|
| 117 | + $file = is_numeric($idOrMeta) ? self::get($idOrMeta) : $idOrMeta; |
|
| 118 | 118 | |
| 119 | - if (! file_exists($file->content->path)) { |
|
| 120 | - throw new FileNotFound('Exception - file not found: ' . $file->content->path); |
|
| 121 | - } |
|
| 122 | - } catch (\Exception $exception) { |
|
| 123 | - if ($throwException) |
|
| 124 | - throw $exception; |
|
| 125 | - else |
|
| 126 | - return false; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - return true; |
|
| 130 | - } |
|
| 119 | + if (! file_exists($file->content->path)) { |
|
| 120 | + throw new FileNotFound('Exception - file not found: ' . $file->content->path); |
|
| 121 | + } |
|
| 122 | + } catch (\Exception $exception) { |
|
| 123 | + if ($throwException) |
|
| 124 | + throw $exception; |
|
| 125 | + else |
|
| 126 | + return false; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + return true; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * Add multiple files, clone file if file id is provided. |
|
| 134 | - * May be used to update backref for all files. |
|
| 135 | - * |
|
| 136 | - * @param array $files array of existing filestorage ids or array with values for the new file |
|
| 137 | - * @param string|null $backref Backref for all files |
|
| 138 | - * @return array Newly created Meta Ids sorted in ascending order |
|
| 139 | - */ |
|
| 140 | - public static function putMany($files, $backref = null) |
|
| 141 | - { |
|
| 142 | - $ids = []; |
|
| 143 | - foreach ((array) $files as $filePath) { |
|
| 144 | - if (! is_numeric($filePath)) { |
|
| 145 | - $ids[] = self::put($filePath); |
|
| 132 | + /** |
|
| 133 | + * Add multiple files, clone file if file id is provided. |
|
| 134 | + * May be used to update backref for all files. |
|
| 135 | + * |
|
| 136 | + * @param array $files array of existing filestorage ids or array with values for the new file |
|
| 137 | + * @param string|null $backref Backref for all files |
|
| 138 | + * @return array Newly created Meta Ids sorted in ascending order |
|
| 139 | + */ |
|
| 140 | + public static function putMany($files, $backref = null) |
|
| 141 | + { |
|
| 142 | + $ids = []; |
|
| 143 | + foreach ((array) $files as $filePath) { |
|
| 144 | + if (! is_numeric($filePath)) { |
|
| 145 | + $ids[] = self::put($filePath); |
|
| 146 | 146 | |
| 147 | - continue; |
|
| 148 | - } |
|
| 147 | + continue; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - $file = self::get($filePath, false)->toArray(); |
|
| 150 | + $file = self::get($filePath, false)->toArray(); |
|
| 151 | 151 | |
| 152 | - if ($backref && $file['backref'] != $backref) { |
|
| 153 | - $file['backref'] = $backref; |
|
| 154 | - unset($file['link']); |
|
| 152 | + if ($backref && $file['backref'] != $backref) { |
|
| 153 | + $file['backref'] = $backref; |
|
| 154 | + unset($file['link']); |
|
| 155 | 155 | |
| 156 | - self::put($file); |
|
| 157 | - } |
|
| 156 | + self::put($file); |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - $ids[] = $file; |
|
| 160 | - } |
|
| 159 | + $ids[] = $file; |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - sort($ids); |
|
| 162 | + sort($ids); |
|
| 163 | 163 | |
| 164 | - return $ids; |
|
| 165 | - } |
|
| 164 | + return $ids; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * @param string|array $fileOrPath |
|
| 169 | - * @param string $content Content of the file |
|
| 170 | - * |
|
| 171 | - * @return int Filestorage ID |
|
| 172 | - * @throws LinkDuplicate |
|
| 173 | - */ |
|
| 174 | - public static function put($fileOrPath, $content = null) |
|
| 175 | - { |
|
| 176 | - $file = $fileOrPath; |
|
| 177 | - if (! $content && is_string($fileOrPath)) { |
|
| 178 | - $content = file_get_contents($fileOrPath); |
|
| 167 | + /** |
|
| 168 | + * @param string|array $fileOrPath |
|
| 169 | + * @param string $content Content of the file |
|
| 170 | + * |
|
| 171 | + * @return int Filestorage ID |
|
| 172 | + * @throws LinkDuplicate |
|
| 173 | + */ |
|
| 174 | + public static function put($fileOrPath, $content = null) |
|
| 175 | + { |
|
| 176 | + $file = $fileOrPath; |
|
| 177 | + if (! $content && is_string($fileOrPath)) { |
|
| 178 | + $content = file_get_contents($fileOrPath); |
|
| 179 | 179 | |
| 180 | - $file = [ |
|
| 181 | - 'name' => basename($fileOrPath) |
|
| 182 | - ]; |
|
| 183 | - } |
|
| 180 | + $file = [ |
|
| 181 | + 'name' => basename($fileOrPath) |
|
| 182 | + ]; |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - if (!empty($file['link']) && self::getIdByLink($file['link'], false)) { |
|
| 186 | - throw new LinkDuplicate($file['link']); |
|
| 187 | - } |
|
| 185 | + if (!empty($file['link']) && self::getIdByLink($file['link'], false)) { |
|
| 186 | + throw new LinkDuplicate($file['link']); |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - $content = $file['content']?? $content; |
|
| 189 | + $content = $file['content']?? $content; |
|
| 190 | 190 | |
| 191 | - if (is_array($content)) { |
|
| 192 | - $path = $content['path']; |
|
| 191 | + if (is_array($content)) { |
|
| 192 | + $path = $content['path']; |
|
| 193 | 193 | |
| 194 | - $file['name'] = $file['name']?? basename($path); |
|
| 194 | + $file['name'] = $file['name']?? basename($path); |
|
| 195 | 195 | |
| 196 | - $content = file_get_contents($path); |
|
| 197 | - } |
|
| 196 | + $content = file_get_contents($path); |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - unset($file['content']); |
|
| 200 | - |
|
| 201 | - return self::updateOrCreate(['id' => $file['id']?? null], array_merge([ |
|
| 202 | - 'created_at' => time(), |
|
| 203 | - 'created_by' => Auth::id(), |
|
| 204 | - 'content_id' => FileContent::put($content) |
|
| 205 | - ], $file))->id; |
|
| 206 | - } |
|
| 199 | + unset($file['content']); |
|
| 200 | + |
|
| 201 | + return self::updateOrCreate(['id' => $file['id']?? null], array_merge([ |
|
| 202 | + 'created_at' => time(), |
|
| 203 | + 'created_by' => Auth::id(), |
|
| 204 | + 'content_id' => FileContent::put($content) |
|
| 205 | + ], $file))->id; |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - public function getThumbnailAttribute() |
|
| 209 | - { |
|
| 210 | - if (! $this->thumbnailPossible()) return false; |
|
| 208 | + public function getThumbnailAttribute() |
|
| 209 | + { |
|
| 210 | + if (! $this->thumbnailPossible()) return false; |
|
| 211 | 211 | |
| 212 | - $image = new \Imagick($this->content->path . '[0]'); |
|
| 212 | + $image = new \Imagick($this->content->path . '[0]'); |
|
| 213 | 213 | |
| 214 | - $image->setImageFormat('jpg'); |
|
| 214 | + $image->setImageFormat('jpg'); |
|
| 215 | 215 | |
| 216 | - $mime = 'image/jpeg'; |
|
| 216 | + $mime = 'image/jpeg'; |
|
| 217 | 217 | |
| 218 | - $name = 'preview.jpeg'; |
|
| 218 | + $name = 'preview.jpeg'; |
|
| 219 | 219 | |
| 220 | - $contents = $image . ''; |
|
| 220 | + $contents = $image . ''; |
|
| 221 | 221 | |
| 222 | - return collect(compact('mime', 'name', 'contents')); |
|
| 223 | - } |
|
| 222 | + return collect(compact('mime', 'name', 'contents')); |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | - protected function thumbnailPossible() { |
|
| 226 | - return $this->content->type == 'application/pdf' && class_exists('Imagick'); |
|
| 227 | - } |
|
| 225 | + protected function thumbnailPossible() { |
|
| 226 | + return $this->content->type == 'application/pdf' && class_exists('Imagick'); |
|
| 227 | + } |
|
| 228 | 228 | } |
| 229 | 229 | \ No newline at end of file |
@@ -6,74 +6,74 @@ |
||
| 6 | 6 | |
| 7 | 7 | class CreateFilestorageTables extends Migration |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Run the migrations. |
|
| 11 | - * |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function up() |
|
| 15 | - { |
|
| 16 | - $this->down(); |
|
| 9 | + /** |
|
| 10 | + * Run the migrations. |
|
| 11 | + * |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function up() |
|
| 15 | + { |
|
| 16 | + $this->down(); |
|
| 17 | 17 | |
| 18 | - Schema::create('filestorage_contents', function (Blueprint $table) { |
|
| 19 | - $table->bigIncrements('id'); |
|
| 20 | - $table->string('hash', 128); |
|
| 21 | - $table->integer('size'); |
|
| 22 | - $table->string('type', 256); |
|
| 18 | + Schema::create('filestorage_contents', function (Blueprint $table) { |
|
| 19 | + $table->bigIncrements('id'); |
|
| 20 | + $table->string('hash', 128); |
|
| 21 | + $table->integer('size'); |
|
| 22 | + $table->string('type', 256); |
|
| 23 | 23 | |
| 24 | - $table->softDeletes(); |
|
| 25 | - }); |
|
| 24 | + $table->softDeletes(); |
|
| 25 | + }); |
|
| 26 | 26 | |
| 27 | - Schema::create('filestorage_files', function (Blueprint $table) { |
|
| 28 | - $table->bigIncrements('id'); |
|
| 29 | - $table->string('name', 256); |
|
| 30 | - $table->string('link', 128)->nullable(); |
|
| 31 | - $table->string('backref', 128)->nullable(); |
|
| 32 | - $table->unsignedBigInteger('content_id'); |
|
| 33 | - $table->unsignedBigInteger('created_by')->nullable(); |
|
| 34 | - $table->timestamps(); |
|
| 27 | + Schema::create('filestorage_files', function (Blueprint $table) { |
|
| 28 | + $table->bigIncrements('id'); |
|
| 29 | + $table->string('name', 256); |
|
| 30 | + $table->string('link', 128)->nullable(); |
|
| 31 | + $table->string('backref', 128)->nullable(); |
|
| 32 | + $table->unsignedBigInteger('content_id'); |
|
| 33 | + $table->unsignedBigInteger('created_by')->nullable(); |
|
| 34 | + $table->timestamps(); |
|
| 35 | 35 | |
| 36 | - $table->softDeletes(); |
|
| 36 | + $table->softDeletes(); |
|
| 37 | 37 | |
| 38 | - $table->foreign('content_id')->references('id')->on('filestorage_contents'); |
|
| 39 | - $table->foreign('created_by')->references('id')->on('users'); |
|
| 40 | - }); |
|
| 38 | + $table->foreign('content_id')->references('id')->on('filestorage_contents'); |
|
| 39 | + $table->foreign('created_by')->references('id')->on('users'); |
|
| 40 | + }); |
|
| 41 | 41 | |
| 42 | - Schema::create('filestorage_remote_access', function (Blueprint $table) { |
|
| 43 | - $table->bigIncrements('id'); |
|
| 44 | - $table->unsignedBigInteger('file_id'); |
|
| 45 | - $table->string('token', 128); |
|
| 46 | - $table->unsignedBigInteger('created_by'); |
|
| 47 | - $table->timestamp('expires_at'); |
|
| 48 | - $table->timestamps(); |
|
| 42 | + Schema::create('filestorage_remote_access', function (Blueprint $table) { |
|
| 43 | + $table->bigIncrements('id'); |
|
| 44 | + $table->unsignedBigInteger('file_id'); |
|
| 45 | + $table->string('token', 128); |
|
| 46 | + $table->unsignedBigInteger('created_by'); |
|
| 47 | + $table->timestamp('expires_at'); |
|
| 48 | + $table->timestamps(); |
|
| 49 | 49 | |
| 50 | - $table->foreign('file_id')->references('id')->on('filestorage_files'); |
|
| 51 | - $table->foreign('created_by')->references('id')->on('users'); |
|
| 52 | - }); |
|
| 50 | + $table->foreign('file_id')->references('id')->on('filestorage_files'); |
|
| 51 | + $table->foreign('created_by')->references('id')->on('users'); |
|
| 52 | + }); |
|
| 53 | 53 | |
| 54 | - Schema::create('filestorage_access_log', function (Blueprint $table) { |
|
| 55 | - $table->bigIncrements('id'); |
|
| 56 | - $table->unsignedBigInteger('file_id'); |
|
| 57 | - $table->timestamp('accessed_at'); |
|
| 58 | - $table->unsignedBigInteger('accessed_by'); |
|
| 59 | - $table->string('action', 32); |
|
| 60 | - $table->string('ip_address', 32); |
|
| 61 | - $table->string('host_name', 64); |
|
| 54 | + Schema::create('filestorage_access_log', function (Blueprint $table) { |
|
| 55 | + $table->bigIncrements('id'); |
|
| 56 | + $table->unsignedBigInteger('file_id'); |
|
| 57 | + $table->timestamp('accessed_at'); |
|
| 58 | + $table->unsignedBigInteger('accessed_by'); |
|
| 59 | + $table->string('action', 32); |
|
| 60 | + $table->string('ip_address', 32); |
|
| 61 | + $table->string('host_name', 64); |
|
| 62 | 62 | |
| 63 | - $table->foreign('file_id')->references('id')->on('filestorage_files'); |
|
| 64 | - }); |
|
| 65 | - } |
|
| 63 | + $table->foreign('file_id')->references('id')->on('filestorage_files'); |
|
| 64 | + }); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Reverse the migrations. |
|
| 69 | - * |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - public function down() |
|
| 73 | - { |
|
| 74 | - Schema::dropIfExists('filestorage_remote_access'); |
|
| 75 | - Schema::dropIfExists('filestorage_access_log'); |
|
| 76 | - Schema::dropIfExists('filestorage_files'); |
|
| 77 | - Schema::dropIfExists('filestorage_contents'); |
|
| 78 | - } |
|
| 67 | + /** |
|
| 68 | + * Reverse the migrations. |
|
| 69 | + * |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + public function down() |
|
| 73 | + { |
|
| 74 | + Schema::dropIfExists('filestorage_remote_access'); |
|
| 75 | + Schema::dropIfExists('filestorage_access_log'); |
|
| 76 | + Schema::dropIfExists('filestorage_files'); |
|
| 77 | + Schema::dropIfExists('filestorage_contents'); |
|
| 78 | + } |
|
| 79 | 79 | } |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | { |
| 16 | 16 | $this->down(); |
| 17 | 17 | |
| 18 | - Schema::create('filestorage_contents', function (Blueprint $table) { |
|
| 18 | + Schema::create('filestorage_contents', function(Blueprint $table) { |
|
| 19 | 19 | $table->bigIncrements('id'); |
| 20 | 20 | $table->string('hash', 128); |
| 21 | 21 | $table->integer('size'); |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | $table->softDeletes(); |
| 25 | 25 | }); |
| 26 | 26 | |
| 27 | - Schema::create('filestorage_files', function (Blueprint $table) { |
|
| 27 | + Schema::create('filestorage_files', function(Blueprint $table) { |
|
| 28 | 28 | $table->bigIncrements('id'); |
| 29 | 29 | $table->string('name', 256); |
| 30 | 30 | $table->string('link', 128)->nullable(); |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | $table->foreign('created_by')->references('id')->on('users'); |
| 40 | 40 | }); |
| 41 | 41 | |
| 42 | - Schema::create('filestorage_remote_access', function (Blueprint $table) { |
|
| 42 | + Schema::create('filestorage_remote_access', function(Blueprint $table) { |
|
| 43 | 43 | $table->bigIncrements('id'); |
| 44 | 44 | $table->unsignedBigInteger('file_id'); |
| 45 | 45 | $table->string('token', 128); |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | $table->foreign('created_by')->references('id')->on('users'); |
| 52 | 52 | }); |
| 53 | 53 | |
| 54 | - Schema::create('filestorage_access_log', function (Blueprint $table) { |
|
| 54 | + Schema::create('filestorage_access_log', function(Blueprint $table) { |
|
| 55 | 55 | $table->bigIncrements('id'); |
| 56 | 56 | $table->unsignedBigInteger('file_id'); |
| 57 | 57 | $table->timestamp('accessed_at'); |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | final public static function getActionUrls($file, $accessParams = []) |
| 51 | 51 | { |
| 52 | - $id = is_numeric($file)? $file: $file->id; |
|
| 52 | + $id = is_numeric($file) ? $file : $file->id; |
|
| 53 | 53 | |
| 54 | 54 | $urls = []; |
| 55 | 55 | foreach (static::$allowedActions as $action) { |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | */ |
| 67 | 67 | final protected function hasUserAccess() |
| 68 | 68 | { |
| 69 | - return $this->forUsersOnly? (bool) Auth::id(): true; |
|
| 69 | + return $this->forUsersOnly ? (bool) Auth::id() : true; |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
@@ -6,30 +6,30 @@ |
||
| 6 | 6 | |
| 7 | 7 | class FileStorageAccess |
| 8 | 8 | { |
| 9 | - public function handle($request, \Closure $next) |
|
| 10 | - { |
|
| 11 | - foreach (Integration\Joints\FileStorageAccessJoint::collect() as $joint) { |
|
| 12 | - if ($joint->accessGranted($request)) { |
|
| 13 | - $this->logFileAccess($request->get('id'), $request->get('action', 'download')); |
|
| 9 | + public function handle($request, \Closure $next) |
|
| 10 | + { |
|
| 11 | + foreach (Integration\Joints\FileStorageAccessJoint::collect() as $joint) { |
|
| 12 | + if ($joint->accessGranted($request)) { |
|
| 13 | + $this->logFileAccess($request->get('id'), $request->get('action', 'download')); |
|
| 14 | 14 | |
| 15 | - return $next($request); |
|
| 16 | - } |
|
| 17 | - } |
|
| 15 | + return $next($request); |
|
| 16 | + } |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - return response('No access to file', 401); |
|
| 20 | - } |
|
| 19 | + return response('No access to file', 401); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - protected function logFileAccess($fileId, $action, $time = null) |
|
| 23 | - { |
|
| 24 | - $ip_address = request()->ip(); |
|
| 22 | + protected function logFileAccess($fileId, $action, $time = null) |
|
| 23 | + { |
|
| 24 | + $ip_address = request()->ip(); |
|
| 25 | 25 | |
| 26 | - Database\Models\FileAccessLog::create([ |
|
| 27 | - 'file_id' => $fileId, |
|
| 28 | - 'accessed_at' => date('Y-m-d H:i:s', $time ?: time()), |
|
| 29 | - 'accessed_by' => Auth::id() ?: 0, |
|
| 30 | - 'action' => $action, |
|
| 31 | - 'ip_address' => $ip_address, |
|
| 32 | - 'host_name' => gethostbyaddr($ip_address) |
|
| 33 | - ]); |
|
| 34 | - } |
|
| 26 | + Database\Models\FileAccessLog::create([ |
|
| 27 | + 'file_id' => $fileId, |
|
| 28 | + 'accessed_at' => date('Y-m-d H:i:s', $time ?: time()), |
|
| 29 | + 'accessed_by' => Auth::id() ?: 0, |
|
| 30 | + 'action' => $action, |
|
| 31 | + 'ip_address' => $ip_address, |
|
| 32 | + 'host_name' => gethostbyaddr($ip_address) |
|
| 33 | + ]); |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -32,8 +32,7 @@ |
||
| 32 | 32 | $mime = $thumbnail['mime']; |
| 33 | 33 | $filename = $thumbnail['name']; |
| 34 | 34 | $contents = $thumbnail['contents']; |
| 35 | - } |
|
| 36 | - else { |
|
| 35 | + } else { |
|
| 37 | 36 | $mime = $file->content->type; |
| 38 | 37 | $filename = $file->name; |
| 39 | 38 | $contents = $file->content->data; |
@@ -7,50 +7,50 @@ |
||
| 7 | 7 | |
| 8 | 8 | class FileStorageController extends Controller |
| 9 | 9 | { |
| 10 | - public function get(Request $request) |
|
| 11 | - { |
|
| 12 | - try { |
|
| 13 | - $file = Database\Models\File::get($request->get('id')); |
|
| 14 | - } catch (\Exception $e) { |
|
| 15 | - abort(404); |
|
| 16 | - } |
|
| 10 | + public function get(Request $request) |
|
| 11 | + { |
|
| 12 | + try { |
|
| 13 | + $file = Database\Models\File::get($request->get('id')); |
|
| 14 | + } catch (\Exception $e) { |
|
| 15 | + abort(404); |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - $useThumbnail = false; |
|
| 19 | - $disposition = 'attachment'; |
|
| 20 | - switch ($request->get('action')) { |
|
| 21 | - case 'preview': |
|
| 22 | - $useThumbnail = true; |
|
| 23 | - // intended fallthrough |
|
| 24 | - case 'inline': |
|
| 25 | - $disposition = 'inline'; |
|
| 26 | - break; |
|
| 18 | + $useThumbnail = false; |
|
| 19 | + $disposition = 'attachment'; |
|
| 20 | + switch ($request->get('action')) { |
|
| 21 | + case 'preview': |
|
| 22 | + $useThumbnail = true; |
|
| 23 | + // intended fallthrough |
|
| 24 | + case 'inline': |
|
| 25 | + $disposition = 'inline'; |
|
| 26 | + break; |
|
| 27 | 27 | |
| 28 | - default: |
|
| 29 | - break; |
|
| 30 | - } |
|
| 28 | + default: |
|
| 29 | + break; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - if ($useThumbnail && $request->get('thumbnail', 1) && ($thumbnail = $file->thumbnail)) { |
|
| 33 | - $mime = $thumbnail['mime']; |
|
| 34 | - $filename = $thumbnail['name']; |
|
| 35 | - $contents = $thumbnail['contents']; |
|
| 36 | - } |
|
| 37 | - else { |
|
| 38 | - $mime = $file->content->type; |
|
| 39 | - $filename = $file->name; |
|
| 40 | - $contents = $file->content->data; |
|
| 41 | - } |
|
| 32 | + if ($useThumbnail && $request->get('thumbnail', 1) && ($thumbnail = $file->thumbnail)) { |
|
| 33 | + $mime = $thumbnail['mime']; |
|
| 34 | + $filename = $thumbnail['name']; |
|
| 35 | + $contents = $thumbnail['contents']; |
|
| 36 | + } |
|
| 37 | + else { |
|
| 38 | + $mime = $file->content->type; |
|
| 39 | + $filename = $file->name; |
|
| 40 | + $contents = $file->content->data; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - $headers = [ |
|
| 44 | - 'Content-Type' => $mime, |
|
| 45 | - 'Content-Length' => strlen($contents), |
|
| 46 | - 'Content-Disposition' => "$disposition; filename=\"$filename\"", |
|
| 47 | - ]; |
|
| 43 | + $headers = [ |
|
| 44 | + 'Content-Type' => $mime, |
|
| 45 | + 'Content-Length' => strlen($contents), |
|
| 46 | + 'Content-Disposition' => "$disposition; filename=\"$filename\"", |
|
| 47 | + ]; |
|
| 48 | 48 | |
| 49 | - if ($request->get('nocache')) { |
|
| 50 | - $headers['Pragma'] = 'no-cache'; |
|
| 51 | - $headers['Expires'] = '0'; |
|
| 52 | - } |
|
| 49 | + if ($request->get('nocache')) { |
|
| 50 | + $headers['Pragma'] = 'no-cache'; |
|
| 51 | + $headers['Expires'] = '0'; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - return response($contents, 200, $headers); |
|
| 55 | - } |
|
| 54 | + return response($contents, 200, $headers); |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -6,27 +6,27 @@ |
||
| 6 | 6 | use Illuminate\Support\Facades\Auth; |
| 7 | 7 | |
| 8 | 8 | class FileRemoteAccess extends Model { |
| 9 | - const DEFAULT_PERIOD = '1 week'; |
|
| 9 | + const DEFAULT_PERIOD = '1 week'; |
|
| 10 | 10 | |
| 11 | - protected $table = 'filestorage_remote_access'; |
|
| 12 | - protected static $unguarded = true; |
|
| 11 | + protected $table = 'filestorage_remote_access'; |
|
| 12 | + protected static $unguarded = true; |
|
| 13 | 13 | |
| 14 | - public static function check($fileId, $token) |
|
| 15 | - { |
|
| 16 | - return (bool) self::where('file_id', $fileId)->where('token', $token)->where('expires_at', '>', date('Y-m-d H:i:s'))->count(); |
|
| 17 | - } |
|
| 14 | + public static function check($fileId, $token) |
|
| 15 | + { |
|
| 16 | + return (bool) self::where('file_id', $fileId)->where('token', $token)->where('expires_at', '>', date('Y-m-d H:i:s'))->count(); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - public static function grant($file, $expires = self::DEFAULT_PERIOD) |
|
| 20 | - { |
|
| 19 | + public static function grant($file, $expires = self::DEFAULT_PERIOD) |
|
| 20 | + { |
|
| 21 | 21 | return self::create([ |
| 22 | 22 | 'file_id' => is_numeric($file)? $file: $file->id, |
| 23 | 23 | 'token' => md5(uniqid(rand(), true)), |
| 24 | 24 | 'created_by' => Auth::id()?: 0, |
| 25 | 25 | 'expires_at' => date('Y-m-d H:i:s', strtotime($expires)), |
| 26 | 26 | ]); |
| 27 | - } |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - public function getHrefAttribute() |
|
| 30 | - { return url('file') . '?' . http_build_query(['id' => $this->file_id, 'token' => $this->token]); |
|
| 31 | - } |
|
| 29 | + public function getHrefAttribute() |
|
| 30 | + { return url('file') . '?' . http_build_query(['id' => $this->file_id, 'token' => $this->token]); |
|
| 31 | + } |
|
| 32 | 32 | } |
| 33 | 33 | \ No newline at end of file |
@@ -19,9 +19,9 @@ |
||
| 19 | 19 | public static function grant($file, $expires = self::DEFAULT_PERIOD) |
| 20 | 20 | { |
| 21 | 21 | return self::create([ |
| 22 | - 'file_id' => is_numeric($file)? $file: $file->id, |
|
| 22 | + 'file_id' => is_numeric($file) ? $file : $file->id, |
|
| 23 | 23 | 'token' => md5(uniqid(rand(), true)), |
| 24 | - 'created_by' => Auth::id()?: 0, |
|
| 24 | + 'created_by' => Auth::id() ?: 0, |
|
| 25 | 25 | 'expires_at' => date('Y-m-d H:i:s', strtotime($expires)), |
| 26 | 26 | ]); |
| 27 | 27 | } |