Storage support for local disk (#165)
* Storage support for local disk * UI change ai feature page --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
28
app/Http/Controllers/Content/FileUploadController.php
Normal file
28
app/Http/Controllers/Content/FileUploadController.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Content;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Forms\PublicFormController;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class FileUploadController extends Controller
|
||||
{
|
||||
/**
|
||||
* Upload file to local temp
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function upload(Request $request)
|
||||
{
|
||||
$uuid = (string) Str::uuid();
|
||||
$path = $request->file('file')->storeAs(PublicFormController::TMP_FILE_UPLOAD_PATH, $uuid);
|
||||
|
||||
return response()->json([
|
||||
'uuid' => $uuid,
|
||||
'key' => $path
|
||||
], 201);
|
||||
}
|
||||
}
|
||||
@@ -177,12 +177,12 @@ class FormController extends Controller
|
||||
|
||||
// Make sure we retrieve the file in tmp storage, move it to persistent
|
||||
$fileName = PublicFormController::TMP_FILE_UPLOAD_PATH.'/'.$fileNameParser->uuid;;
|
||||
if (!Storage::disk('s3')->exists($fileName)) {
|
||||
if (!Storage::exists($fileName)) {
|
||||
// File not found, we skip
|
||||
return null;
|
||||
}
|
||||
$newPath = self::ASSETS_UPLOAD_PATH.'/'.$fileNameParser->getMovedFileName();
|
||||
Storage::disk('s3')->move($fileName, $newPath);
|
||||
Storage::move($fileName, $newPath);
|
||||
|
||||
return $this->success([
|
||||
'message' => 'File uploaded.',
|
||||
@@ -199,12 +199,12 @@ class FormController extends Controller
|
||||
$this->authorize('view', $form);
|
||||
|
||||
$path = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $form->id).'/'.$fileName;
|
||||
if (!Storage::disk('s3')->exists($path)) {
|
||||
if (!Storage::exists($path)) {
|
||||
return $this->error([
|
||||
'message' => 'File not found.'
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect()->to(Storage::disk('s3')->temporaryUrl($path, now()->addMinutes(5)));
|
||||
return redirect()->to(Storage::temporaryUrl($path, now()->addMinutes(5)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,14 +57,14 @@ class FormSubmissionController extends Controller
|
||||
$fileName = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $id).'/'
|
||||
.urldecode($fileName);
|
||||
|
||||
if (!Storage::disk('s3')->exists($fileName)) {
|
||||
if (!Storage::exists($fileName)) {
|
||||
return $this->error([
|
||||
'message' => 'File not found.',
|
||||
], 404);
|
||||
}
|
||||
|
||||
return redirect(
|
||||
Storage::disk('s3')->temporaryUrl($fileName, now()->addMinute())
|
||||
Storage::temporaryUrl($fileName, now()->addMinute())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,14 +66,14 @@ class PublicFormController extends Controller
|
||||
public function showAsset($assetFileName)
|
||||
{
|
||||
$path = FormController::ASSETS_UPLOAD_PATH.'/'.$assetFileName;
|
||||
if (!Storage::disk('s3')->exists($path)) {
|
||||
if (!Storage::exists($path)) {
|
||||
return $this->error([
|
||||
'message' => 'File not found.',
|
||||
'file_name' => $assetFileName
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect()->to(Storage::disk('s3')->temporaryUrl($path, now()->addMinutes(5)));
|
||||
|
||||
return redirect()->to(Storage::temporaryUrl($path, now()->addMinutes(5)));
|
||||
}
|
||||
|
||||
public function answer(AnswerFormRequest $request)
|
||||
|
||||
@@ -13,6 +13,7 @@ class VerifyCsrfToken extends Middleware
|
||||
*/
|
||||
protected $except = [
|
||||
'stripe/webhook',
|
||||
'vapor/signed-storage-url'
|
||||
'vapor/signed-storage-url',
|
||||
'upload-file'
|
||||
];
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ class StoreFormSubmissionJob implements ShouldQueue
|
||||
private function isSkipForUpload($value)
|
||||
{
|
||||
$newPath = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $this->form->id);
|
||||
return Storage::disk('s3')->exists($newPath.'/'.$value);
|
||||
return Storage::exists($newPath.'/'.$value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,7 +165,7 @@ class StoreFormSubmissionJob implements ShouldQueue
|
||||
|
||||
// Make sure we retrieve the file in tmp storage, move it to persistent
|
||||
$fileName = PublicFormController::TMP_FILE_UPLOAD_PATH.'/'.$fileNameParser->uuid;
|
||||
if (!Storage::disk('s3')->exists($fileName)) {
|
||||
if (!Storage::exists($fileName)) {
|
||||
// File not found, we skip
|
||||
return null;
|
||||
}
|
||||
@@ -178,7 +178,7 @@ class StoreFormSubmissionJob implements ShouldQueue
|
||||
'form_id' => $this->form->id,
|
||||
'form_slug' => $this->form->slug,
|
||||
]);
|
||||
Storage::disk('s3')->move($fileName, $completeNewFilename);
|
||||
Storage::move($fileName, $completeNewFilename);
|
||||
|
||||
return $fileNameParser->getMovedFileName();
|
||||
}
|
||||
@@ -193,7 +193,7 @@ class StoreFormSubmissionJob implements ShouldQueue
|
||||
$newPath = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $this->form->id);
|
||||
$completeNewFilename = $newPath.'/'.$fileName;
|
||||
|
||||
Storage::disk('s3')->put($completeNewFilename, base64_decode(explode(',', $value)[1]));
|
||||
Storage::put($completeNewFilename, base64_decode(explode(',', $value)[1]));
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ use Illuminate\Support\ServiceProvider;
|
||||
use Laravel\Cashier\Cashier;
|
||||
use Laravel\Dusk\DuskServiceProvider;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
@@ -18,6 +19,16 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
if(config('filesystems.default') === 'local'){
|
||||
Storage::disk('local')->buildTemporaryUrlsUsing(function ($path, $expiration, $options) {
|
||||
return URL::temporarySignedRoute(
|
||||
'local.temp',
|
||||
$expiration,
|
||||
array_merge($options, ['path' => $path])
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
JsonResource::withoutWrapping();
|
||||
Cashier::calculateTaxes();
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ class StorageFile implements Rule
|
||||
// This is use when updating a record, and file uploads aren't changed.
|
||||
if($this->form){
|
||||
$newPath = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $this->form->id);
|
||||
if(Storage::disk('s3')->exists($newPath.'/'.$value)){
|
||||
if(Storage::exists($newPath.'/'.$value)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user