PHP Classes

File: resources/js/upload-config.js

Recommend this page to a friend!
  Packages of Boss Ibrahim Mussa   Caelrik   resources/js/upload-config.js   Download  
File: resources/js/upload-config.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Caelrik
Build Web user interface from PHP components
Author: By
Last change:
Date: 8 days ago
Size: 1,736 bytes
 

 

Contents

Class file image Download
export const CSRF_HEADER = 'X-CSRF-TOKEN'; export const MESSAGES = { required: 'Please add a file.', pending: 'Please wait until the upload is finished.', failed: 'Remove the files that could not be uploaded.', }; // FilePond options for one file field, from the PHP data-caelrik-upload options. export function buildFilePondConfig(input, options, csrfToken) { const headers = csrfToken ? { [CSRF_HEADER]: csrfToken } : {}; const maxFiles = options.maxFiles ?? 1; return { name: input.name, allowMultiple: maxFiles > 1, maxFiles, acceptedFileTypes: options.types ?? [], fileSizeBase: 1024, maxFileSize: `${Math.floor((options.maxSize ?? 5 * 1024 * 1024) / 1024)}KB`, credits: false, server: { process: { url: options.url, method: 'POST', headers }, revert: { url: options.url, method: 'DELETE', headers }, restore: null, load: null, fetch: null, }, ...options.filepond, }; } // Why the form must not be submitted yet, or null. `files` are normalized FilePond // items: {done: uploaded and accepted, pending: still uploading, failed: rejected}. export function uploadProblem(files, options = {}) { if (files.some((file) => file.pending)) { return MESSAGES.pending; } if (files.some((file) => file.failed)) { return MESSAGES.failed; } if (options.required && !files.some((file) => file.done)) { return MESSAGES.required; } return null; } export function csrfTokenFor(form, formOptions) { const field = formOptions?.csrf?.field; if (!field) { return null; } for (const input of form.querySelectorAll('input[type="hidden"]')) { if (input.name === field) { return input.value; } } return null; }