mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-14 19:52:10 +08:00
upload pic
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user