lundi 7 mai 2018

Laravel and DropzoneJS file uploaded with different extension

I created a form here with Laravel and DropzoneJS and I tried uploading a Gimp file (.xcf) and when it is uploaded it is saved in S3 as the following

<random-name>.

without the "xcf" extension just the dot.

Also, I created a text file and renamed it to test.xcf when I tried uploading that file it uploaded it with the .txt extension.

Here is my UploadController.php which handles the upload:

<?php

namespace App\Http\Controllers;

use App\Upload;
use Illuminate\Http\Request;

class UploadController extends Controller
{
    public function upload(Request $request)
    {
        $originalName = $request->file('file')->getClientOriginalName();
        $fileSize = $request->file('file')->getClientSize();
        $path = $request->file('file')->store('documents');
        $explode = explode('documents/', $path);
        $name = $explode[1];
        $uniqueId = $this->generateUniqueId();

        $upload = new Upload();
        $upload->unique_id = $uniqueId;
        $upload->name = $name;
        $upload->path = $path;
        $upload->original_name = $originalName;
        $upload->size = $fileSize;

        if ($upload->save())
        {
            return response()->json([
                'original_name' => $originalName,
                'size' => $fileSize,
                'url' => env('AWS_URL') . $path,
                'id' => $uniqueId,
                'status' => 'OK'
            ]);
        }

        return response()->json(['status' => 'BAD', 'message' => 'There was a problem saving your file.']);
    }

    public function generateUniqueId()
    {
        $result = '1';
        $result .= rand(100000000, 999999999);

        while(Upload::where('unique_id', '=', $result)->first())
        {
            $result = '1';
            $result .= rand(100000000, 999999999);
        }

        return $result;
    }
}

I've got no idea why it's doing that.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire