temp_file.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. /**
  3. *
  4. * 临时文件
  5. * @author Mai
  6. * @date 2026/4/21
  7. * @version
  8. */
  9. module.exports = app => {
  10. class TempFile extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'temp_file';
  20. }
  21. _analysisData(files) {
  22. const helper = this.ctx.helper;
  23. const userId = this.ctx.session.sessionUser.accountId;
  24. const ossPath = this.ctx.app.config.fujianOssPath;
  25. files.forEach(x => {
  26. x.viewpath = helper.canPreview(x.fileext) ? ossPath + x.filepath : '';
  27. x.filepath = ossPath + x.filepath;
  28. x.fileext_str = helper.fileExtStr(x.fileext);
  29. x.canEdit = x.user_id === userId;
  30. });
  31. }
  32. async getFiles(condition) {
  33. condition.orders = [['create_time', 'desc']];
  34. const result = await this.getAllDataByCondition(condition);
  35. this._analysisData(result);
  36. return result;
  37. }
  38. async addFiles(tenderId, fileInfo, user) {
  39. const conn = await this.db.beginTransaction();
  40. const result = {};
  41. try {
  42. const insertData = fileInfo.map(x => {
  43. return {
  44. id: this.uuid.v4(), tid: tenderId, rela_type: x.rela_type, rela_id: x.rela_id,
  45. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  46. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  47. };
  48. });
  49. await conn.insert(this.tableName, insertData);
  50. await conn.commit();
  51. result.files = { id: insertData.map(x => { return x.id; })};
  52. } catch (err) {
  53. await conn.rollback();
  54. throw err;
  55. }
  56. return await this.getFiles({ where: result.files });
  57. }
  58. async delFiles(files) {
  59. const fileDatas = await this.getAllDataByCondition({ where: { id: files } });
  60. const result = {};
  61. const conn = await this.db.beginTransaction();
  62. try {
  63. for (const f of fileDatas) {
  64. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + f.filepath);
  65. }
  66. await conn.delete(this.tableName, { id: files });
  67. await conn.commit();
  68. result.del = files;
  69. } catch (err) {
  70. await conn.rollback();
  71. throw err;
  72. }
  73. return result;
  74. }
  75. async clearFiles(days) {
  76. const fileDatas = await this.db.query(`SELECT * FROM ${this.tableName} WHERE create_time < DATE_SUB(CURDATE(), INTERVAL ${days} day)`);
  77. if (fileDatas.length === 0) return;
  78. for (const f of fileDatas) {
  79. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + f.filepath);
  80. await this.deleteById(f.id);
  81. }
  82. }
  83. }
  84. return TempFile;
  85. };