Skip to content

Commit

Permalink
Merge pull request #141 from ConductionNL/feature/DIMOC-178/publicati…
Browse files Browse the repository at this point in the history
…on-download

Make Uploaded _file and downloadURL mutually exclusive
  • Loading branch information
WilcoLouwerse authored Aug 9, 2024
2 parents c8f158b + 7d379ed commit db82028
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions lib/Controller/AttachmentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private function checkUploadedFile(): JSONResponse|array
$uploadedFile = $this->request->getUploadedFile(key: '_file');

if (empty($uploadedFile) === true) {
return new JSONResponse(data: ['error' => 'No file uploaded for key "_file"'], statusCode: 400);
return new JSONResponse(data: ['error' => 'Please upload a file using key "_file" or give a "downloadUrl"'], statusCode: 400);
}

// Check for upload errors
Expand All @@ -173,14 +173,14 @@ private function checkUploadedFile(): JSONResponse|array
}

/**
* Gets all params from the request body and then validates if the URL fields are actual valid urls (or null).
* Validates if the URL fields are actual valid urls (or null).
*
* @param array $data The form-data fields and their values (/request body).
*
* @return JSONResponse|array An error response if there are validation errors or an array containing all request body params.
*/
private function checkRequestBody(): JSONResponse|array
private function checkRequestBody(array $data): JSONResponse|array
{
$data = $this->request->getParams();

$errorMsg = [];
if (empty($data['accessUrl']) === false && filter_var(value: $data['accessUrl'], filter: FILTER_VALIDATE_URL) === false) {
$errorMsg[] = "accessUrl is not a valid url";
Expand Down Expand Up @@ -231,7 +231,7 @@ private function handleFile(array $uploadedFile): JSONResponse|string


/**
* Adds information about the uploaded file to the appropriate Attachment fields. And removes fields we do not want to post.
* Adds information about the uploaded file to the appropriate Attachment fields.
*
* @param array $data The form-data fields and their values (/request body) that we are going to update before posting the Attachment.
* @param array $uploadedFile Information about the uploaded file from the request body.
Expand All @@ -257,14 +257,8 @@ private function AddFileInfoToData(array $data, array $uploadedFile, string $fil
if (empty($data['accessUrl']) === true) {
$data['accessUrl'] = $shareLink;
}
$data['downloadUrl'] = "$shareLink/download";

// Remove fields we should never post
unset($data['id']);
foreach($data as $key => $value) {
if(str_starts_with(haystack: $key, needle: '_')) {
unset($data[$key]);
}
if (empty($data['downloadUrl']) === true) {
$data['downloadUrl'] = "$shareLink/download";
}

return $data;
Expand All @@ -279,26 +273,40 @@ private function AddFileInfoToData(array $data, array $uploadedFile, string $fil
*/
public function create(ObjectService $objectService, ElasticSearchService $elasticSearchService): JSONResponse
{
// Check if a file was uploaded
$uploadedFile = $this->checkUploadedFile();
if ($uploadedFile instanceof JSONResponse) {
return $uploadedFile;
$data = $this->request->getParams();
// Uploaded _file and downloadURL are mutually exclusive
if (empty($data['downloadUrl']) === true) {
// Check if a file was uploaded
$uploadedFile = $this->checkUploadedFile();
if ($uploadedFile instanceof JSONResponse) {
return $uploadedFile;
}
}

// Get form-data field/request body.
$data = $this->checkRequestBody();
// Get form-data field/request body and validate the input.
$data = $this->checkRequestBody($data);
if ($data instanceof JSONResponse) {
return $data;
}

// Handle saving the uploaded file in NextCloud
$filePath = $this->handleFile(uploadedFile: $uploadedFile);
if ($filePath instanceof JSONResponse) {
return $filePath;
if (empty($uploadedFile) === false) {
// Handle saving the uploaded file in NextCloud
$filePath = $this->handleFile(uploadedFile: $uploadedFile);
if ($filePath instanceof JSONResponse) {
return $filePath;
}

// Update Attachment data
$data = $this->AddFileInfoToData(data: $data, uploadedFile: $uploadedFile, filePath: $filePath);
}

// Update Attachment data
$data = $this->AddFileInfoToData(data: $data, uploadedFile: $uploadedFile, filePath: $filePath);
// Remove fields we should never post
unset($data['id']);
foreach($data as $key => $value) {
if(str_starts_with(haystack: $key, needle: '_')) {
unset($data[$key]);
}
}

if ($this->config->hasKey(app: $this->appName, key: 'mongoStorage') === false
|| $this->config->getValueString(app: $this->appName, key: 'mongoStorage') !== '1'
Expand Down

0 comments on commit db82028

Please sign in to comment.