R2 파일 업로드
This commit is contained in:
@@ -103,7 +103,7 @@ public function fileable()
|
||||
*/
|
||||
public function getStoragePath(): string
|
||||
{
|
||||
return Storage::disk('tenant')->path($this->file_path);
|
||||
return $this->file_path;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,22 +111,38 @@ public function getStoragePath(): string
|
||||
*/
|
||||
public function exists(): bool
|
||||
{
|
||||
return Storage::disk('tenant')->exists($this->file_path);
|
||||
return Storage::disk('r2')->exists($this->file_path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get download response
|
||||
* Get download response (streaming from R2)
|
||||
*
|
||||
* @param bool $inline true = 브라우저에서 바로 표시 (이미지/PDF), false = 다운로드
|
||||
*/
|
||||
public function download()
|
||||
public function download(bool $inline = false)
|
||||
{
|
||||
if (! $this->exists()) {
|
||||
abort(404, 'File not found in storage');
|
||||
}
|
||||
|
||||
return response()->download(
|
||||
$this->getStoragePath(),
|
||||
$this->display_name ?? $this->original_name
|
||||
);
|
||||
$fileName = $this->display_name ?? $this->original_name;
|
||||
$mimeType = $this->mime_type ?? 'application/octet-stream';
|
||||
$disposition = $inline ? 'inline' : 'attachment';
|
||||
|
||||
// Stream from R2 (메모리에 전체 로드하지 않음)
|
||||
$stream = Storage::disk('r2')->readStream($this->file_path);
|
||||
|
||||
return response()->stream(function () use ($stream) {
|
||||
fpassthru($stream);
|
||||
if (is_resource($stream)) {
|
||||
fclose($stream);
|
||||
}
|
||||
}, 200, [
|
||||
'Content-Type' => $mimeType,
|
||||
'Content-Disposition' => $disposition . '; filename="' . $fileName . '"',
|
||||
'Content-Length' => $this->file_size,
|
||||
'Cache-Control' => 'private, max-age=3600',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,9 +165,9 @@ public function moveToFolder(Folder $folder): bool
|
||||
$this->stored_name ?? $this->file_name
|
||||
);
|
||||
|
||||
// Move physical file
|
||||
if (Storage::disk('tenant')->exists($this->file_path)) {
|
||||
Storage::disk('tenant')->move($this->file_path, $newPath);
|
||||
// Move physical file in R2
|
||||
if (Storage::disk('r2')->exists($this->file_path)) {
|
||||
Storage::disk('r2')->move($this->file_path, $newPath);
|
||||
}
|
||||
|
||||
// Update DB
|
||||
@@ -182,9 +198,9 @@ public function softDeleteFile(int $userId): void
|
||||
*/
|
||||
public function permanentDelete(): void
|
||||
{
|
||||
// Delete physical file
|
||||
// Delete physical file from R2
|
||||
if ($this->exists()) {
|
||||
Storage::disk('tenant')->delete($this->file_path);
|
||||
Storage::disk('r2')->delete($this->file_path);
|
||||
}
|
||||
|
||||
// Decrement tenant storage
|
||||
|
||||
Reference in New Issue
Block a user