Skip to content

Commit

Permalink
Add: ability to get file path on disk
Browse files Browse the repository at this point in the history
  • Loading branch information
jordyvanderhaegen committed Nov 19, 2024
1 parent 409da88 commit da71743
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ $post->storeFile('file', $request->file('attachment'));
$post->hasFile('file'); // returns true/false
$post->getFolderPath('file'); // returns posts/file
$post->getFilePath('file'); // returns posts/file/1.pdf
$post->getFilePathOnDisk('file'); // returns /path/to/storage/app/public/posts/file/1.pdf
$post->getFileMime('file'); // returns application/pdf
$post->getFileExtension('file'); // returns pdf
$post->getFileUrl('file'); // returns https://www.example.com/storage/posts/file/1.pdf
Expand Down
11 changes: 11 additions & 0 deletions src/Concerns/HasFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public function getFilePath(string $column): ?string
return $this->getFolderPath($column) . '/' . $this->getKey() . '.' . $this->getFileExtension($column);
}

public function getFilePathOnDisk(string $column): ?string
{
if (! $this->hasFile($column)) {
return null;
}

return Storage::disk($this->getFileDisk())->path(
$this->getFilePath($column)
);
}

public function getFolderPath(string $column): string
{
return $this->getTable() . '/' . $this->guessFileColumn($column);
Expand Down
11 changes: 11 additions & 0 deletions tests/Feature/Concerns/HasFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,15 @@ protected function createPostWithDocument(
...$attributes,
]);
}

/** @test */
public function it_can_get_the_file_path_on_disk()
{
Storage::fake();
$post = $this->createPostWithDocument(true, 'my-document.pdf', 'application/pdf');

$filePathOnDisk = $post->getFilePathOnDisk('document');

$this->assertEquals(Storage::path($post->getFilePath('document')), $filePathOnDisk);
}
}

0 comments on commit da71743

Please sign in to comment.