'use strict'; const request = require('superagent'); const moment = require('moment'); async function createProjectDir(ctx, next) { let error = { message: '新增项目目录失败' }, rs = null; const { roadName } = ctx.query; try { const models = ctx.fs.dc.models; rs = await models.FileRoad.create({ roadName }) error = null; } catch (err) { ctx.status = 500; ctx.body = { detail: err, ...error }; } if (error) { ctx.status = 400; ctx.body = { ...error }; } else { ctx.status = 200; ctx.body = { message: '新增项目目录成功!', result: rs }; } } async function getFileDirs(ctx, next) { let error = { message: '查询项目目录失败' }, rslt = null; try { const models = ctx.fs.dc.models; rslt = await models.FileRoad.findAll({ // include: [{ // model: models.FileType // }] }) error = null; } catch (err) { ctx.status = 500; ctx.body = { detail: err, ...error }; } if (error) { ctx.status = 400; ctx.body = { ...error }; } else { ctx.status = 200; ctx.body = rslt; } } async function delFileDir(ctx, next) { let error = { message: '文件夹删除失败' }; let rslt = [], fileDirIds = []; try { const { id } = ctx.query, // type == parent / child models = ctx.fs.dc.models; const transaction = await ctx.fs.dc.orm.transaction(); await models.FileRoad.destroy({ where: { rId: id }, transaction }) await models.Files.destroy({ where: { roadId: id }, transaction }) await transaction.commit(); error = null; } catch (err) { await transaction.rollback(); ctx.status = 500; ctx.body = { detail: err, ...error }; } if (error) { ctx.status = 400; ctx.body = { ...error }; } else { ctx.status = 200; ctx.body = { message: '文件夹删除成功' }; } } async function uploadFile(ctx, next) { let error = { message: '文件上传失败' }, rslt = null; const { typeId, userId, userName, fileSize, fileName, fileUrl, fileExt, roadId } = ctx.request.body try { const models = ctx.fs.dc.models; rslt = await models.Files.create({ fId: typeId, uploaderId: userId, roadId, createDate: moment().format('YYYY-MM-DD HH:mm:ss'), fileSize, fileName, fileUrl, fileExt, uploaderName: userName, isDelete: false }) error = null; } catch (err) { ctx.status = 500; ctx.body = { detail: err, ...error }; } if (error) { ctx.status = 400; ctx.body = { ...error }; } else { ctx.status = 200; ctx.body = { message: '文件上传成功', rslt }; } } async function deleteFile(ctx, next) { let error = { message: '文件删除失败' }, rslt = null; const { id } = ctx.query; try { const models = ctx.fs.dc.models; rslt = await models.Files.update({ isDelete: true }, { where: { id: id } }) error = null; } catch (err) { ctx.status = 500; ctx.body = { detail: err, ...error }; } if (error) { ctx.status = 400; ctx.body = { ...error }; } else { ctx.status = 200; ctx.body = { message: '文件删除成功', rslt }; } } async function getFileList(ctx, next) { let error = { message: '文件上传失败' }, rslt = { list: [], counter: 0, type: '' }; const { fId, limit, offset, searchTxt, roadId } = ctx.query; let limit_ = limit, offset_ = offset; if (limit == null || limit < 0) { limit_ = 10; } if (offset == null || offset < 0) { offset_ = 0; } try { const models = ctx.fs.dc.models; let queryOptions = { isDelete: false } if (searchTxt && searchTxt.trim() != '') { queryOptions.fileName = { $like: `%${searchTxt}%` } } if (fId) { queryOptions.fId = fId } if (roadId) { queryOptions.roadId = roadId } rslt.road = await models.FileRoad.findOne({ where: { rId: roadId } }) rslt.type = await models.FileType.findOne({ where: { fId } }) rslt.list = await models.Files.findAll({ where: queryOptions, offset: offset_, limit: limit_, order: [ ['id', 'DESC'] ] }) rslt.counter = await models.Files.count({ where: queryOptions }) error = null; } catch (err) { ctx.status = 500; ctx.body = { detail: err, ...error }; } if (error) { ctx.status = 400; ctx.body = { ...error }; } else { ctx.status = 200; ctx.body = rslt; } } function updateStructDir(opts) { return async function (ctx, next) { let error = { message: '文件夹名称更新失败' }; const models = ctx.fs.dc.models; const { id, name } = ctx.query; try { await models.FileRoad.update({ roadName: name }, { where: { id: id }, }) error = null; } catch (err) { ctx.status = 500; ctx.body = { detail: err, ...error }; } if (error) { ctx.status = 400; ctx.body = { ...error }; } else { ctx.status = 200; ctx.body = { message: '文件夹名称更新成功' }; } } } module.exports = { uploadFile, deleteFile, getFileList, createProjectDir, delFileDir, getFileDirs, updateStructDir, }