Skip to content

Commit

Permalink
Better response for uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
BafS committed Feb 19, 2016
1 parent f104636 commit 1380226
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Parvula/routes/api/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@
* @apiGroup Files
* @apiDescription Upload file(s) via multipart data upload
*
* @apiSuccess (204) FileUploaded File uploaded
* @apiSuccess (201) FileUploaded File uploaded
* @apiError (400) NoFileSent No file was sent
* @apiError (400) FileSizeExceeded Exceeded file size limit
* @apiError (400) FileNameError Exceeded file name limit
* @apiError (500) InternalError
* @apiError (500) UploadException
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 201 OK
* {
* "filename": "supercat.png",
* "directory": "static\/files"
* }
*/
$router->post('/upload', function ($req, $res) use ($app, $fs) {
$config = $app['config'];
Expand Down Expand Up @@ -114,9 +121,10 @@
}

// Name should be unique // TODO
$filename = $basename . '.' . $ext;
if (!move_uploaded_file(
$file['tmp_name'],
sprintf('%s/%s.%s', _UPLOADS_, $basename, $ext)
_UPLOADS_ . $filename
)) {
throw new RuntimeException('Failed to move uploaded file');
}
Expand All @@ -128,7 +136,10 @@
]);
}

return $res->sendStatus(204);
return $res->status(201)->send([
'filename' => $filename,
'directory' => _UPLOADS_
]);
});

/**
Expand Down

0 comments on commit 1380226

Please sign in to comment.