|
@@ -0,0 +1,137 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+const assert = require('assert');
|
|
|
+const Service = require('egg').Service;
|
|
|
+class TasktService extends Service {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx);
|
|
|
+ this.model = this.ctx.model.Task;
|
|
|
+ this.nginxUnit = this.app.config.nginxUnit;
|
|
|
+ this.nginxFilePath = this.app.config.nginxFilePath;
|
|
|
+ }
|
|
|
+ async create({ thumbnail, name, path }) {
|
|
|
+ assert(thumbnail, '缩略图不存在');
|
|
|
+ assert(name, '名称不存在');
|
|
|
+ assert(path, '文件路径不存在');
|
|
|
+ // 组织配置 nginx 数据
|
|
|
+ const uri = path.split('/').filter(e => e !== '');
|
|
|
+ const filePath = uri.slice(0, -1).join('/');
|
|
|
+ const datas = {
|
|
|
+ match: {
|
|
|
+ uri: `/${uri[uri.length - 1]}/`,
|
|
|
+ },
|
|
|
+ action: {
|
|
|
+ share: `/${filePath}/$uri`,
|
|
|
+ },
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ const { data } = await this.ctx.curl(this.nginxUnit, {
|
|
|
+ method: 'post',
|
|
|
+ dataType: 'json',
|
|
|
+ data: JSON.stringify(datas),
|
|
|
+ });
|
|
|
+ if (data.success) {
|
|
|
+ const url = `${this.nginxFilePath}/${uri[uri.length - 1]}/`;
|
|
|
+ const res = await this.model.create({ thumbnail, name, url, status: '0', path });
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: res };
|
|
|
+ }
|
|
|
+ return { errcode: -1001, errmsg: 'nginx配置错误' };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async update({ id, thumbnail, name, status, path }) {
|
|
|
+ assert(id, 'id不存在');
|
|
|
+ // 组织配置 nginx 数据
|
|
|
+ const uri = path.split('/').filter(e => e !== '');
|
|
|
+ const filePath = uri.slice(0, -1).join('/');
|
|
|
+ const datas = {
|
|
|
+ match: {
|
|
|
+ uri: `/${uri[uri.length - 1]}/`,
|
|
|
+ },
|
|
|
+ action: {
|
|
|
+ share: `/${filePath}/$uri`,
|
|
|
+ },
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ const { data } = await this.ctx.curl(this.nginxUnit, {
|
|
|
+ method: 'post',
|
|
|
+ dataType: 'json',
|
|
|
+ data: JSON.stringify(datas),
|
|
|
+ });
|
|
|
+ if (data.success) {
|
|
|
+ const url = `${this.nginxFilePath}/${uri[uri.length - 1]}/`;
|
|
|
+ await this.model.updateOne({ _id: id }, { thumbnail, name, url, status, path });
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: '' };
|
|
|
+ }
|
|
|
+ return { errcode: -1001, errmsg: 'nginx配置错误' };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async delete({ id }) {
|
|
|
+ assert(id, 'id不存在');
|
|
|
+ try {
|
|
|
+ const data = await this.model.findOne({ _id: id });
|
|
|
+ await this.stopTask({ path: data.path });
|
|
|
+ await this.model.deleteOne({ _id: id });
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: '' };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async stopTask({ id, path }) {
|
|
|
+ assert(path, '文件路径不存在');
|
|
|
+ const uri = path.split('/').filter(e => e !== '');
|
|
|
+ const { data } = await this.ctx.curl(this.nginxUnit, {
|
|
|
+ method: 'get',
|
|
|
+ dataType: 'json',
|
|
|
+ });
|
|
|
+ let position;
|
|
|
+ for (const e in data) {
|
|
|
+ if (data[e].match.uri === `/${uri[uri.length - 1]}/`) {
|
|
|
+ position = e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const res = await this.ctx.curl(`${this.nginxUnit}${position}/`, {
|
|
|
+ method: 'delete',
|
|
|
+ dataType: 'json',
|
|
|
+ });
|
|
|
+ try {
|
|
|
+ if (res.data.success) {
|
|
|
+ if (id) {
|
|
|
+ await this.model.updateOne({ _id: id }, { status: '1' });
|
|
|
+ }
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: '' };
|
|
|
+ }
|
|
|
+ return { errcode: -1001, errmsg: 'nginx配置错误' };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async query({ skip, limit, name, url, status }) {
|
|
|
+ const filter = {};
|
|
|
+ const arr = { name, url, status };
|
|
|
+ for (const e in arr) {
|
|
|
+ const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
|
|
|
+ if (arr[e]) {
|
|
|
+ filter.$or = [];
|
|
|
+ filter.$or.push(JSON.parse(data));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ const total = await this.model.find({ ...filter });
|
|
|
+ let res;
|
|
|
+ if (skip && limit) {
|
|
|
+ res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
|
|
|
+ } else {
|
|
|
+ res = await this.model.find({ ...filter });
|
|
|
+ }
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = TasktService;
|