Skip to content

Commit 68960bf

Browse files
committed
er
1 parent 9d6bb3c commit 68960bf

File tree

5 files changed

+113
-267
lines changed

5 files changed

+113
-267
lines changed

notes.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
add cleaner
22

3-
add attach from url, stream ,
3+
add attach from uploadedFile, remote url, stream, sockets, ...
44

5-
add image optimizer
5+
add image service: optimizer, resizer
66

7-
add jobs, events for upload, image process
7+
add jobs, events for upload, image process, ...
88

99
add file to attachmentable by specific user logged in or ...
10+
11+

src/Services/AttachmentService.php

+6-235
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Carbon\Carbon;
66
use Illuminate\Http\UploadedFile;
77
use Intervention\Image\Facades\Image;
8-
use Illuminate\Support\Facades\Storage;
98

109

1110
final class AttachmentService extends Service
@@ -131,56 +130,29 @@ function mkdir_if_not_exists($dirPath)
131130
}
132131
}
133132

134-
135-
136-
/**
137-
* Creates a file object from a file an uploaded file.
138-
*
139-
* @param UploadedFile $uploadedFile source file
140-
* @param string $disk target storage disk
141-
*
142-
* @return $this|null
143-
*/
144-
public function fromPost($uploadedFile, $disk = null)
133+
public function fromPost($file, $disk = null)
145134
{
146-
if ($uploadedFile === null) {
135+
if ($file === null) {
147136
return null;
148137
}
149138

150-
$this->filename = $uploadedFile->getClientOriginalName();
151-
$this->filesize = method_exists($uploadedFile, 'getSize') ? $uploadedFile->getSize() : $uploadedFile->getClientSize();
152-
$this->filetype = $uploadedFile->getMimeType();
139+
$this->filename = $file->getClientOriginalName();
140+
$this->filesize = method_exists($file, 'getSize') ? $file->getSize() : $file->getClientSize();
141+
$this->filetype = $file->getMimeType();
153142
$this->filepath = $this->filepath ?: ($this->getStorageDirectory() . $this->getPartitionDirectory() . $this->getDiskName());
154143
$this->filename = $file->getFilename();
155144
$this->filesize = $file->getSize();
156145
$this->filetype = $file->getMimeType();
157146
$this->filepath = $this->filepath ?: ($this->getStorageDirectory() . $this->getPartitionDirectory() . $this->getDiskName());
158147
$this->putFile($file->getRealPath(), $this->filepath);
159148

160-
$this->putFile($uploadedFile->getRealPath(), $this->filepath);
149+
$this->putFile($file->getRealPath(), $this->filepath);
161150

162151
return $this;
163152
}
164153

165-
166-
167-
/**
168-
* Register an outputting model event with the dispatcher.
169-
*
170-
* @param \Closure|string $callback
171-
*
172-
* @return void
173-
*/
174-
public static function outputting($callback)
175-
{
176-
static::registerModelEvent('outputting', $callback);
177-
}
178-
179154
public function output($disposition = 'inline')
180155
{
181-
if ($this->fireModelEvent('outputting') === false) {
182-
return false;
183-
}
184156

185157
header("Content-type: " . $this->filetype);
186158
header('Content-Disposition: ' . $disposition . '; filename="' . $this->filename . '"');
@@ -193,42 +165,6 @@ public function output($disposition = 'inline')
193165
exit($this->getContents());
194166
}
195167

196-
197-
/**
198-
* Get file contents from storage device.
199-
*/
200-
public function getContents()
201-
{
202-
return $this->storageCommand('get', $this->filepath);
203-
}
204-
205-
206-
/**
207-
* Get a metadata value by key with dot notation
208-
*
209-
* @param string $key The metadata key, supports dot notation
210-
* @param mixed $default The default value to return if key is not found
211-
*
212-
* @return array|mixed
213-
*/
214-
public function metadata($key, $default = null)
215-
{
216-
if (is_null($key)) {
217-
return $this->metadata;
218-
}
219-
220-
return Arr::get($this->metadata, $key, $default);
221-
}
222-
223-
224-
/**
225-
* Saves a file
226-
*
227-
* @param string $sourcePath An absolute local path to a file name to read from.
228-
* @param string $filePath A storage file path to save to.
229-
*
230-
* @return bool
231-
*/
232168
protected function putFile($sourcePath, $filePath = null)
233169
{
234170
if ( ! $filePath) {
@@ -252,169 +188,4 @@ protected function putFile($sourcePath, $filePath = null)
252188
return FileHelper::copy($sourcePath, $destinationPath . basename($filePath));
253189
}
254190

255-
256-
protected function deleteFile()
257-
{
258-
$this->storageCommand('delete', $this->filepath);
259-
$this->deleteEmptyDirectory($this->path);
260-
}
261-
262-
263-
/**
264-
* Generates a disk name from the supplied file name.
265-
*/
266-
protected function getDiskName()
267-
{
268-
if ($this->filepath !== null) {
269-
return $this->filepath;
270-
}
271-
272-
$ext = strtolower($this->getExtension());
273-
$name = str_replace('.', '', $this->uuid);
274-
275-
return $this->filepath = $ext !== null ? $name . '.' . $ext : $name;
276-
}
277-
278-
/**
279-
* Generate a temporary url at which the current file can be downloaded until $expire
280-
*
281-
* @param Carbon $expire
282-
* @param bool $inline
283-
*
284-
* @return string
285-
*/
286-
public function getTemporaryUrl(Carbon $expire, $inline = false)
287-
{
288-
289-
$payload = Crypt::encryptString(collect([
290-
'id' => $this->uuid,
291-
'expire' => $expire->getTimestamp(),
292-
'shared_at' => Carbon::now()->getTimestamp(),
293-
'disposition' => $inline ? 'inline' : 'attachment',
294-
])->toJson());
295-
296-
return route('attachments.download-shared', ['token' => $payload]);
297-
298-
}
299-
300-
301-
/**
302-
* Generates a partition for the file.
303-
* return /ABC/DE1/234 for an name of ABCDE1234.
304-
*
305-
* @return mixed
306-
*/
307-
protected function getPartitionDirectory()
308-
{
309-
return implode('/', array_slice(str_split($this->uuid, 3), 0, 3)) . '/';
310-
}
311-
312-
313-
/**
314-
* Define the internal storage path, override this method to define.
315-
*/
316-
protected function getStorageDirectory()
317-
{
318-
return config('attachments.storage_directory.prefix', 'attachments') . '/';
319-
}
320-
321-
322-
/**
323-
* Returns true if a directory contains no files.
324-
*
325-
* @param string|null $dir the directory path
326-
*
327-
* @return bool
328-
*/
329-
protected function isDirectoryEmpty($dir)
330-
{
331-
if ( ! $dir || ! $this->storageCommand('exists', $dir)) {
332-
return null;
333-
}
334-
335-
return count($this->storageCommand('allFiles', $dir)) === 0;
336-
}
337-
338-
339-
/**
340-
* Copy the local file to Storage
341-
*
342-
* @param string $localPath
343-
* @param string $storagePath
344-
*
345-
* @return bool
346-
*/
347-
protected function copyToStorage($localPath, $storagePath)
348-
{
349-
return Storage::disk($this->disk)->put($storagePath, FileHelper::get($localPath));
350-
}
351-
352-
353-
/**
354-
* Checks if directory is empty then deletes it,
355-
* three levels up to match the partition directory.
356-
*
357-
* @param string|null $dir the directory path
358-
*
359-
* @return void
360-
*/
361-
protected function deleteEmptyDirectory($dir = null)
362-
{
363-
if ( ! $this->isDirectoryEmpty($dir)) {
364-
return;
365-
}
366-
367-
$this->storageCommand('deleteDirectory', $dir);
368-
369-
$dir = dirname($dir);
370-
371-
if ( ! $this->isDirectoryEmpty($dir)) {
372-
return;
373-
}
374-
375-
$this->storageCommand('deleteDirectory', $dir);
376-
377-
$dir = dirname($dir);
378-
379-
if ( ! $this->isDirectoryEmpty($dir)) {
380-
return;
381-
}
382-
383-
$this->storageCommand('deleteDirectory', $dir);
384-
}
385-
386-
387-
/**
388-
* Calls a method against File or Storage depending on local storage.
389-
* This allows local storage outside the storage/app folder and is
390-
* also good for performance. For local storage, *every* argument
391-
* is prefixed with the local root path.
392-
*
393-
* @param string $string the command string
394-
* @param string $filepath the path on storage
395-
*
396-
* @return mixed
397-
*/
398-
protected function storageCommand($string, $filepath)
399-
{
400-
$args = func_get_args();
401-
$command = array_shift($args);
402-
403-
if ($this->isLocalStorage()) {
404-
$interface = 'File';
405-
$path = $this->getLocalRootPath();
406-
$args = array_map(function ($value) use ($path) {
407-
return $path . '/' . $value;
408-
}, $args);
409-
} else {
410-
if (substr($filepath, 0, 1) !== '/') {
411-
$args[0] = $filepath = '/' . $filepath;
412-
}
413-
414-
$interface = Storage::disk($this->disk);
415-
}
416-
417-
return forward_static_call_array([$interface, $command], $args);
418-
}
419-
420191
}

src/Services/DownloadService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function __construct()
99
parent::__construct();
1010
}
1111

