- add file.controller.ts and File.ts for downloading files from server
- allow Get and Post form Oia/Index method
This commit is contained in:
parent
44688ac806
commit
83685b68ed
5 changed files with 133 additions and 23 deletions
54
src/controllers/file.controller.ts
Normal file
54
src/controllers/file.controller.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
import { Controller, Get } from "@overnightjs/core";
|
||||
import { File } from "../models/init-models.js";
|
||||
import { StatusCodes } from "http-status-codes";
|
||||
import { Request, Response } from "express";
|
||||
import * as path from "path";
|
||||
import * as fs from "fs";
|
||||
|
||||
@Controller("file")
|
||||
export class FileController {
|
||||
constructor() {}
|
||||
|
||||
@Get("download/:id")
|
||||
public async findOne(req: Request, res: Response) {
|
||||
const id = req.params.id;
|
||||
const file = await File.findOne({
|
||||
where: { id: id },
|
||||
});
|
||||
if (file) {
|
||||
let filePath = "/storage/app/public/" + file.path_name;
|
||||
const ext = path.extname(filePath);
|
||||
let fileName = file.label + ext;
|
||||
try {
|
||||
fs.accessSync(filePath, fs.constants.R_OK | fs.constants.W_OK);
|
||||
// console.log("can read/write:", path);
|
||||
res.set({
|
||||
"Cache-Control": "no-cache private",
|
||||
"Content-Description": "File Transfer",
|
||||
"Content-Type": file.mime_type,
|
||||
"Content-Disposition": "inline; filename=" + fileName,
|
||||
"Content-Transfer-Encoding": "binary",
|
||||
});
|
||||
res.status(StatusCodes.OK).sendFile(filePath);
|
||||
} catch (err) {
|
||||
// console.log("no access:", path);
|
||||
res.status(StatusCodes.NOT_FOUND).send({
|
||||
message: `File with id ${id} doesn't exist on file server`,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// res.status(StatusCodes.OK).sendFile(filePath, (err) => {
|
||||
// // res.setHeader("Content-Type", "application/json");
|
||||
// // res.removeHeader("Content-Disposition");
|
||||
// res.status(StatusCodes.NOT_FOUND).send({
|
||||
// message: `File with id ${id} doesn't exist on file server`,
|
||||
// });
|
||||
// });
|
||||
} else {
|
||||
res.status(StatusCodes.NOT_FOUND).send({
|
||||
message: `Cannot find File with id=${id}.`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
import { Controller, Get } from "@overnightjs/core";
|
||||
import { Controller, Get, Post } from "@overnightjs/core";
|
||||
import Sequelize from "sequelize";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { StatusCodes } from "http-status-codes";
|
||||
|
@ -55,6 +55,7 @@ export class OaiController {
|
|||
this.configuration = new Configuration();
|
||||
}
|
||||
|
||||
@Post("")
|
||||
@Get("")
|
||||
public async index(request: Request, response: Response, next: NextFunction) {
|
||||
this.xml = create(
|
||||
|
@ -87,7 +88,6 @@ export class OaiController {
|
|||
try {
|
||||
await this.handleRequest(oaiRequest, request);
|
||||
} catch (error) {
|
||||
// return next(error);
|
||||
if (error instanceof OaiModelException) {
|
||||
this.xsltParameter["oai_error_code"] = error.oaiCode;
|
||||
this.xsltParameter["oai_error_message"] = error.message;
|
||||
|
@ -151,7 +151,7 @@ export class OaiController {
|
|||
// $this->loadStyleSheet('datasetxml2oai-pmh.xslt');
|
||||
|
||||
// Set response time
|
||||
const now: dayjs.Dayjs = dayjs();
|
||||
const now: Dayjs = dayjs();
|
||||
this.xsltParameter["responseDate"] = now.format("YYYY-MM-DDThh:mm:ss[Z]");
|
||||
this.xsltParameter["unixTimestamp"] = now.unix();
|
||||
|
||||
|
@ -182,12 +182,8 @@ export class OaiController {
|
|||
this.handleIllegalVerb();
|
||||
}
|
||||
} else {
|
||||
// const err = new HttpException(404, 'Not Found')
|
||||
// next(err);
|
||||
|
||||
// try {
|
||||
// console.log("Async code example.")
|
||||
// const err = new HttpException(404, 'Not Found');
|
||||
const err = new PageNotFoundException("verb not found");
|
||||
throw err;
|
||||
// } catch (error) { // manually catching
|
||||
|
@ -363,21 +359,8 @@ export class OaiController {
|
|||
let totalIds = 0;
|
||||
let start = maxRecords + 1;
|
||||
let reldocIds: (number | null)[] = [];
|
||||
|
||||
let metadataPrefix = null;
|
||||
|
||||
// const tokenWorker = new TokenWorker(86400);
|
||||
// await tokenWorker.connect();
|
||||
// $tokenWorker->setResumptionPath($tokenTempPath);
|
||||
// const url = process.env.REDIS_URL || "redis://redis:6379";
|
||||
// const redisClient = createClient({
|
||||
// url
|
||||
// });
|
||||
// redisClient.on('error', (error) => {
|
||||
// const err = new InternalServerErrorException("Error occured while connecting or accessing redis server'");
|
||||
// throw err;
|
||||
// });
|
||||
|
||||
// resumptionToken is defined
|
||||
if ("resumptionToken" in oaiRequest) {
|
||||
const resParam = oaiRequest["resumptionToken"]; //e.g. "158886496600000"
|
||||
|
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue