Skip to content

Commit

Permalink
feat(#416): Update file validation logic and add files route to swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
tholulomo committed Jul 11, 2023
1 parent 9f95874 commit 246aab3
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 19 deletions.
261 changes: 252 additions & 9 deletions resfulservice/src/api-docs/swagger-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ paths:
description: Missing required input
content:
application/json:

schema:
$ref: '#/components/schemas/Missing-Input-Error'
'403':
Expand Down Expand Up @@ -487,6 +488,7 @@ paths:
type: string
example: An error occurred
delete:

security:
- BearerAuth: []
summary: Deletes a curation or list of curations
Expand All @@ -504,15 +506,18 @@ paths:
schema:
type: string
description: Dataset ID linked to multiple curations

responses:
'200':
description: Returns a success message for deleting curations
content:
application/json:
schema:

type: object
properties:
message:

type: string
enum: ['Curated sample ID: 6475c82ffbeb8571b77a5991 successfully deleted', 'Curated Samples with Dataset ID: 583e3d6ae74a1d205f4e3fd3 successfully deleted']

Expand Down Expand Up @@ -647,16 +652,12 @@ paths:
type: object
properties:
bulkCurations:
type: object
properties:
root:
$ref: '#/components/schemas/Successful-Curation'
'Li984jna9':
$ref: '#/components/schemas/Successful-Curation'
type: array
items:
$ref: '#/components/schemas/Successful-Curation'
bulkErrors:
type: object
example:
"Ls-94k-askd":
- "flename": "mm_files/bulk-curation-1688984487096/master_template (1).xlsx"
"real_permittivity.csv": "file not uploaded"
"loss_permittivity.csv": "file not uploaded"
"tan_delta.csv": "file not uploaded"
Expand All @@ -680,6 +681,15 @@ paths:
schema:
$ref: '#/components/schemas/Unauthorized-Error'

'404':
description: Dataset not found
content:
application/json:
schema:
type: object
example:
message: "Dataset ID: 583e3d6ae74a1d205f4e3fd3 not found"

'500':
description: Internal Server Error
content:
Expand All @@ -690,6 +700,217 @@ paths:
message:
type: string
example: An error occurred

/files/{fileId}:
get:
summary: Download files from the server
description: A route to download files by specifying a boolean isDirectory query params for files saved in the directory on the server or in database
tags:
- Files
parameters:
- in: query
name: isDirectory
schema:
type: string
example: true
description: a boolean value to specify if the file is stored in a directory or database
- in: path
required: true
name: fileId
description: the id of the file to download
schema:
type: string
example: 'mobile (15).png'
responses:
'200':
description: Returns the downloaded file type
content:
application/octet-stream:
schema:
type: string
format: binary

'400':
description: Bad user input
content:
application/json:
schema:
type: object
example:
success: false
message: validation error
data:
- value: trued
msg: only boolean value allowed
param: isDirectory
location: query
'500':
description: Internal Server Error
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: 'Error fetching file'

delete:
security:
- BearerAuth: []
summary: Deletes a file from the server
description: A route to delete a file by passing the fileId path params
tags:
- Files
parameters:
- in: path
required: true
name: fileId
description: the id of the file to delete
schema:
type: string
example: 'mobile (15).png'
responses:
'200':
description: OK

'400':
description: Bad user input
content:
application/json:
schema:
type: object
example:
success: false
message: validation error
data:
- value: 'mobile (15).pnge'
msg: Unsupported filetype
param: fileId
location: params

'401':
description: User not authorized for this service
content:
application/json:
schema:
$ref: '#/components/schemas/Unauthorized-Error'

'500':
description: Internal Server Error
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: 'Error deleting files'

/files/image_migration/{imageType}:
get:
summary: Image migration routes
description: A route to fetch images of specific types only
tags:
- Files
parameters:
- in: path
required: true
name: imageType
description: the type of images to fetch
schema:
type: string
example: 'tiff'
responses:
'200':
description: Returns the fetched image information
content:
application/json:
schema:
type: object
example:
images:
- fieldname: 'uploadfile'
originalname: '001.tif'
encoding: '7bit'
mimetype: 'image/tiff'
destination: 'mm_files'
filename: 'exciting_spoonbill_ermentrude-2023-05-05T11:25:53.451Z-001.tif'
path: 'mm_files/exciting_spoonbill_ermentrude-2023-05-05T11:25:53.451Z-001.tif'
size: 101196

'400':
description: Bad user input
content:
application/json:
schema:
type: object
example:
success: false
message: validation error
data:
- value: png
msg: only supports tiff & tif migration
param: imageType
location: params
'500':
description: Internal Server Error
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: 'Error fetching file'

/files/upload:
post:
summary: upload files to the server
description: A route to upload files to the server
tags:
- Files
requestBody:
description: An array selection of files to upload
required: true
content:
multipart/form-data:
schema:
type: object
properties:
uploadfile:
type: array
items:
type: string
format: binary
responses:
'201':
description: Returns the downloaded file type
content:
application/json:
schema:
$ref: '#/components/schemas/Sample-Files'

'400':
description: User uploads unsupported files
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: 'Only .png, .jpg, .jpeg, .tiff, .tif, .csv, .zip, .xls and .xlsx format allowed!'
'500':
description: Internal Server Error
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: Error uploading files
components:
securitySchemes:
BearerAuth:
Expand Down Expand Up @@ -724,6 +945,7 @@ components:
_id: "643931cc6f44b02f01380f7a"
displayName: "Test"
groupId: "583e3d6ae74a1d205f4e3fd3"
sampleID: "64394c8032bc6325505af6f9"
isApproved: false,
status: "Editing"

Expand All @@ -739,4 +961,25 @@ components:
Citation Type: lab-generated
Author: Aditya Shanker Prasad
URL: https://search.proquest.com/openview/eb63d4d6b84b1252971b3e3eec53b97c/1?pq-origsite=gscholar&cbl=51922&diss=y
Location: Rensselaer Polytechnic Institute
Location: Rensselaer Polytechnic Institute

Sample-Files:
type: object
example:
files:
- fieldname: 'uploadfile'
originalname: '001.tif'
encoding: '7bit'
mimetype: 'image/tiff'
destination: 'mm_files'
filename: 'exciting_spoonbill_ermentrude-2023-05-05T11:25:53.451Z-001.tif'
path: 'mm_files/exciting_spoonbill_ermentrude-2023-05-05T11:25:53.451Z-001.tif'
size: 101196
- fieldname: 'uploadfile'
originalname: 'master-template.xlsx'
encoding: '7bit'
mimetype: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
destination: 'mm_files'
filename: 'educational_hornet_maisie-2023-04-18T16:01:27.609Z-master-template.xlsx'
path: 'mm_files/educational_hornet_maisie-2023-04-18T16:01:27.609Z-master-template.xlsx'
size: 104928
12 changes: 9 additions & 3 deletions resfulservice/src/controllers/fileController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const mongoose = require('mongoose');
const { PassThrough } = require('stream');
const fsFiles = require('../models/fsFiles');
const latency = require('../middlewares/latencyTimer');
const { errorWriter, successWriter } = require('../utils/logWriter');
const { deleteFile, findFile } = require('../utils/fileManager');

Expand All @@ -19,6 +20,7 @@ exports.imageMigration = async (req, res, next) => {
.limit(10)
.toArray();
successWriter(req, { message: 'success' }, 'imageMigration');
latency.latencyCalculator(res);
return res.status(200).json({ images: files });
} catch (error) {
next(errorWriter(req, 'Error fetching image', 'imageMigration', 500));
Expand All @@ -32,9 +34,10 @@ exports.fileContent = async (req, res, next) => {

if (!fileStream) {
res.setHeader('Content-Type', 'image/png');
latency.latencyCalculator(res);
return _createEmptyStream().pipe(res);
}

latency.latencyCalculator(res);
return fileStream.pipe(res);
}

Expand All @@ -47,9 +50,11 @@ exports.fileContent = async (req, res, next) => {
const exist = await fsFiles.findById(_id).limit(1);
if (!exist) {
res.setHeader('Content-Type', 'image/png');
latency.latencyCalculator(res);
return _createEmptyStream().pipe(res);
}
const downloadStream = bucket.openDownloadStream(_id);
latency.latencyCalculator(res);
downloadStream.pipe(res);
} catch (error) {
next(errorWriter(req, 'Error fetching file', 'fileContent', 500));
Expand All @@ -61,9 +66,10 @@ exports.uploadFile = async (req, res, next) => {
req.logger.info('datasetIdUpload Function Entry:');

successWriter(req, { message: 'success' }, 'uploadFile');
latency.latencyCalculator(res);
return res.status(201).json({ files: req.files.uploadfile });
} catch (error) {
next(errorWriter(req, 'error uploading files', 'uploadFile', 500));
next(errorWriter(req, 'Error uploading files', 'uploadFile', 500));
}
};

Expand All @@ -73,7 +79,7 @@ exports.deleteFile = (req, res, next) => {
const filePath = `${filesDirectory}/${fileId}`;
try {
deleteFile(filePath, req);

latency.latencyCalculator(res);
return res.sendStatus(200);
} catch (err) {
next(errorWriter(req, 'Error deleting files', 'deleteFile', 500));
Expand Down
Loading

0 comments on commit 246aab3

Please sign in to comment.