You are here

public function TinyMCEController::upload in TinyMCE 1.x

Upload file retrieved through a POST request on the route.

Parameters

Request $request:

Return value

JsonResponse

1 string reference to 'TinyMCEController::upload'
tinymce.routing.yml in ./tinymce.routing.yml
tinymce.routing.yml

File

src/Controller/TinyMCEController.php, line 21

Class

TinyMCEController
Implementing our example JSON api.

Namespace

Drupal\tinymce\Controller

Code

public function upload(Request $request) : JsonResponse {

  // Check that we retrieve a multipart/form-data element or return an error.
  if (strpos($request->headers
    ->get('Content-Type'), 'multipart/form-data;') !== 0) {
    $res = new JsonResponse();
    $res
      ->setStatusCode(400, 'Please submit multipart/form-data');
    return $res;
  }

  // Retrieve file content.
  reset($_FILES);
  $tmpFile = current($_FILES);
  $data = file_get_contents($tmpFile['tmp_name']);

  // Prepare saving.
  $destinationFolder = 'public://tinymce/';
  $destinationFile = $destinationFolder . $tmpFile['name'];

  // Attempt to save the file and the folder if it does not exist.
  if (\Drupal::service('file_system')
    ->prepareDirectory($destinationFolder, FileSystemInterface::CREATE_DIRECTORY)) {
    $file = file_save_data($data, $destinationFile, FILE_EXISTS_REPLACE);
  }

  // Prepare the json output for the editor.
  $path = file_create_url($destinationFile);
  $response['location'] = $path;
  return new JsonResponse($response);
}