Skip to content

Commit

Permalink
added new file utility function to get the file contents, as well as …
Browse files Browse the repository at this point in the history
…claned up file upload function
  • Loading branch information
roncodes committed Jul 5, 2024
1 parent 9c5a3ce commit f7aae7c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/Http/Controllers/Internal/v1/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public function upload(UploadFileRequest $request)
$type = $request->input('type');
$size = $request->input('file_size', $request->file->getSize());
$path = $request->input('path', 'uploads');
$subjectId = $request->input('subject_uuid');
$subjectType = $request->input('subject_type');

// Generate a filename
$fileName = File::randomFileNameFromRequest($request);
Expand Down
22 changes: 22 additions & 0 deletions src/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,26 @@ public static function fromRequest(Request $request, string $param = 'files'): C

return static::whereIn('uuid', $ids)->get();
}

/**
* Retrieves the contents of the file from the specified disk and path.
*
* This method utilizes the Laravel Storage facade to access the filesystem configured
* for the model's specified disk and retrieves the file located at the model's path.
* Error handling is included to manage cases where the disk or path might not be set,
* or if the file does not exist on the disk.
*
* @return string|null the file contents as a string if found, or null if the file does not exist
*
* @throws \InvalidArgumentException if the disk or path property is not set
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException if the file does not exist
*/
public function getContents(): ?string
{
if (!isset($this->disk) || !isset($this->path)) {
throw new \InvalidArgumentException('Disk or path is not specified.');
}

return Storage::disk($this->disk)->get($this->path);
}
}

0 comments on commit f7aae7c

Please sign in to comment.