oc-resizer-plugin/classes/UploadStorage.php

63 lines
1.6 KiB
PHP
Raw Normal View History

2020-10-25 20:53:10 +01:00
<?php
namespace Aweos\Resizer\Classes;
use Queue;
use Storage;
class UploadStorage {
public $disk = 'uploads';
public $file;
public $data;
public $strategy = FirstLetterStrategy::class;
public function storeFileFromUpload($uploadedFile, $data) {
$this->file = $uploadedFile;
$this->data = $data;
$fileName = $this->getStrategy()->sourceFileBasename($this->file, $this->data);
$sourcePath = $this->getStrategy()->sourcePath($fileName);
$fileName = $this->transformFileName($sourcePath, $fileName).'.'.$this->file->getClientOriginalExtension();
$uploadedFile->storeAs($sourcePath, $fileName, $this->disk);
Queue::push(CompressJob::class, [
'path' => $sourcePath,
'filename' => $fileName,
'disk' => $this->disk,
'strategy' => $this->strategy,
'crop' => $this->data['crop']
]);
return $fileName;
}
private function storage() {
return Storage::disk($this->disk);
}
private function transformFileName($path, $basename) {
if (count(glob($this->storage()->path($path).'/'.$basename.'*')) == 0) {
return $basename;
}
$i = 1;
while(count(glob($this->storage()->path($path).'/'.$basename.'-'.$i.'*')) != 0) {
$i++;
}
return $basename.'-'.$i;
}
private function originalExtension() {
return $this->file->getClientOriginalExtension();
}
private function getStrategy() {
return app($this->strategy);
}
}