-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add endpoints for retrieving only upload info
- Loading branch information
Showing
6 changed files
with
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import express from 'express'; | ||
import multer from 'multer'; | ||
|
||
import { sendError } from '../lib/error/sendError'; | ||
import { getLimitMessage } from '../lib/misc/getLimitMessage'; | ||
import { getUploadLimits } from '../lib/misc/getUploadLimits'; | ||
import Settings from '../lib/parser/Settings'; | ||
import Workspace from '../lib/parser/WorkSpace'; | ||
import { UploadedFile } from '../lib/storage/types'; | ||
import GeneratePackagesUseCase from '../usecases/uploads/GeneratePackagesUseCase'; | ||
|
||
const getUploadHandler = (res: express.Response) => { | ||
const maxUploadCount = 21; | ||
return multer({ | ||
limits: getUploadLimits(res.locals.patreon), | ||
dest: process.env.UPLOAD_BASE, | ||
}).array('pakker', maxUploadCount); | ||
}; | ||
class SimpleUploadController { | ||
async handleUpload(req: express.Request, res: express.Response) { | ||
try { | ||
const settings = new Settings(req.body || {}); | ||
|
||
const useCase = new GeneratePackagesUseCase(); | ||
const { packages } = await useCase.execute( | ||
res.locals.patreon, | ||
req.files as UploadedFile[], | ||
settings | ||
); | ||
|
||
if (packages.length === 0) { | ||
return res.status(400).json({ | ||
error: 'no decks created', | ||
}); | ||
} | ||
|
||
const response: { name: string; link: string }[] = []; | ||
const workspace = new Workspace(true, 'fs'); | ||
const basePath = `/download/${workspace.id}`; | ||
for (const pkg of packages) { | ||
const p = path.join(workspace.location, pkg.name); | ||
fs.writeFileSync(p, pkg.apkg); | ||
response.push({ | ||
name: pkg.name, | ||
link: `${basePath}/${pkg.name}`, | ||
}); | ||
} | ||
|
||
return res.json(response); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
return res.json({ | ||
error: err.message, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
file(req: express.Request, res: express.Response) { | ||
try { | ||
console.info('uploading file'); | ||
const handleUploadEndpoint = getUploadHandler(res); | ||
|
||
handleUploadEndpoint(req, res, async (error) => { | ||
if (error) { | ||
let msg = error.message; | ||
if (msg === 'File too large') { | ||
msg = getLimitMessage(); | ||
} else { | ||
sendError(error); | ||
} | ||
return res.status(500).send(msg); | ||
} | ||
await this.handleUpload(req, res); | ||
}); | ||
} catch (error) { | ||
sendError(error); | ||
res.status(400); | ||
} | ||
} | ||
} | ||
|
||
export default SimpleUploadController; |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import express from 'express'; | ||
|
||
import SimpleUploadController from '../controllers/SimpleUploadController'; | ||
import RequireAllowedOrigin from './middleware/RequireAllowedOrigin'; | ||
|
||
const UploadRouter = () => { | ||
const router = express.Router(); | ||
const uploadController = new SimpleUploadController(); | ||
|
||
router.post('/api/simple-upload/file', RequireAllowedOrigin, (req, res) => | ||
uploadController.file(req, res) | ||
); | ||
|
||
return router; | ||
}; | ||
|
||
export default UploadRouter; |
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