2020-10-25 20:53:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aweos\Resizer\Classes;
|
|
|
|
|
|
|
|
use Queue;
|
|
|
|
use Storage;
|
2020-10-27 01:49:52 +01:00
|
|
|
use Aweos\Resizer\Models\SourceFile;
|
|
|
|
use Aweos\Resizer\Models\Attachment;
|
2020-10-25 20:53:10 +01:00
|
|
|
|
|
|
|
class UploadStorage {
|
|
|
|
|
|
|
|
public $disk = 'uploads';
|
|
|
|
public $file;
|
|
|
|
public $data;
|
|
|
|
public $strategy = FirstLetterStrategy::class;
|
|
|
|
|
|
|
|
public function storeFileFromUpload($uploadedFile, $data) {
|
|
|
|
$this->file = $uploadedFile;
|
|
|
|
$this->data = $data;
|
|
|
|
|
2020-10-27 01:49:52 +01:00
|
|
|
$sourceFile = new SourceFile([
|
|
|
|
'basename' => pathinfo($this->file->getClientOriginalName(), PATHINFO_FILENAME),
|
|
|
|
'extension' => $this->file->getClientOriginalExtension(),
|
|
|
|
'tags' => []
|
|
|
|
]);
|
|
|
|
$sourceFile->slugAttributes();
|
|
|
|
|
|
|
|
$uploadedFile->storeAs('', $sourceFile->path, $this->disk);
|
|
|
|
$sourceFile->save();
|
2020-10-25 20:53:10 +01:00
|
|
|
|
2020-10-27 01:49:52 +01:00
|
|
|
$attachment = $sourceFile->attachments()->create([
|
|
|
|
'title' => $this->data['title'],
|
|
|
|
'description' => $this->data['description'] ?? ''
|
|
|
|
]);
|
2020-10-25 20:53:10 +01:00
|
|
|
|
|
|
|
Queue::push(CompressJob::class, [
|
2020-10-27 01:49:52 +01:00
|
|
|
'attachment_id' => $attachment->id,
|
2020-10-25 20:53:10 +01:00
|
|
|
'crop' => $this->data['crop']
|
|
|
|
]);
|
|
|
|
|
2020-10-27 01:49:52 +01:00
|
|
|
return $attachment;
|
2020-10-25 23:02:13 +01:00
|
|
|
}
|
|
|
|
|
2020-10-27 01:49:52 +01:00
|
|
|
public function getFileData($id) {
|
|
|
|
$attachment = Attachment::find($id);
|
2020-10-25 23:53:55 +01:00
|
|
|
|
2020-10-25 23:02:13 +01:00
|
|
|
return [
|
2020-10-27 01:49:52 +01:00
|
|
|
'url' => $this->storage()->url($attachment->source->path),
|
|
|
|
'type' => $this->storage()->mimeType($attachment->source->path),
|
|
|
|
'size' => $this->storage()->size($attachment->source->path),
|
|
|
|
'name' => $attachment->slug,
|
|
|
|
'id' => $id
|
2020-10-25 23:02:13 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-10-27 01:49:52 +01:00
|
|
|
public function removeAttachment($id) {
|
|
|
|
$attachment = Attachment::find($id);
|
2020-10-25 23:02:13 +01:00
|
|
|
|
2020-10-27 01:49:52 +01:00
|
|
|
collect(Storage::disk($this->disk)->files($attachment->path))->filter(function($file) use ($attachment) {
|
|
|
|
return preg_match($attachment->versionRegex, $file);
|
2020-10-25 23:02:13 +01:00
|
|
|
})->each(function($file) {
|
|
|
|
Storage::disk($this->disk)->delete($file);
|
|
|
|
});
|
|
|
|
|
2020-10-27 01:49:52 +01:00
|
|
|
$attachment->delete();
|
2020-10-25 20:53:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function storage() {
|
|
|
|
return Storage::disk($this->disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function originalExtension() {
|
|
|
|
return $this->file->getClientOriginalExtension();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getStrategy() {
|
|
|
|
return app($this->strategy);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|