Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions app/controller/appCenters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
const moment = require('moment');
const path = require('path');
const fs = require('fs');
// 异步二进制 写入流
const awaitWriteStream = require('await-stream-ready').write;
// 管道读入一个虫洞。
const sendToWormhole = require('stream-wormhole');

class AppCentersController extends Controller {
async getAppCenterList() {
const { app, ctx } = this;

Check warning on line 9 in app/controller/appCenters.js

View workflow job for this annotation

GitHub Actions / setup (18.x)

Unexpected aliasing of members of 'this' to local variables
const { appName = '', appTags } = ctx.request.query;
const appResult = await app.model.AppCenters.findAndCountAll({
attributes: [
Expand Down Expand Up @@ -57,7 +53,7 @@
}

async updateApplications() {
const { app, ctx } = this;

Check warning on line 56 in app/controller/appCenters.js

View workflow job for this annotation

GitHub Actions / setup (18.x)

Unexpected aliasing of members of 'this' to local variables
const { appName, appUrl, appDesc, appTags, id } = ctx.request.body;

if (_.isNil(appName)) throw new Error('缺少必要参数appName');
Expand All @@ -71,7 +67,7 @@
appDesc,
appTags: appTags.join(),
id,
updated_at: moment().format('YYYY-MM-DD'),

Check warning on line 70 in app/controller/appCenters.js

View workflow job for this annotation

GitHub Actions / setup (18.x)

Identifier 'updated_at' is not in camel case
});

if (id) {
Expand All @@ -87,7 +83,7 @@
}

async getApplicationById() {
const { app, ctx } = this;

Check warning on line 86 in app/controller/appCenters.js

View workflow job for this annotation

GitHub Actions / setup (18.x)

Unexpected aliasing of members of 'this' to local variables
const { id } = ctx.request.query;
const result = await app.model.AppCenters.findOne({
attributes: [
Expand All @@ -107,7 +103,7 @@
}

async clickApplications() {
const { app, ctx } = this;

Check warning on line 106 in app/controller/appCenters.js

View workflow job for this annotation

GitHub Actions / setup (18.x)

Unexpected aliasing of members of 'this' to local variables
const { params } = ctx.request.body;
const result = await ctx.service.appCenters.clickApplications({
...params,
Expand All @@ -116,7 +112,7 @@
}

async deleteApplications() {
const { app, ctx } = this;

Check warning on line 115 in app/controller/appCenters.js

View workflow job for this annotation

GitHub Actions / setup (18.x)

Unexpected aliasing of members of 'this' to local variables
const { id } = ctx.request.body;
if (_.isNil(id)) throw new Error('缺少必要参数id');
const result = await ctx.service.appCenters.deleteApplications(id);
Expand All @@ -126,10 +122,17 @@
async uploadLogo() {
const { app, ctx } = this;
const id = ctx.params.id;
// 读取文件流
const stream = await this.ctx.getFileStream();
// 文件名
const fileName = stream.filename;
const files = ctx.request.files
? Array.isArray(ctx.request.files)
? ctx.request.files
: [ctx.request.files]
: [];
const file = files[0];
if (!file) {
ctx.throw(400, '缺少上传文件');
}
// 全局 multipart 已经是 file 模式,这里直接复用临时文件,避免重复消费请求体
const fileName = file.filename;
// 目标文件夹,没有就创建,创建多级目录存储
const date = new Date();
const dirTree = [
Expand All @@ -142,14 +145,14 @@
const dir = app.utils.createFolder(dirTree);
// 创建文件
const target = path.join(dir, fileName);
const writeStream = fs.createWriteStream(target);
try {
// 异步把文件流 写入
await awaitWriteStream(stream.pipe(writeStream));
fs.copyFileSync(file.filepath, target);
} catch (err) {
// 如果出现错误,关闭管道
await sendToWormhole(stream);
throw new Error('图片上传出错');
} finally {
if (file.filepath && fs.existsSync(file.filepath)) {
fs.unlinkSync(file.filepath);
}
}
const result = await this.ctx.model.AppCenters.update(
{ logoUrl: [...dirTree, fileName].join('/') },
Expand Down
Loading