Skip to content

Commit

Permalink
refactor: reduce duplication of getUploadHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Nov 24, 2023
1 parent bf377df commit ea426da
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
11 changes: 2 additions & 9 deletions src/controllers/SimpleUploadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@ 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);
};
import { getUploadHandler } from '../lib/misc/GetUploadHandler';

class SimpleUploadController {
async handleUpload(req: express.Request, res: express.Response) {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/UploadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import express from 'express';
import { getOwner } from '../lib/User/getOwner';
import { sendError } from '../lib/error/sendError';
import { getLimitMessage } from '../lib/misc/getLimitMessage';
import StorageHandler from '../lib/storage/StorageHandler';
import NotionService from '../services/NotionService';
import UploadService from '../services/UploadService';
import { getUploadHandler } from '../lib/misc/GetUploadHandler';

class UploadController {
constructor(
Expand Down Expand Up @@ -46,7 +46,7 @@ class UploadController {
file(req: express.Request, res: express.Response) {
try {
console.info('uploading file');
const handleUploadEndpoint = this.service.getUploadHandler(res);
const handleUploadEndpoint = getUploadHandler(res);

handleUploadEndpoint(req, res, async (error) => {
if (error) {
Expand Down
12 changes: 12 additions & 0 deletions src/lib/misc/GetUploadHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import express from 'express';
import multer from 'multer';
import { getUploadLimits } from './getUploadLimits';

export const getUploadHandler = (res: express.Response) => {
const maxUploadCount = 21;

return multer({
limits: getUploadLimits(res.locals.patreon),
dest: process.env.UPLOAD_BASE,
}).array('pakker', maxUploadCount);
};
8 changes: 0 additions & 8 deletions src/services/UploadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ class UploadService {
await s.delete(key);
}

getUploadHandler(res: express.Response) {
const maxUploadCount = 21;
return multer({
limits: getUploadLimits(res.locals.patreon),
dest: process.env.UPLOAD_BASE,
}).array('pakker', maxUploadCount);
}

async handleUpload(req: express.Request, res: express.Response) {
try {
let payload;
Expand Down

0 comments on commit ea426da

Please sign in to comment.