oc-resizer-plugin/classes/UploadStorage.php

91 lines
2.6 KiB
PHP

<?php
namespace Aweos\Resizer\Classes;
use Queue;
use Storage;
use Aweos\Resizer\Models\SourceFile;
use Aweos\Resizer\Models\Attachment;
class UploadStorage {
public $disk = 'uploads';
public $file;
public $data;
public $strategy = FirstLetterStrategy::class;
public function storeFileFromUpload($uploadedFile, $data) {
$this->file = $uploadedFile;
$this->data = $data;
$sourceFile = new SourceFile([
'basename' => pathinfo($this->file->getClientOriginalName(), PATHINFO_FILENAME),
'extension' => $this->file->getClientOriginalExtension(),
'tags' => [],
'aspect_ratio' => $this->data['aspectRatio'],
'min_width' => $this->data['minWidth']
]);
$sourceFile->slugAttributes();
$uploadedFile->storeAs('', $sourceFile->path, $this->disk);
$sourceFile->save();
return $this->storeAttachment($sourceFile, [
'title' => $this->data['title'],
'description' => $this->data['description'] ?? '',
'crop' => $this->data['crop']
]);
}
public function storeAttachment($sourceFile, $data) {
$attachment = $sourceFile->attachments()->create([
'title' => $data['title'],
'description' => $data['description']
]);
Queue::push(CompressJob::class, [
'attachment_id' => $attachment->id,
'crop' => $data['crop']
]);
return $attachment;
}
public function getFileData($id) {
$attachment = Attachment::find($id);
return $attachment ? [
'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
] : null;
}
public function removeAttachment($id) {
$attachment = Attachment::find($id);
collect(Storage::disk($this->disk)->files($attachment->path))->filter(function($file) use ($attachment) {
return preg_match($attachment->versionRegex, $file);
})->each(function($file) {
Storage::disk($this->disk)->delete($file);
});
$attachment->delete();
}
private function storage() {
return Storage::disk($this->disk);
}
private function originalExtension() {
return $this->file->getClientOriginalExtension();
}
private function getStrategy() {
return app($this->strategy);
}
}