oc-resizer-plugin/classes/UploadStorage.php

81 lines
2.2 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' => []
]);
$sourceFile->slugAttributes();
$uploadedFile->storeAs('', $sourceFile->path, $this->disk);
$sourceFile->save();
$attachment = $sourceFile->attachments()->create([
'title' => $this->data['title'],
'description' => $this->data['description'] ?? ''
]);
Queue::push(CompressJob::class, [
'attachment_id' => $attachment->id,
'crop' => $this->data['crop']
]);
return $attachment;
}
public function getFileData($id) {
$attachment = Attachment::find($id);
return [
'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
];
}
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);
}
}