forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create uploadFile middleware & endpoint, with Multer and diskStorage b…
- Loading branch information
Showing
9 changed files
with
202 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
staff/abel-prieto/PROYECT/API/handlers/uploadFileHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import fs from 'fs' | ||
import uploadFile from '../logic/uploadFile.js' | ||
import { errors } from 'com' | ||
const { NotFoundError, ContentError } = errors | ||
|
||
function saveFile(file) { | ||
const newPath = `./uploads/${file.originalname}` | ||
fs.renameSync(file.path, newPath) | ||
|
||
return | ||
} | ||
|
||
export default (req, res) => { | ||
const userId = req.headers.authorization.substring(7) | ||
const { originalname: name, mimetype: type } = req.file | ||
|
||
saveFile(req.file) | ||
|
||
try { | ||
uploadFile(userId, name, type) | ||
.then(({ user, file }) => res.json({ user, file, message: 'Upload file!' })) | ||
.catch(error => { | ||
let status = 500 | ||
|
||
if (error instanceof NotFoundError) { | ||
status = 404 | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
|
||
return | ||
}) | ||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) { | ||
status = 406 | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { User } from "../data/models.js" | ||
import { errors } from "com" | ||
const { SystemError, NotFoundError} = errors | ||
const { SystemError, NotFoundError } = errors | ||
|
||
function retrieveGuest() { | ||
return User.findOne({ email: '[email protected]' }).lean() | ||
.catch(error => { throw new SystemError(error.message)}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(guest => { | ||
if (!guest) { | ||
throw new NotFoundError('Guest not found. Try again') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { User, File } from '../data/models.js' | ||
import { errors } from 'com' | ||
const { SystemError, NotFoundError } = errors | ||
|
||
async function uploadFile(userId, name, type) { | ||
try { | ||
const user = await User.findById(userId).lean() | ||
|
||
if (!user) { | ||
throw new NotFoundError('User not found. Try again') | ||
} | ||
|
||
// const { username, email, password, group, __v, role, ...cleanedUser } = user | ||
|
||
delete user.__v | ||
delete user.username | ||
delete user.email | ||
delete user.password | ||
delete user.group | ||
delete user.role | ||
|
||
const file = await File.create({ name: name, owner: userId, type: type, permissions: 3 }) | ||
|
||
// const { _id, owner, permissions, __v, ...cleanedFile } = file | ||
|
||
delete file._id | ||
delete file.owner | ||
delete file.permissions | ||
delete file.__v | ||
|
||
return { user, file } | ||
// return { user: cleanedUser, file: cleanedFile } | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
} | ||
|
||
export default uploadFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.