will-core
Version:
core module
142 lines (141 loc) • 6.63 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AttachHandler = void 0;
const uuid_1 = require("uuid");
const will_sql_1 = require("will-sql");
const will_api_1 = require("will-api");
const VerifyError_1 = require("../models/VerifyError");
const SchemeHandler_1 = require("./SchemeHandler");
const fs_1 = __importDefault(require("fs"));
class AttachHandler extends SchemeHandler_1.SchemeHandler {
constructor() {
super(...arguments);
this.model = { name: "tattachfile", alias: { privateAlias: this.section } };
//declared addon actions name
this.handlers = [{ name: "get" }, { name: "attach" }];
}
async get(context) {
return this.callFunctional(context, { operate: "get", raw: false }, this.doGet);
}
async doGet(context, model) {
let db = this.getPrivateConnector(model);
try {
let attachid = context.params.id;
this.logger.debug(this.constructor.name + ".doGet: id=" + attachid);
if (!attachid || attachid.trim().length == 0) {
return Promise.reject(new VerifyError_1.VerifyError("Attach id is undefined", will_api_1.HTTP.NOT_ACCEPTABLE, -16010));
}
let sql = new will_sql_1.KnSQL("select * from tattachfile ");
sql.append("where attachid = ?attachid ");
sql.set("attachid", attachid);
this.logger.info(this.constructor.name + ".doGet", sql);
let rs = await sql.executeQuery(db);
return this.createRecordSet(rs);
}
catch (ex) {
this.logger.error(this.constructor.name, ex);
return Promise.reject(this.getDBError(ex));
}
finally {
if (db)
db.close();
}
}
async attach(context) {
return this.callFunctional(context, { operate: "attach", raw: false }, this.doAttach);
}
async doAttach(context, model) {
let file = context.params.file;
if (!file || !file.filename || !file.originalname) {
return Promise.reject(new VerifyError_1.VerifyError("No attachment found", will_api_1.HTTP.NOT_ACCEPTABLE, -16091));
}
let stream = undefined;
let existing = fs_1.default.existsSync(file.path);
if (existing) {
let buffer = fs_1.default.readFileSync(file.path, { flag: 'r' });
stream = buffer.toString("base64");
}
let db = this.getPrivateConnector(model);
try {
let attachid = context.params.id;
let attachtype = context.params.type;
let attachno = context.params.no;
this.logger.debug(this.constructor.name + ".doAttach: id=" + attachid + ", type=" + attachtype + ", no=" + attachno);
if (!attachid || attachid.trim().length == 0) {
attachid = context.params.fileid;
}
if (!attachid || attachid.trim().length == 0) {
attachid = (0, uuid_1.v4)();
}
let ut = await this.getUserTokenInfo(context, true);
let attachuser = ut?.userid;
if (!attachuser)
attachuser = this.getCurrentUser();
let now = new Date();
let sql = new will_sql_1.KnSQL("update tattachfile ");
sql.append("set attachfile = ?attachfile , ");
sql.append("sourcefile = ?sourcefile , ");
sql.append("attachdate = ?attachdate , ");
sql.append("attachtime = ?attachtime , ");
sql.append("attachmillis = ?attachmillis ");
if (attachtype) {
sql.append(", attachtype = ?attachtype ");
sql.set("attachtype", attachtype);
}
if (attachno) {
sql.append(", attachno = ?attachno ");
sql.set("attachno", attachno);
}
if (attachuser) {
sql.append(", attachuser = ?attachuser ");
sql.set("attachuser", attachuser);
}
sql.append(", attachpath = ?attachpath , attachstream = ?attachstream ");
sql.append("where attachid = ?attachid ");
sql.set("attachfile", file.filename);
sql.set("sourcefile", file.originalname);
sql.set("attachdate", now, "DATE");
sql.set("attachtime", now, "TIME");
sql.set("attachmillis", now.getTime());
sql.set("attachpath", file.path);
sql.set("attachstream", stream);
sql.set("attachid", attachid);
let rs = await sql.executeUpdate(db, context);
if (rs.rows.affectedRows == 0) {
if (!attachno || attachno.trim().length == 0)
attachno = attachid;
if (!attachtype || attachtype.trim().length == 0)
attachtype = "NONE";
sql.clear();
sql.append("insert into tattachfile (attachid,attachno,attachtype,attachfile,sourcefile,attachdate,attachtime,attachmillis,attachuser,attachpath,attachstream) ");
sql.append("values(?attachid,?attachno,?attachtype,?attachfile,?sourcefile,?attachdate,?attachtime,?attachmillis,?attachuser,?attachpath,?attachstream) ");
sql.set("attachid", attachid);
sql.set("attachno", attachno);
sql.set("attachtype", attachtype);
sql.set("attachfile", file.filename);
sql.set("sourcefile", file.originalname);
sql.set("attachdate", now, "DATE");
sql.set("attachtime", now, "TIME");
sql.set("attachmillis", now.getTime());
sql.set("attachuser", attachuser);
sql.set("attachpath", file.path);
sql.set("attachstream", stream);
rs = await sql.executeUpdate(db, context);
}
rs.rows = { ...rs.rows, attachid: attachid, attachno: attachno, attachtype: attachtype };
return this.createRecordSet(rs);
}
catch (ex) {
this.logger.error(this.constructor.name, ex);
return Promise.reject(this.getDBError(ex));
}
finally {
if (db)
db.close();
}
}
}
exports.AttachHandler = AttachHandler;