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 logics: createGroup, createCommand, retrieveCommand, deleteUse…
…rs, registerAdmin, uploadFile | adding testint (HTML & SPECS) | create handlers | modify Group and Files model b00tc4mp#361
- Loading branch information
Abel Prieto
authored and
Abel Prieto
committed
Mar 7, 2024
1 parent
d6bd3e3
commit 33f89ee
Showing
35 changed files
with
640 additions
and
124 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
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
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
55 changes: 55 additions & 0 deletions
55
staff/abel-prieto/PROYECT/API/handlers/uploadFileBBHandler.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,55 @@ | ||
import jwt from 'jsonwebtoken' | ||
import busboy from 'busboy' | ||
import fs from 'fs' | ||
import uploadFileBB from '../logic/uploadFileBB.js' | ||
import { errors } from 'com' | ||
|
||
const { NotFoundError, ContentError, TokenError } = errors | ||
const { JsonWebTokenError } = jwt | ||
|
||
export default (req, res) => { | ||
try { | ||
const token = req.headers.authorization.substring(7) | ||
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET) | ||
|
||
const bb = new busboy({ body: req.body }) | ||
|
||
bb.on('file', async (name, file, info) => { | ||
const { filename, mimeType } = info | ||
|
||
try { | ||
const saveFile = await uploadFileBB(userId, filename, mimeType) | ||
|
||
const writeStream = fs.createWriteStream(saveFile) | ||
file.pipe(writeStream) | ||
|
||
writeStream.on('close', () => { | ||
res.sendStatus(200) | ||
}) | ||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof NotFoundError) { | ||
status = 404 | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
}) | ||
|
||
req.pipe(bb) | ||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof TypeError || error instanceof ContentError) { | ||
status = 406 | ||
} | ||
|
||
if (error instanceof JsonWebTokenError) { | ||
status = 401 | ||
error = new TokenError(error.message) | ||
} | ||
|
||
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
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
Oops, something went wrong.