task.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. const assert = require('assert');
  3. const Service = require('egg').Service;
  4. class TasktService extends Service {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.model = this.ctx.model.Task;
  8. this.nginxUnit = this.app.config.nginxUnit;
  9. this.nginxFilePath = this.app.config.nginxFilePath;
  10. }
  11. async create({ thumbnail, name, path }) {
  12. assert(thumbnail, '缩略图不存在');
  13. assert(name, '名称不存在');
  14. assert(path, '文件路径不存在');
  15. // 组织配置 nginx 数据
  16. const uri = path.split('/').filter(e => e !== '');
  17. const filePath = uri.slice(0, -1).join('/');
  18. const datas = {
  19. match: {
  20. uri: `/${uri[uri.length - 1]}/`,
  21. },
  22. action: {
  23. share: `/${filePath}/$uri`,
  24. },
  25. };
  26. try {
  27. const { data } = await this.ctx.curl(this.nginxUnit, {
  28. method: 'post',
  29. dataType: 'json',
  30. data: JSON.stringify(datas),
  31. });
  32. if (data.success) {
  33. const url = `${this.nginxFilePath}/${uri[uri.length - 1]}/`;
  34. const res = await this.model.create({ thumbnail, name, url, status: '0', path });
  35. return { errcode: 0, errmsg: 'ok', data: res };
  36. }
  37. return { errcode: -1001, errmsg: 'nginx配置错误' };
  38. } catch (error) {
  39. throw error;
  40. }
  41. }
  42. async update({ id, thumbnail, name, status, path }) {
  43. assert(id, 'id不存在');
  44. // 组织配置 nginx 数据
  45. const uri = path.split('/').filter(e => e !== '');
  46. const filePath = uri.slice(0, -1).join('/');
  47. const datas = {
  48. match: {
  49. uri: `/${uri[uri.length - 1]}/`,
  50. },
  51. action: {
  52. share: `/${filePath}/$uri`,
  53. },
  54. };
  55. try {
  56. const { data } = await this.ctx.curl(this.nginxUnit, {
  57. method: 'post',
  58. dataType: 'json',
  59. data: JSON.stringify(datas),
  60. });
  61. if (data.success) {
  62. const url = `${this.nginxFilePath}/${uri[uri.length - 1]}/`;
  63. await this.model.updateOne({ _id: id }, { thumbnail, name, url, status, path });
  64. return { errcode: 0, errmsg: 'ok', data: '' };
  65. }
  66. return { errcode: -1001, errmsg: 'nginx配置错误' };
  67. } catch (error) {
  68. throw error;
  69. }
  70. }
  71. async delete({ id }) {
  72. assert(id, 'id不存在');
  73. try {
  74. const data = await this.model.findOne({ _id: id });
  75. await this.stopTask({ path: data.path });
  76. await this.model.deleteOne({ _id: id });
  77. return { errcode: 0, errmsg: 'ok', data: '' };
  78. } catch (error) {
  79. throw error;
  80. }
  81. }
  82. async stopTask({ id, path }) {
  83. assert(path, '文件路径不存在');
  84. const uri = path.split('/').filter(e => e !== '');
  85. const { data } = await this.ctx.curl(this.nginxUnit, {
  86. method: 'get',
  87. dataType: 'json',
  88. });
  89. let position;
  90. for (const e in data) {
  91. if (data[e].match.uri === `/${uri[uri.length - 1]}/`) {
  92. position = e;
  93. }
  94. }
  95. const res = await this.ctx.curl(`${this.nginxUnit}${position}/`, {
  96. method: 'delete',
  97. dataType: 'json',
  98. });
  99. try {
  100. if (res.data.success) {
  101. if (id) {
  102. await this.model.updateOne({ _id: id }, { status: '1' });
  103. }
  104. return { errcode: 0, errmsg: 'ok', data: '' };
  105. }
  106. return { errcode: -1001, errmsg: 'nginx配置错误' };
  107. } catch (error) {
  108. throw error;
  109. }
  110. }
  111. async query({ skip, limit, name, url, status }) {
  112. const filter = {};
  113. const arr = { name, url, status };
  114. for (const e in arr) {
  115. const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
  116. if (arr[e]) {
  117. filter.$or = [];
  118. filter.$or.push(JSON.parse(data));
  119. }
  120. }
  121. try {
  122. const total = await this.model.find({ ...filter });
  123. let res;
  124. if (skip && limit) {
  125. res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  126. } else {
  127. res = await this.model.find({ ...filter });
  128. }
  129. return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
  130. } catch (error) {
  131. throw error;
  132. }
  133. }
  134. }
  135. module.exports = TasktService;