sekolah_adiwiyata/app/Http/Controllers/StorageController.php

77 lines
2.1 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');
}
$expl = explode('.',$path);
if($expl[1] == 'pdf'){
$mime = 'application/pdf';
}elseif($expl[1] == 'jpeg'){
$mime = 'image/jpeg';
}elseif($expl[1] == 'png'){
$mime = 'image/png';
}else{
$mime = 'application/octet-stream';
}
return response()->stream(function () use ($path,$mime) {
$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' => $mime,
'Content-Disposition' => 'inline; filename="'.basename($path).'"',
'X-Content-Type-Options' => 'nosniff',
]);
} catch (\Throwable $th) {
// optional: log error
abort(404, 'File tidak ditemukan');
}
}
}