Skip to content

Commit 92adc1a

Browse files
committed
feat: add functionality to find attachment by url
1 parent bfc0d80 commit 92adc1a

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/AttachmentManager.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Facades\Config;
99
use Illuminate\Support\Facades\Storage;
10+
use Illuminate\Support\Str;
1011
use VanOns\LaravelAttachmentLibrary\Adapters\FileMetadata\MetadataAdapter;
1112
use VanOns\LaravelAttachmentLibrary\DataTransferObjects\Directory;
1213
use VanOns\LaravelAttachmentLibrary\DataTransferObjects\FileMetadata;
@@ -95,14 +96,6 @@ protected function getFilesystem(): Filesystem
9596
return Storage::disk($this->disk);
9697
}
9798

98-
/**
99-
* Return single file or null for given ID.
100-
*/
101-
public function find(string|int $id): ?Attachment
102-
{
103-
return $this->attachmentClass::find($id);
104-
}
105-
10699
/**
107100
* Return files under a given path.
108101
*
@@ -121,6 +114,25 @@ public function file(string $path): ?Attachment
121114
return $this->attachmentClass::whereDisk($this->disk)->whereFilename(new Filename($path))->first();
122115
}
123116

117+
/**
118+
* Return single file or null for given ID.
119+
*/
120+
public function find(string|int $id): ?Attachment
121+
{
122+
return $this->attachmentClass::whereDisk($this->disk)->find($id);
123+
}
124+
125+
/**
126+
* Return single file or null for given URL.
127+
*/
128+
public function findByUrl(string $url): ?Attachment
129+
{
130+
$baseUrl = Str::chopEnd(route('attachment', ['attachment' => 'PLACEHOLDER']), 'PLACEHOLDER');
131+
$path = Str::chopStart($url, $baseUrl);
132+
133+
return $this->file($path);
134+
}
135+
124136
/**
125137
* Upload a file to the selected disk under the desired path and create a database entry.
126138
*

0 commit comments

Comments
 (0)