Laravel Vapor - Fix path to upload documents to S3 bucket with dynamic path

This commit is contained in:
Dillon Ngo
2023-12-28 23:26:53 +08:00
parent aa319fadc9
commit 0f61a84fc5
3 changed files with 33 additions and 22 deletions
@@ -54,17 +54,20 @@ class RenderDocumentLogic extends AbstractControllerLogic
$this->canRenderDocument->passes();
// if(!Storage::disk('documents')->exists($file))
// {
// throw new ResourceNotFoundException();
// }
// return $this->response(['src' => explode('.', $file)[1] == 'pdf' ? chunk_split(base64_encode(Storage::disk('documents')->get($file))) : Storage::disk('documents')->get($file) ]);
if(!Storage::disk('s3')->exists($file))
{
throw new ResourceNotFoundException();
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver == 's3'){
if(!Storage::disk('s3')->exists($file))
{
throw new ResourceNotFoundException();
}
return $this->response(['src' => explode('.', $file)[1] == 'pdf' ? chunk_split(base64_encode(Storage::disk('documents')->get($file))) : Storage::disk('documents')->get($file) ]);
}
else{
if(!Storage::disk('documents')->exists($file))
{
throw new ResourceNotFoundException();
}
return $this->response(['src' => explode('.', $file)[1] == 'pdf' ? chunk_split(base64_encode(Storage::disk('documents')->get($file))) : Storage::disk('documents')->get($file) ]);
}
return $this->response(['src' => explode('.', $file)[1] == 'pdf' ? chunk_split(base64_encode(Storage::disk('documents')->get($file))) : Storage::disk('documents')->get($file) ]);
}
}
@@ -4,6 +4,7 @@ namespace App\Classes\Modules\Documents\DataTransferObjects;
use App\Classes\General\Interfaces\DataTransferObject;
use App\Classes\Modules\Documents\Services\ConvertsBase64ToFile;
use Illuminate\Support\Facades\Storage;
class DocumentObject implements DataTransferObject
{
@@ -72,9 +73,14 @@ class DocumentObject implements DataTransferObject
*/
public function getFiles(): array
{
return (new ConvertsBase64ToFile($this->path))->convert($this->files);
$filePath = $this->path;
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver == 's3'){
$filePath = 'documents/'.$this->path;
}
return (new ConvertsBase64ToFile($filePath))->convert($this->files);
}
}
}
@@ -99,15 +99,17 @@ class ConvertsBase64ToFile
*/
private function generateFile(FileObject $file, string $suffix = '') {
// $filePath = $this->path.'/'.$file->getFileName().$suffix.'.'.$file->getExtension();
// Storage::disk('documents')->put($filePath, $file->getDecodedData());
// return $filePath;
$filePath = 'localhost/'.$file->getFileName().$suffix.'.'.$file->getExtension();
Storage::put($filePath, $file->getDecodedData(), 's3');
return $filePath;
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver == 's3'){
$filePath = $this->path.'/'.$file->getFileName().$suffix.'.'.$file->getExtension();
Storage::put($filePath, $file->getDecodedData(), 's3');
return $filePath;
}
else{
$filePath = $this->path.'/'.$file->getFileName().$suffix.'.'.$file->getExtension();
Storage::disk('documents')->put($filePath, $file->getDecodedData());
return $filePath;
}
}
/**