125 lines
3.8 KiB
JavaScript
125 lines
3.8 KiB
JavaScript
"use strict";
|
|
|
|
const Service = require("egg").Service;
|
|
const OSS = require('ali-oss');
|
|
const http = require('http');
|
|
function fileToBlob(file) {
|
|
// 创建 FileReader 对象
|
|
let reader = new FileReader();
|
|
return new Promise(resolve => {
|
|
// FileReader 添加 load 事件
|
|
reader.addEventListener('load', (e) => {
|
|
let blob;
|
|
if (typeof e.target.result === 'object') {
|
|
blob = new Blob([e.target.result])
|
|
} else {
|
|
blob = e.target.result
|
|
}
|
|
resolve(blob)
|
|
})
|
|
// FileReader 以 ArrayBuffer 格式 读取 File 对象中数据
|
|
reader.readAsArrayBuffer(file)
|
|
})
|
|
}
|
|
|
|
|
|
|
|
class ImageService extends Service {
|
|
/**
|
|
* 保存图片
|
|
* @param {*} showPath 图片展示路径
|
|
* @param {*} dicomInfo dicom信息
|
|
* @param {*} user_id user_id
|
|
*/
|
|
async downloadImage(path, osPath, config) {
|
|
try {
|
|
let client = await this.getClient()
|
|
if (client) {
|
|
const result = await client.get(path, osPath);
|
|
return result
|
|
}
|
|
return false
|
|
// const client = new OSS({
|
|
// region: config.region,
|
|
// accessKeyId: config.accessKeyId,
|
|
// accessKeySecret: config.accessKeySecret,
|
|
// bucket: config.bucket
|
|
// });
|
|
|
|
} catch (e) {
|
|
console.log(e)
|
|
}
|
|
}
|
|
dcmUpload(name, file, config) {
|
|
|
|
// const client = new OSS({
|
|
// region: config.region,
|
|
// accessKeyId: config.accessKeyId,
|
|
// accessKeySecret: config.accessKeySecret,
|
|
// bucket: config.bucket
|
|
// });
|
|
return new Promise(async resolve => {
|
|
let client = await this.getClient()
|
|
try {
|
|
let res = await client.put(name, file)
|
|
resolve({
|
|
...res,
|
|
})
|
|
} catch (e) {
|
|
resolve(false)
|
|
console.log(e)
|
|
}
|
|
})
|
|
}
|
|
async getClient() {
|
|
if (this.ctx.state.ossConfig && this.isCredentialsExpired(this.ctx.state.ossConfig.expiration)) return new OSS(this.ctx.state.ossConfig)
|
|
let config = null
|
|
let res = await this.getOssConfig()
|
|
if (res && res.IsSuccess) {
|
|
config = res.Result[res.Result.ObjectStoreUse]
|
|
config.bucket = config.bucketName
|
|
config.basePath = config.viewEndpoint;
|
|
config.stsToken = config.securityToken
|
|
config.timeout = 10 * 60 * 1000
|
|
this.ctx.state.config = config
|
|
return new OSS(config)
|
|
}
|
|
return false
|
|
}
|
|
getOssConfig() {
|
|
return new Promise((res, rej) => {
|
|
const Authorization = this.ctx.get('Authorization');
|
|
let options = {
|
|
hostname: '106.14.89.110',
|
|
port: 30000,
|
|
path: "/user/GetObjectStoreToken",
|
|
method: "GET",
|
|
headers: {
|
|
Authorization: Authorization
|
|
}
|
|
}
|
|
const req = http.request(options, r => {
|
|
let data = '';
|
|
r.on('data', chunk => {
|
|
data += chunk
|
|
})
|
|
r.on('end', () => {
|
|
res(JSON.parse(data))
|
|
})
|
|
})
|
|
req.end()
|
|
})
|
|
}
|
|
isCredentialsExpired(credentials) {
|
|
if (!credentials) {
|
|
return true;
|
|
}
|
|
const expireDate = new Date(credentials);
|
|
const now = new Date();
|
|
// 如果有效期不足五分钟,视为过期。
|
|
return expireDate.getTime() - now.getTime() <= 300000;
|
|
|
|
}
|
|
}
|
|
module.exports = ImageService;
|