Commit 8992c19e authored by Primsi's avatar Primsi

by primsi: addressing review stuff.

parent b44c8b50
......@@ -2,6 +2,6 @@ name: dropzonejs
type: module
description: DropzoneJS
core: 8.x
package: Other
package: Media
dependencies:
- file
dropzonejs:
version: VERSION
title: 'Dropzonejs'
website: http://www.dropzonejs.com
version: 4.0.1
license:
name: BSD
url: https://github.com/yui/yui3/blob/master/LICENSE.md
gpl-compatible: true
js:
# @todo add option to include uncompressed
assets/dropzone/dist/min/dropzone.min.js: {}
js/dropzone.integration.js: {}
/libraries/dropzone/dist/min/dropzone.min.js: {}
css:
component:
# @todo add option to include uncompressed
assets/dropzone/dist/min/dropzone.min.css: {}
/libraries/dropzone/dist/min/dropzone.min.css: {}
integration:
version: VERSION
js:
js/dropzone.integration.js: {}
......@@ -2,6 +2,9 @@
* @file dropzone.integration.js
*
* Defines the behaviors needed for dropzonejs integration.
*
* @todo Implement maxfilesexceeded.
*
*/
(function ($, Drupal, drupalSettings) {
"use strict";
......@@ -13,19 +16,18 @@
selector.addClass("dropzone");
// Initiate dropzonejs.
var dropzoneInstance = new Dropzone("#" + selector.attr("id"), {
// @todo we should get all this from somewhere.
url: "/drupal-8-media/dropzonejs/upload",
maxFilesize: 2,
var config = {
url: drupalSettings.dropzonejs.upload_path,
addRemoveLinks: true,
});
};
var instanceConfig = drupalSettings.dropzonejs.instances[selector.attr('id')];
var dropzoneInstance = new Dropzone("#" + selector.attr("id"), $.extend({}, instanceConfig, config));
// React on add file. Add only accepted files.
dropzoneInstance.on("addedfile", function(file) {
var uploadedFilesElement = selector.siblings(':hidden');
var currentValue = uploadedFilesElement.attr('value');
// @todo handle files with validaiton errors.
uploadedFilesElement.attr('value', currentValue + file.name + ';');
});
......
......@@ -7,6 +7,7 @@
namespace Drupal\dropzonejs\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\dropzonejs\UploadException;
......@@ -15,6 +16,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
* Handles requests that dropzone issues when uploading files.
......@@ -52,17 +54,23 @@ class UploadController extends ControllerBase {
*
* @param \Symfony\Component\HttpFoundation\Request $request
* Request object.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
* Config factory.
*/
public function __construct(Request $request) {
public function __construct(Request $request, ConfigFactoryInterface $config) {
$this->request = $request;
$this->temporaryUploadLocation = \Drupal::config('system.file')->get('path.temporary');
$tmp_override = $config->get('dropzonejs.settings')->get('tmp_dir');
$this->temporaryUploadLocation = ($tmp_override) ? $tmp_override : $config->get('system.file')->get('path.temporary');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('request_stack')->getCurrentRequest());
return new static(
$container->get('request_stack')->getCurrentRequest(),
$container->get('config.factory')
);
}
/**
......@@ -77,17 +85,6 @@ class UploadController extends ControllerBase {
catch (UploadException $e) {
return $e->getErrorResponse();
}
// Return JSON-RPC response.
// @todo Not sure if we need this.
return new JsonResponse(
array(
'jsonrpc' => '2.0',
'result' => NULL,
'id' => 'id',
),
200
);
}
/**
......@@ -118,7 +115,6 @@ class UploadController extends ControllerBase {
*/
protected function getFilename(UploadedFile $file) {
if (empty($this->filename)) {
// @todo I am not sure why plupload gets 'name' here ($this->request->request->get('name')).
$this->filename = $file->getClientOriginalName();
// Check the file name for security reasons; it must contain letters,
......@@ -135,12 +131,13 @@ class UploadController extends ControllerBase {
* Handles multipart uploads.
*
* @throws \Drupal\dropzonejs\UploadException
* @throws Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*/
protected function handleUpload() {
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = $this->request->files->get('file');
if (!$file instanceof UploadedFile) {
throw new UploadException(UploadException::REQUEST_ERROR);
throw new AccessDeniedHttpException();
}
elseif ($file->getError() != UPLOAD_ERR_OK) {
throw new UploadException(UploadException::FILE_UPLOAD_ERROR);
......@@ -148,7 +145,6 @@ class UploadController extends ControllerBase {
// Open temp file.
$tmp = $this->temporaryUploadLocation . $this->getFilename($file);
// @todo Is the 'b' at the end of the mode ok?
if (!($out = fopen("{$this->temporaryUploadLocation}/{$this->getFilename($file)}", $this->request->request->get('chunk', 0) ? 'ab' : 'wb'))) {
throw new UploadException(UploadException::OUTPUT_ERROR);
}
......@@ -165,10 +161,8 @@ class UploadController extends ControllerBase {
}
// Be nice and keep everything nice and clean.
// @todo when implementing multipart dont forget to drupal_unlink.
fclose($in);
fclose($out);
if ($is_multipart) {
drupal_unlink($multipart_file['tmp_name']);
}
}
}
......@@ -14,6 +14,18 @@ use Drupal\Core\Render\Element\FormElement;
/**
* Provides a DropzoneJS atop of the file element.
*
* Configuration options are:
* - #title
* The main field title.
* - #description
* Description under the field.
* - #dropzone_description
* Will be visible inside the upload area.
* - #max_filesize
* Used by dropzonejs and expressed in MB. See
* http://www.dropzonejs.com/#config-maxFilesize
*
*
* @todo Remove updated_files from the values array.
*
* @FormElement("dropzonejs")
......@@ -32,7 +44,9 @@ class DropzoneJs extends FormElement {
'#pre_render' => [[$class, 'preRenderDropzoneJs']],
'#theme' => 'dropzonejs',
'#theme_wrappers' => ['form_element'],
'#attached' => ['library' => ['dropzonejs/dropzonejs']],
'#attached' => [
'library' => ['dropzonejs/dropzonejs', 'dropzonejs/integration']
],
];
}
......@@ -54,13 +68,23 @@ class DropzoneJs extends FormElement {
*
* @param array $element
* An associative array containing the properties of the element.
* Properties used: #title, #name, #size, #description, #required,
* #attributes.
* Properties used: #title, #description, #required, #attributes,
* #dropzone_description, #max_filesize.
*
* @return array
* The $element with prepared variables ready for input.html.twig.
*/
public static function preRenderDropzoneJs($element) {
$element['#attached']['drupalSettings']['dropzonejs'] = [
'upload_path' => base_path() . 'dropzonejs/upload',
'instances' => [
$element['#id'] => [
'maxFilesize' => $element['#max_filesize'],
'dictDefaultMessage' => $element['#dropzone_description']
],
],
];
static::setAttributes($element, ['dropzone-enable']);
return $element;
}
......
......@@ -36,15 +36,10 @@ class UploadExceptionn extends \Exception {
*/
const FILENAME_ERROR = 105;
/**
* Error with temporary file name.
*/
const REQUEST_ERROR = 106;
/**
* File upload resulted in error.
*/
const FILE_UPLOAD_ERROR = 107;
const FILE_UPLOAD_ERROR = 106;
/**
* Code to error message mapping.
......@@ -57,7 +52,6 @@ class UploadExceptionn extends \Exception {
self::MOVE_ERROR => 'Failed to move uploaded file.',
self::DESTINATION_FOLDER_ERROR => 'Failed to open temporary directory.',
self::FILENAME_ERROR => 'Invalid temporary file name.',
self::REQUEST_ERROR => 'The request does not contain a UploadedFile object.',
self::FILE_UPLOAD_ERROR => 'The file upload resulted in an error on php level. See http://php.net/manual/en/features.file-upload.errors.php',
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment