Skip to content

Commit

Permalink
feat: download page
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Nov 12, 2023
1 parent 43fe543 commit b8a626d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
47 changes: 46 additions & 1 deletion src/controllers/DownloadController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Request, Response } from 'express';
import fs from 'fs';

import { static as serve, NextFunction, Request, Response } from 'express';

import { sendError } from '../lib/error/sendError';
import DownloadService from '../services/DownloadService';
import StorageHandler from '../lib/storage/StorageHandler';
import path from 'path';
import { DownloadPage } from '../pages/DownloadPage';

class DownloadController {
constructor(private service: DownloadService) {}
Expand Down Expand Up @@ -34,6 +38,47 @@ class DownloadController {
}
}
}

getDownloadPage(req: Request, res: Response) {
const { id } = req.params;
const workspace = path.join(process.env.WORKSPACE_BASE!, id);

if (!fs.existsSync(workspace)) {
return res.status(404).end();
}

if (fs.statSync(workspace).isDirectory()) {
fs.readdir(workspace, (err, files) => {
if (err) {
console.error(err);
res.status(500).send('Error reading directory');
return;
}

const page = DownloadPage({ id, files });
res.send(page);
});
} else {
const fileContent = fs.readFileSync(workspace, 'utf8');
return res.send(fileContent);
}
}

getAPKGFile(req: Request, res: Response) {
const { id, apkg } = req.params;
const workspace = path.join(process.env.WORKSPACE_BASE!, id);
const apkgFilePath = path.join(workspace, apkg);

if (!fs.existsSync(apkgFilePath)) {
return res.status(404).end();
}

const fileContent = fs.readFileSync(apkgFilePath, 'utf8');
const contentLength = Buffer.byteLength(fileContent);
res.set('Content-Type', 'application/apkg');
res.set('Content-Length', contentLength.toString());
return res.send(fileContent);
}
}

export default DownloadController;
3 changes: 0 additions & 3 deletions src/controllers/UploadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class UploadController {
file(req: express.Request, res: express.Response) {
try {
console.info('uploading file');
console.time(req.path);
const storage = new StorageHandler();
const handleUploadEndpoint = this.service.getUploadHandler(res);

Expand All @@ -85,11 +84,9 @@ class UploadController {
} else {
sendError(error);
}
console.timeEnd(req.path);
return res.status(500).send(msg);
}
await this.service.handleUpload(storage, req, res);
console.timeEnd(req.path);
});
} catch (error) {
sendError(error);
Expand Down
28 changes: 28 additions & 0 deletions src/pages/DownloadPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from 'path';

import ReactDOMServer from 'react-dom/server';

interface DownloadPageProps {
id: string;
files: string[];
}

export const DownloadPage = ({ id, files }: DownloadPageProps) => {
return ReactDOMServer.renderToStaticMarkup(
<html>
<header>
<title>Your download</title>
</header>
<body>
<h1>Your download is ready</h1>
{files.map((file) => (
<li key={file}>
<a download={`${path.basename(file)}`} href={`${id}/${file}`}>
{file}
</a>
</li>
))}
</body>
</html>
);
};
8 changes: 8 additions & 0 deletions src/routes/DownloadRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const DownloadRouter = () => {
controller.getFile(req, res, storage);
});

router.get('/download/:id', (req, res) => {
controller.getDownloadPage(req, res);
});

router.get('/download/:id/:apkg', (req, res) => {
controller.getAPKGFile(req, res);
});

return router;
};

Expand Down

0 comments on commit b8a626d

Please sign in to comment.