65 lines
1.8 KiB
PHP
65 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class StorageController extends Controller
|
|
{
|
|
function s3migration() {
|
|
$files = File::allFiles(public_path('uploads'));
|
|
foreach ($files as $file) {
|
|
$relativePath = $file->getRelativePathname();
|
|
$fullPath = $file->getRealPath();
|
|
|
|
$stream = fopen($fullPath, 'r');
|
|
|
|
Storage::disk('s3')->put(configEnvApp()."uploads/" . $relativePath, $stream, 'public');
|
|
|
|
if (is_resource($stream)) {
|
|
fclose($stream);
|
|
}
|
|
|
|
echo "Uploaded: " . $relativePath . "\n";
|
|
}
|
|
}
|
|
function storagelistfile() {
|
|
return Storage::disk('s3')->allFiles();
|
|
}
|
|
function fileasset(){
|
|
$path = request('path');
|
|
|
|
try {
|
|
// 🔍 cek dulu file ada atau tidak
|
|
if (!Storage::disk('s3')->exists(configEnvApp().$path)) {
|
|
abort(404, 'File tidak ditemukan');
|
|
}
|
|
|
|
return response()->stream(function () use ($path) {
|
|
$stream = Storage::disk('s3')->readStream(configEnvApp().$path);
|
|
|
|
if (!$stream) {
|
|
abort(404, 'File tidak ditemukan');
|
|
}
|
|
|
|
fpassthru($stream);
|
|
|
|
if (is_resource($stream)) {
|
|
fclose($stream);
|
|
}
|
|
}, 200, [
|
|
'Content-Type' => Storage::disk('s3')->mimeType($path) ?? 'application/octet-stream',
|
|
'Content-Disposition' => 'attachment; filename="'.basename($path).'"',
|
|
'X-Content-Type-Options' => 'nosniff',
|
|
]);
|
|
|
|
} catch (\Throwable $th) {
|
|
// optional: log error
|
|
|
|
abort(404, 'File tidak ditemukan');
|
|
}
|
|
}
|
|
}
|