12-
public function download()
12+
public function download($file, $options = null)
1313
{
1414
//
1515
}

src/Services/Service.php

+71
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,76 @@ public function getFileUrl($file)
8484
}
8585

8686

87+
public function getFileMetadata($key, $default = null)
88+
{
89+
if (is_null($key)) {
90+
return $this->metadata;
91+
}
92+
93+
return Arr::get($this->metadata, $key, $default);
94+
}
95+
96+
/**
97+
* Generate a temporary url at which the current file can be downloaded until $expire
98+
*
99+
* @param Carbon $expire
100+
* @param bool $inline
101+
*
102+
* @return string
103+
*/
104+
public function getTemporaryUrl(Carbon $expire, $inline = false)
105+
{
106+
107+
$payload = Crypt::encryptString(collect([
108+
'id' => $this->uuid,
109+
'expire' => $expire->getTimestamp(),
110+
'shared_at' => Carbon::now()->getTimestamp(),
111+
'disposition' => $inline ? 'inline' : 'attachment',
112+
])->toJson());
113+
114+
return route('attachments.download-shared', ['token' => $payload]);
115+
116+
}
117+
118+
protected function isDirectoryEmpty($dir)
119+
{
120+
if ( ! $dir || ! $this->storageCommand('exists', $dir)) {
121+
return null;
122+
}
123+
124+
return count($this->storageCommand('allFiles', $dir)) === 0;
125+
}
126+
127+
128+
protected function copyToStorage($localPath, $storagePath)
129+
{
130+
return Storage::disk($this->disk)->put($storagePath, FileHelper::get($localPath));
131+
}
132+
133+
protected function deleteEmptyDirectory($dir = null)
134+
{
135+
if ( ! $this->isDirectoryEmpty($dir)) {
136+
return;
137+
}
138+
139+
$this->storageCommand('deleteDirectory', $dir);
140+
141+
$dir = dirname($dir);
142+
143+
if ( ! $this->isDirectoryEmpty($dir)) {
144+
return;
145+
}
146+
147+
$this->storageCommand('deleteDirectory', $dir);
148+
149+
$dir = dirname($dir);
150+
151+
if ( ! $this->isDirectoryEmpty($dir)) {
152+
return;
153+
}
154+
155+
$this->storageCommand('deleteDirectory', $dir);
156+
}
157+
87158

88159
}

0 commit comments

Comments
 (0)