Skip to content

Commit

Permalink
Merge pull request #92 from midhun-mobtexting/master
Browse files Browse the repository at this point in the history
file mime types validation
  • Loading branch information
sankarsuda authored Apr 18, 2024
2 parents a62bb36 + f7779fc commit b86f7a9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
15 changes: 14 additions & 1 deletion resources/assets/js/bootstrap/filepond.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
function load_app_filepond() {
window.pond = function (selector) {
FilePond.registerPlugin(FilePondPluginFileValidateSize);
FilePond.registerPlugin(FilePondPluginFileValidateSize, FilePondPluginFileValidateType);

var form = selector.parents('form:eq(0)');
var prefix = selector.attr('data-upload-prefix') || '';
var accept = selector.attr('accept') || '';

if (prefix && typeof prefix === 'undefined') {
prefix = '';
Expand All @@ -16,6 +17,7 @@ function load_app_filepond() {
var options = {
credits: false,
maxFileSize: '500MB',
allowFileTypeValidation:true,
server: {
timeout: 99999999,
revert: (uniqueFileId, load, error) => {
Expand Down Expand Up @@ -66,6 +68,7 @@ function load_app_filepond() {
extension: metadata.fileInfo.fileExtension,
prefix: prefix,
metadata: metadata,
accept: accept,
}),
})
.then(function (response) {
Expand Down Expand Up @@ -138,6 +141,16 @@ function load_app_filepond() {
});
};

filepondRequest.onreadystatechange = function (response) {
if (filepondRequest.status === 422) {
var jsonResponse = JSON.parse(filepondRequest.responseText);
notify({
type: 'error',
text: jsonResponse.message
});
}
};

filepondRequest.send(filepondFormData);
});
return {
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Controllers/Upload/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Diviky\Bright\Http\Controllers\Upload;

use Aws\S3\S3Client;
use Diviky\Bright\Rules\FileValidationRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
Expand Down Expand Up @@ -41,12 +42,13 @@ public function forLocal(Request $request): JsonResponse
'action' => $url,
'name' => $filename,
'extension' => $request->input('extension'),
'accept' => $request->input('accept'),
],
'inputs' => $request->input(),
], 201);
}

public function upload(Request $request): JsonResponse
public function upload(FileValidationRequest $request): JsonResponse
{
abort_unless($request->hasValidSignature(), 401);

Expand Down
27 changes: 27 additions & 0 deletions src/Rules/FileValidationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Diviky\Bright\Rules;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class FileValidationRequest extends FormRequest
{
public function rules(): array
{
return [
'file' => 'mimetypes:' . $this->input('accept'),
];
}

protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
'status' => 'ERROR',
'code' => 422,
'message' => $validator->errors()->first(),
'errors' => $validator->errors(),
], 422));
}
}

0 comments on commit b86f7a9

Please sign in to comment.