123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 'use strict';
- const fs = require('fs');
- // node.js 路径操作对象
- const path = require('path');
- // 异步二进制 写入流
- const awaitWriteStream = require('await-stream-ready').write;
- // 管道读入一个虫洞。
- const sendToWormhole = require('stream-wormhole');
- const axios = require('axios');
- const FormData = require('form-data');
- const Service = require('egg').Service;
- class ImageHandleService extends Service {
- async upload(filename) {
- const ctx = this.ctx;
- const stream = await ctx.getFileStream();// 获取文件
- const fileNameT = stream.filename;
- const fileType = fileNameT.split('.')[fileNameT.split('.').length - 1].toLowerCase();
- if (fileType === 'png' || fileType === 'jpg' || fileType === 'jpeg') {
- if (ctx.request.headers['content-length'] / 1024 < 1024) { // 上传文件大小限制
- // let stream;
- try {
- if (!filename) {
- // 新建一个文件名 = 文件名 + 时间 + 文件后缀
- filename = path.parse(stream.filename).name
- + Date.now() + path.extname(stream.filename)
- .toLocaleLowerCase();
- }
- // TODO 修改成OSS -CH (已完成)
- // OSS (STS) Bucket
- await this.ctx.oss.get('bucket1').putStream(this.app.config.defaultWriteAliOSSPath + filename, stream);
- return this.app.config.defaultWritePathPre + filename;
- } catch (err) {
- ctx.logger.debug(err);
- // 如果出现错误,关闭管道
- // await sendToWormhole(stream);
- return null;
- }
- } else {
- return null;
- }
- } else {
- return null;
- }
- }
- // 上传操作手册,限制只能为docx的格式
- async uploadOperationManual(filename) {
- const ctx = this.ctx;
- const stream = await ctx.getFileStream();// 获取文件
- const fileNameT = stream.filename;
- const fileType = fileNameT.split('.')[fileNameT.split('.').length - 1];
- if (fileType === 'docx') {
- try {
- if (!filename) {
- // 新建一个文件名 = 文件名 + 时间 + 文件后缀
- filename = path.parse(stream.filename).name
- + Date.now() + path.extname(stream.filename)
- .toLocaleLowerCase();
- }
- // TODO 修改成OSS -CH (已完成)
- // OSS Bucket
- await this.ctx.oss.get('bucket1').putStream(this.app.config.operationManualPath + filename, stream);
- return this.app.config.defaultWriteFilePathPre + filename;
- } catch (err) {
- ctx.logger.debug(err);
- return null;
- }
- } else {
- return null;
- }
- }
- async uploadImageXyService(filename, filepath, clientType) {
- const { ctx } = this;
- const filenameObs = filename.substring(1); // 上传至obs的 需要去掉起始的/
- const mydata = new FormData();
- mydata.append('objkey', 'sckjpicup$' + filenameObs);
- mydata.append('objdata', fs.createReadStream(path.resolve(__dirname, filepath)));
- const requestResult = await axios({
- method: 'post',
- url: 'http://10.201.147.2:8080/obs-demo/objUpload',
- data: mydata,
- headers: mydata.getHeaders(),
- });
- if (requestResult.status === 200) {
- if (clientType === 'PC') {
- const arr = [];
- arr.push({ url: filename });
- return arr;
- } else if (clientType === 'wxInfo') {
- return { url: filename };
- } else if (clientType === 'SCAN') {
- return filename;
- }
- ctx.logger('回显信息错误,请联系管理员!');
- } else {
- ctx.logger('上传文件错误,请联系管理员!');
- }
- }
- }
- module.exports = ImageHandleService;
|