diff --git a/src/Database/Attach/File.php b/src/Database/Attach/File.php index 67c4f39c2..05fa4d517 100644 --- a/src/Database/Attach/File.php +++ b/src/Database/Attach/File.php @@ -1,4 +1,6 @@ -file_name = $uploadedFile->getClientOriginalName(); $this->file_size = $uploadedFile->getSize(); @@ -125,11 +129,8 @@ public function fromPost($uploadedFile) /** * Creates a file object from a file on the local filesystem. - * - * @param string $filePath The path to the file. - * @return static */ - public function fromFile($filePath, $filename = null) + public function fromFile(string $filePath, ?string $filename = null): static { $file = new FileObj($filePath); $this->file_name = empty($filename) ? $file->getFilename() : $filename; @@ -172,12 +173,8 @@ public function fromStorage(string $filePath): static /** * Creates a file object from raw data. - * - * @param string $data The raw data. - * @param string $filename The name of the file. - * @return static */ - public function fromData($data, $filename) + public function fromData(mixed $data, string $filename): static { $tempName = str_replace('.', '', uniqid('', true)) . '.tmp'; $tempPath = temp_path($tempName); @@ -191,12 +188,8 @@ public function fromData($data, $filename) /** * Creates a file object from url - * - * @param string $url The URL to retrieve and store. - * @param string|null $filename The name of the file. If null, the filename will be extracted from the URL. - * @return static */ - public function fromUrl($url, $filename = null) + public function fromUrl(string $url, ?string $filename = null): static { $data = Http::get($url); @@ -238,31 +231,24 @@ public function fromUrl($url, $filename = null) /** * Helper attribute for getPath. - * - * @return string */ - public function getPathAttribute() + public function getPathAttribute(): string { return $this->getPath(); } /** * Helper attribute for getExtension. - * - * @return string */ - public function getExtensionAttribute() + public function getExtensionAttribute(): string { return $this->getExtension(); } /** * Used only when filling attributes. - * - * @param mixed $value - * @return void */ - public function setDataAttribute($value) + public function setDataAttribute(mixed $value): void { $this->data = $value; } @@ -271,10 +257,8 @@ public function setDataAttribute($value) * Helper attribute for get image width. * * Returns `null` if this file is not an image. - * - * @return string|int|null */ - public function getWidthAttribute() + public function getWidthAttribute(): string|int|null { if ($this->isImage()) { $dimensions = $this->getImageDimensions(); @@ -289,10 +273,8 @@ public function getWidthAttribute() * Helper attribute for get image height. * * Returns `null` if this file is not an image. - * - * @return string|int|null */ - public function getHeightAttribute() + public function getHeightAttribute(): string|int|null { if ($this->isImage()) { $dimensions = $this->getImageDimensions(); @@ -305,10 +287,8 @@ public function getHeightAttribute() /** * Helper attribute for file size in human format. - * - * @return string */ - public function getSizeAttribute() + public function getSizeAttribute(): string { return $this->sizeToString(); } @@ -391,11 +371,8 @@ public function outputThumb($width, $height, $options = [], $returnResponse = fa /** * Returns the cache key used for the hasFile method - * - * @param string|null $path The path to get the cache key for - * @return string */ - public function getCacheKey($path = null) + public function getCacheKey(?string $path = null): string { if (empty($path)) { $path = $this->getDiskPath(); @@ -406,31 +383,24 @@ public function getCacheKey($path = null) /** * Returns the file name without path - * - * @return string */ - public function getFilename() + public function getFilename(): string { return $this->file_name; } /** * Returns the file extension. - * - * @return string */ - public function getExtension() + public function getExtension(): string { return FileHelper::extension($this->file_name); } /** * Returns the last modification date as a UNIX timestamp. - * - * @param string|null $fileName - * @return int */ - public function getLastModified($fileName = null) + public function getLastModified(?string $fileName = null): int { return $this->storageCmd('lastModified', $this->getDiskPath($fileName)); } @@ -439,10 +409,8 @@ public function getLastModified($fileName = null) * Returns the file content type. * * Returns `null` if the file content type cannot be determined. - * - * @return string|null */ - public function getContentType() + public function getContentType(): ?string { if ($this->content_type !== null) { return $this->content_type; @@ -458,22 +426,16 @@ public function getContentType() /** * Get file contents from storage device. - * - * @param string|null $fileName - * @return string */ - public function getContents($fileName = null) + public function getContents(?string $fileName = null): mixed { return $this->storageCmd('get', $this->getDiskPath($fileName)); } /** * Returns the public address to access the file. - * - * @param string|null $fileName - * @return string */ - public function getPath($fileName = null) + public function getPath(?string $fileName = null): string { if (empty($fileName)) { $fileName = $this->disk_name; @@ -484,10 +446,8 @@ public function getPath($fileName = null) /** * Returns a local path to this file. If the file is stored remotely, * it will be downloaded to a temporary directory. - * - * @return string */ - public function getLocalPath() + public function getLocalPath(): string { if ($this->isLocalStorage()) { return $this->getLocalRootPath() . '/' . $this->getDiskPath(); @@ -506,11 +466,8 @@ public function getLocalPath() /** * Returns the path to the file, relative to the storage disk. - * - * @param string|null $fileName - * @return string */ - public function getDiskPath($fileName = null) + public function getDiskPath(?string $fileName = null): string { if (empty($fileName)) { $fileName = $this->disk_name; @@ -520,10 +477,8 @@ public function getDiskPath($fileName = null) /** * Determines if the file is flagged "public" or not. - * - * @return bool */ - public function isPublic() + public function isPublic(): bool { if (array_key_exists('is_public', $this->attributes)) { return (bool) $this->attributes['is_public']; @@ -538,10 +493,8 @@ public function isPublic() /** * Returns the file size as string. - * - * @return string */ - public function sizeToString() + public function sizeToString(): string { return FileHelper::sizeToString($this->file_size); } @@ -595,20 +548,16 @@ public function afterDelete() /** * Checks if the file extension is an image and returns true or false. - * - * @return bool */ - public function isImage() + public function isImage(): bool { return in_array(strtolower($this->getExtension()), static::$imageExtensions); } /** * Get image dimensions - * - * @return array|false */ - protected function getImageDimensions() + protected function getImageDimensions(): array|false { return getimagesize($this->getLocalPath()); } @@ -756,10 +705,8 @@ protected function makeThumb(string $thumbFile, string $thumbPath, int $width, i /** * Delete all thumbnails for this file. - * - * @return void */ - public function deleteThumbs() + public function deleteThumbs(): void { $pattern = 'thumb_'.$this->id.'_'; @@ -823,12 +770,8 @@ protected function getLocalTempPath(?string $path = null): string /** * Saves a file - * - * @param string $sourcePath An absolute local path to a file name to read from. - * @param string|null $destinationFileName A storage file name to save to. - * @return bool */ - protected function putFile($sourcePath, $destinationFileName = null) + protected function putFile(string $sourcePath, ?string $destinationFileName = null): bool { if (!$destinationFileName) { $destinationFileName = $this->disk_name; @@ -863,11 +806,8 @@ protected function putFile($sourcePath, $destinationFileName = null) /** * Delete file contents from storage device. - * - * @param string|null $fileName - * @return void */ - protected function deleteFile($fileName = null) + protected function deleteFile(?string $fileName = null): void { if (!$fileName) { $fileName = $this->disk_name; @@ -886,11 +826,8 @@ protected function deleteFile($fileName = null) /** * Check file exists on storage device. - * - * @param string|null $fileName - * @return bool */ - protected function hasFile($fileName = null) + protected function hasFile(?string $fileName = null): bool { $filePath = $this->getDiskPath($fileName); @@ -908,11 +845,8 @@ protected function hasFile($fileName = null) /** * Checks if directory is empty then deletes it, three levels up to match the partition directory. - * - * @param string|null $dir Directory to check and delete if empty. - * @return void */ - protected function deleteEmptyDirectory($dir = null) + protected function deleteEmptyDirectory(?string $dir = null): void { if (!$this->isDirectoryEmpty($dir)) { return; @@ -939,9 +873,8 @@ protected function deleteEmptyDirectory($dir = null) * Returns true if a directory contains no files. * * @param string|null $dir Directory to check. - * @return bool */ - protected function isDirectoryEmpty($dir = null) + protected function isDirectoryEmpty(?string $dir = null): bool { return count($this->storageCmd('allFiles', $dir)) === 0; } @@ -1019,30 +952,22 @@ public static function getMaxFilesize() /** * Define the internal storage path, override this method to define. - * - * @return string */ - public function getStorageDirectory() + public function getStorageDirectory(): string { if ($this->isPublic()) { - return 'uploads/public/'; + return 'public/'; } - return 'uploads/protected/'; + return 'protected/'; } /** - * Define the public address for the storage path. - * - * @return string + * Returns the public URL to the storage directory folder for this model. */ - public function getPublicPath() + public function getPublicPath(): string { - if ($this->isPublic()) { - return 'http://localhost/uploads/public/'; - } - - return 'http://localhost/uploads/protected/'; + return $this->getDisk()->url($this->getStorageDirectory()); } /** @@ -1063,20 +988,16 @@ public function getTempPath() /** * Returns the storage disk the file is stored on - * - * @return FilesystemAdapter */ - public function getDisk() + public function getDisk(): FilesystemAdapter { - return Storage::disk(); + return Storage::disk(static::DISK); } /** * Returns true if the storage engine is local. - * - * @return bool */ - protected function isLocalStorage() + protected function isLocalStorage(): bool { return FileHelper::isLocalDisk($this->getDisk()); } @@ -1085,10 +1006,8 @@ protected function isLocalStorage() * Generates a partition for the file. * * For example, returns `/ABC/DE1/234` for an name of `ABCDE1234`. - * - * @return string */ - protected function getPartitionDirectory() + protected function getPartitionDirectory(): string { return implode('/', array_slice(str_split($this->disk_name, 3), 0, 3)) . '/'; }