Skip to content

Commit

Permalink
Merge pull request #182 from Conflux-Chain/dev
Browse files Browse the repository at this point in the history
use server sign upload
  • Loading branch information
21paradox authored May 28, 2020
2 parents 7c9ba56 + 6ecd251 commit 9580fd7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 510 deletions.
1 change: 1 addition & 0 deletions oss-utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// console.log('Loading function ...');
// eslint-disable-next-line import/no-unresolved
const OSS = require('ali-oss');
const config = require('./config');

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
},
"dependencies": {
"@sentry/browser": "^5.5.0",
"ali-oss": "^6.1.1",
"axios": "^0.18.0",
"classnames": "^2.2.6",
"cross-env": "^5.2.0",
Expand Down
2 changes: 0 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export const REGEX = {
};

export const ALI_OSS_KEYS = {
AccessKeyId: 'LTAIf1is0L1Eil4F',
AccessKeySecret: 'Ac7mB405j33q2JaNotxbe6Kbra76r1',
bucket: 'conflux-bucket-test',
region: 'oss-cn-beijing',
downBucket: 'conflux-bounty-readonly-test',
Expand Down
7 changes: 7 additions & 0 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,10 @@ export const reqAcccountUnBind = param => {
body: param,
}).then(res => res.body);
};

export const reqOssKey = param => {
return sendRequest({
url: '/file/getKey',
body: param,
}).then(res => res.body);
};
44 changes: 22 additions & 22 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { toast as $toast } from '../components/Toast';
import { notice as $notice } from '../components/Message/notice';
import { ALI_OSS_KEYS, UPDATE_HEAD, UPDATE_UNREAD_MESSAGE_COUNT } from '../constants';
// eslint-disable-next-line import/no-cycle
import { reqAccountQuery, reqMessageCount } from './api';
import { reqAccountQuery, reqMessageCount, reqOssKey } from './api';
import { i18nTxt, i18nTxtAsync } from './i18n';
import unitParser from './device';

Expand Down Expand Up @@ -277,33 +277,33 @@ export function genImgUrlFromName(name, md5hex) {
return `${ALI_OSS_KEYS.attachmentGetHost}/img/${md5hex}/${encodeURIComponent(name)}`;
}

let ossClient;
const getOssClient = () => {
if (ossClient) {
return Promise.resolve(ossClient);
}
return import('ali-oss').then(mod => {
const Oss = mod.default;
ossClient = new Oss({
region: ALI_OSS_KEYS.region,
accessKeyId: ALI_OSS_KEYS.AccessKeyId,
accessKeySecret: ALI_OSS_KEYS.AccessKeySecret,
bucket: ALI_OSS_KEYS.bucket,
});
return ossClient;
});
};

export const uploadFileOss = (key, file) => {
dispatch({
type: 'PAGE_LOADING+',
payload: {},
});

const reqFile = getOssClient().then(client => {
return client.put(key, file).then(result => {
return result;
});
let dirType = 'bounty';
if (key.match(/^img/)) {
dirType = 'img';
}

const reqFile = reqOssKey({ dirType }).then(body => {
const formData = new FormData();
const ossKeys = body.result;
formData.append('key', key);
formData.append('host', ossKeys.host);
formData.append('policy', ossKeys.policy);
formData.append('Signature', ossKeys.signature);
formData.append('OSSAccessKeyId', ossKeys.OSSAccessKeyId);
formData.append('success_action_status', ossKeys.success_action_status);
formData.append('file', file);
return superagent
.post(ossKeys.host)
.send(formData)
.then(result => {
return result;
});
});

reqFile
Expand Down
Loading

1 comment on commit 9580fd7

@vercel
Copy link

@vercel vercel bot commented on 9580fd7 May 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.