Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
upload file using multipart/form-data
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
wellingguzman committed Apr 6, 2018
1 parent 23afa55 commit f654c30
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/core/Directus/Filesystem/Exception/FailedUploadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Directus\Filesystem\Exception;

class FailedUploadException extends FilesystemException
{
protected $uploadedError = 0;

public function __construct($errorCode)
{
$this->uploadedError = $errorCode;
parent::__construct(get_uploaded_file_error($errorCode));
}

public function getErrorCode()
{
return $this->uploadedError;
}
}
15 changes: 15 additions & 0 deletions src/core/Directus/Filesystem/Exception/FilesystemException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Directus\Filesystem\Exception;

use Directus\Exception\Exception;

class FilesystemException extends Exception
{
const ERROR_CODE = 300;

public function __construct($message = '')
{
parent::__construct($message, static::ERROR_CODE);
}
}
1 change: 1 addition & 0 deletions src/core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require __DIR__ . '/constants.php';
require __DIR__ . '/helpers/arrays.php';
require __DIR__ . '/helpers/extensions.php';
require __DIR__ . '/helpers/file.php';
require __DIR__ . '/helpers/items.php';
require __DIR__ . '/helpers/mail.php';
require __DIR__ . '/helpers/sorting.php';
Expand Down
60 changes: 60 additions & 0 deletions src/core/helpers/file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

if (!function_exists('is_uploaded_file_okay')) {
/**
* Checks whether upload file has not error.
*
* @param $error
*
* @return bool
*/
function is_uploaded_file_okay($error)
{
return $error === UPLOAD_ERR_OK;
}
}

if (!function_exists('get_uploaded_file_error')) {
/**
* Returns the upload file error message
*
* Returns null if there's not error
*
* @param $error
*
* @return string|null
*/
function get_uploaded_file_error($error)
{
switch ($error) {
case UPLOAD_ERR_INI_SIZE:
$message = 'The uploaded file exceeds max upload size that was specified on the server.';
break;
case UPLOAD_ERR_FORM_SIZE:
$message = 'The uploaded file exceeds the max upload size that was specified in the client.';
break;
case UPLOAD_ERR_PARTIAL:
$message = 'The uploaded file was only partially uploaded.';
break;
case UPLOAD_ERR_NO_FILE:
$message = 'No file was uploaded.';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$message = 'Missing temporary upload folder';
break;
case UPLOAD_ERR_CANT_WRITE:
$message = 'Failed to write file to disk.';
break;
case UPLOAD_ERR_EXTENSION:
$message = 'A PHP extension stopped the file upload';
break;
case UPLOAD_ERR_OK:
$message = null;
break;
default:
$message = 'Unknown error uploading a file.';
}

return $message;
}
}
30 changes: 29 additions & 1 deletion src/endpoints/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
use Directus\Application\Http\Request;
use Directus\Application\Http\Response;
use Directus\Application\Route;
use Directus\Exception\Exception;
use Directus\Filesystem\Exception\FailedUploadException;
use Directus\Services\FilesServices;
use Directus\Util\ArrayUtils;
use Slim\Http\UploadedFile;

class Files extends Route
{
Expand Down Expand Up @@ -38,12 +41,37 @@ public function __invoke(Application $app)
* @param Response $response
*
* @return Response
*
* @throws Exception
*/
public function create(Request $request, Response $response)
{
$service = new FilesServices($this->container);
$uploadedFiles = $request->getUploadedFiles();
$payload = $request->getParsedBody();

if (!empty($uploadedFiles)) {
/** @var UploadedFile $uploadedFile */
$uploadedFile = array_shift($uploadedFiles);
if (!is_uploaded_file_okay($uploadedFile->getError())) {
throw new FailedUploadException($uploadedFile->getError());
}

if (empty($payload)) {
$payload = [];
}

// TODO: the file already exists move it to the upload path location
$data = file_get_contents($uploadedFile->file);
$payload = array_merge([
'filename' => $uploadedFile->getClientFilename(),
'type' => $uploadedFile->getClientMediaType(),
'data' => base64_encode($data)
], $payload);
}

$responseData = $service->create(
$request->getParsedBody(),
$payload,
$request->getQueryParams()
);

Expand Down

0 comments on commit f654c30

Please sign in to comment.