diff --git a/app/controller/v1/example.js b/app/controller/v1/example.js index 59d71c2..728aa39 100644 --- a/app/controller/v1/example.js +++ b/app/controller/v1/example.js @@ -2,6 +2,8 @@ const BaseController = require('../base'); const os = require('os'); +const fs = require('fs'); +const path = require('path'); class ExampleController extends BaseController { @@ -31,6 +33,37 @@ class ExampleController extends BaseController { self.sendSuccess(data); } + + async uploadFile() { + const self = this; + const { ctx, service } = this; + let tmpDir = service.storage.getStorageDir(); + // for (const file of ctx.request.files) { + // this.app.logger.info('file:', file); + + // try { + // let tmpFile = fs.readFileSync(file.filepath) + // fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile) + // } finally { + // await fs.unlink(file.filepath, function(){}); + // } + // const fileStream = fs.createReadStream(path.join(tmpDir, file.filename)) + // const uploadRes = await service.example.uploadFileToSMMS(fileStream); + // } + const file = ctx.request.files[0]; + this.app.logger.info('file:', file); + + try { + let tmpFile = fs.readFileSync(file.filepath) + fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile) + } finally { + await fs.unlink(file.filepath, function(){}); + } + const fileStream = fs.createReadStream(path.join(tmpDir, file.filename)) + const uploadRes = await service.example.uploadFileToSMMS(fileStream); + + self.sendData(uploadRes); + } } module.exports = ExampleController; diff --git a/app/router/example.js b/app/router/example.js index 4a446c9..88842b8 100644 --- a/app/router/example.js +++ b/app/router/example.js @@ -7,4 +7,6 @@ module.exports = app => { const { router, controller } = app; // open local dir router.post('/api/v1/example/openLocalDir', controller.v1.example.openLocalDir); + // upload file + router.post('/api/v1/example/uploadFile', controller.v1.example.uploadFile); }; \ No newline at end of file diff --git a/app/service/example.js b/app/service/example.js index 203f454..9c5f677 100644 --- a/app/service/example.js +++ b/app/service/example.js @@ -1,6 +1,8 @@ 'use strict'; const BaseService = require('./base'); +const fs = require('fs'); +const path = require('path'); class ExampleService extends BaseService { async openLocalDir(dir) { @@ -10,6 +12,80 @@ class ExampleService extends BaseService { return true; } + + async getSMMSToken() { + const res = { + code: 1000, + message: 'unknown error', + }; + + try { + //throw new Error('Sync Error'); + params = { + username: '', + password: '' + }; + const url = 'https://sm.ms/api/v2/token'; + const response = await this.app.curl(url, { + method: 'POST', + contentType: 'application/json', + data: params, + dataType: 'json', + timeout: 15000, + }); + const result = response.data; + if (this.app.config.env === 'local') { + this.app.logger.info('[ExampleService] [getSMMSToken]: info result:%j', result); + } + // this.app.logger.info('[OutapiService] [api]: result:%j', result); + if (result.code !== 'success') { + this.app.logger.error('[ExampleService] [getSMMSToken]: res error result:%j', result); + } + return result; + } catch (e) { + this.app.logger.error('[ExampleService] [getSMMSToken]: ERROR ', e); + } + + return res; + } + async uploadFileToSMMS(tmpFile) { + const res = { + code: 1000, + message: 'unknown error', + }; + + try { + //throw new Error('Sync Error'); + const headersObj = { + 'Content-Type': 'multipart/form-data', + 'Authorization': 'pHVaIfVX8kgxsEL2THTYMVzJDYY3MMZU' + }; + const url = 'https://sm.ms/api/v2/upload'; + const response = await this.app.curl(url, { + method: 'POST', + headers: headersObj, + files: { + smfile: tmpFile, + }, + //contentType: 'application/json', + //data: params, + dataType: 'json', + timeout: 15000, + }); + const result = response.data; + if (this.app.config.env === 'local') { + this.app.logger.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result); + } + if (result.code !== 'success') { + this.app.logger.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result); + } + return result; + } catch (e) { + this.app.logger.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e); + } + + return res; + } } module.exports = ExampleService; \ No newline at end of file diff --git a/frontend/src/api/main.js b/frontend/src/api/main.js index 1037d5a..a7e01df 100644 --- a/frontend/src/api/main.js +++ b/frontend/src/api/main.js @@ -3,7 +3,8 @@ import request from '@/utils/request' const mainApi = { outApi: '/v1/outApi', - openDir: '/v1/example/openLocalDir' + openDir: '/v1/example/openLocalDir', + uploadFile: '/v1/example/uploadFile', } /** @@ -28,4 +29,15 @@ export function openDir (parameter) { method: 'post', data: parameter }) +} + +/** + * uploadFile + */ +export function uploadFile (parameter) { + return request({ + url: mainApi.uploadFile, + method: 'post', + data: parameter + }) } \ No newline at end of file diff --git a/frontend/src/config/router.config.js b/frontend/src/config/router.config.js index 563852a..9b419dd 100644 --- a/frontend/src/config/router.config.js +++ b/frontend/src/config/router.config.js @@ -12,6 +12,11 @@ export const constantRouterMap = [ name: 'FileOpenDir', component: () => import('@/views/file/OpenDir') }, + { + path: 'uploadFile', + name: 'UploadFile', + component: () => import('@/views/file/UploadFile') + }, { path: 'setting1', name: 'setting1', diff --git a/frontend/src/views/Layout.vue b/frontend/src/views/Layout.vue index bc0fedd..9ac094c 100644 --- a/frontend/src/views/Layout.vue +++ b/frontend/src/views/Layout.vue @@ -57,7 +57,11 @@ export default { 'subMenu_1' : { title: '打开文件夹', page: 'fileOpenDir' - } + }, + 'subMenu_2' : { + title: '上传文件到sm图床', + page: 'uploadFile' + }, }, 'menu_2' : { 'subMenu_1' : { diff --git a/frontend/src/views/file/OpenDir.vue b/frontend/src/views/file/OpenDir.vue index 81633f4..f74b128 100644 --- a/frontend/src/views/file/OpenDir.vue +++ b/frontend/src/views/file/OpenDir.vue @@ -1,7 +1,7 @@