Skip to content

Commit

Permalink
feat: add endpoints for retrieving only upload info
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Nov 25, 2023
1 parent 7378593 commit 759d60a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 10 deletions.
86 changes: 86 additions & 0 deletions src/controllers/SimpleUploadController.ts
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;
3 changes: 1 addition & 2 deletions src/controllers/UploadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class UploadController {
file(req: express.Request, res: express.Response) {
try {
console.info('uploading file');
const storage = new StorageHandler();
const handleUploadEndpoint = this.service.getUploadHandler(res);

handleUploadEndpoint(req, res, async (error) => {
Expand All @@ -59,7 +58,7 @@ class UploadController {
}
return res.status(500).send(msg);
}
await this.service.handleUpload(storage, req, res);
await this.service.handleUpload(req, res);
});
} catch (error) {
sendError(error);
Expand Down
17 changes: 17 additions & 0 deletions src/routes/SimpleUploadRouter.ts
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;
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import downloadRouter from './routes/DownloadRouter';
import favoriteRouter from './routes/FavoriteRouter';
import templatesRouter from './routes/TemplatesRouter';
import defaultRouter from './routes/DefaultRouter';
import simpleUploadRouter from './routes/SimpleUploadRouter';

import { sendError } from './lib/error/sendError';

Expand Down Expand Up @@ -71,6 +72,7 @@ const serve = async () => {
app.use(downloadRouter());
app.use(favoriteRouter());
app.use(templatesRouter());
app.use(simpleUploadRouter());

// Note: this has to be the last router
app.use(defaultRouter());
Expand Down
8 changes: 2 additions & 6 deletions src/services/UploadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@ class UploadService {
}).array('pakker', maxUploadCount);
}

async handleUpload(
storage: StorageHandler,
req: express.Request,
res: express.Response
) {
async handleUpload(req: express.Request, res: express.Response) {
try {
let payload;
let plen;
const settings = new Settings(req.body || {});

const useCase = new GeneratePackagesUseCase(storage);
const useCase = new GeneratePackagesUseCase();
const { packages } = await useCase.execute(
res.locals.patreon,
req.files as UploadedFile[],
Expand Down
2 changes: 0 additions & 2 deletions src/usecases/uploads/GeneratePackagesUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ const getPackagesFromZip = async (
};

class GeneratePackagesUseCase {
constructor(private readonly storage: StorageHandler) {}

async execute(
isPatreon: boolean,
files: UploadedFile[],
Expand Down

0 comments on commit 759d60a

Please sign in to comment.