Skip to content

Commit

Permalink
feat: update cdnHost
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Oct 15, 2020
1 parent 9848a76 commit b344d67
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
23 changes: 10 additions & 13 deletions src/api/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Buffer from 'buffer-from';
import {
v4 as uuidv4
} from 'uuid';
import { resolveConfigFile } from 'prettier';

const defaultConfig = {
username: 'filess',
Expand All @@ -29,7 +30,7 @@ function fileUpload(content, file) {
case 'aliOSS':
return aliOSSFileUpload(content, file.name);
case 'txCOS':
return txCOSFileUpload(file);
return txCOSFileUpload(file);
case 'github':
default:
return ghFileUpload(content, file.name);
Expand Down Expand Up @@ -79,7 +80,7 @@ async function ghFileUpload(content, filename) {
const githubResourceUrl = 'raw.githubusercontent.com/filess/images/master/';
const cdnResourceUrl = 'cdn.jsdelivr.net/gh/filess/images/';
return isDefault ? res.content.download_url.replace(githubResourceUrl, cdnResourceUrl) : res.content.download_url;

}

async function aliOSSFileUpload(content, filename) {
Expand All @@ -95,10 +96,7 @@ async function aliOSSFileUpload(content, filename) {
accessKeySecret: aliOSSConfig.accessKeySecret
});
const res = await client.put(dir, buffer);
if(aliOSSConfig.cdnHost != ''){
return aliOSSConfig.cdnHost +'/'+dir;
}
return res.url;
return aliOSSConfig.cdnHost == '' ? res.url : aliOSSConfig.cdnHost + '/' + (aliOSSConfig.path == '' ? dateFilename : dir);
} catch (e) {
return Promise.reject(e);
}
Expand All @@ -117,16 +115,15 @@ async function txCOSFileUpload(file) {
Region: txCOSConfig.region,
Key: txCOSConfig.path + '/' + dateFilename,
Body: file
}, function(err, data) {
}, function (err, data) {
if (err) {
reject(err);
} else if (txCOSConfig.cdnHost) {
// if cdnHost exists
resolve(txCOSConfig.path != '' ? txCOSConfig.cdnHost + '/' + txCOSConfig.path + '/' + dateFilename : txCOSConfig.cdnHost + '/' + dateFilename);
} else {
if(txCOSConfig.cdnHost != ''){
resolve(txCOSConfig.cdnHost+'/'+txCOSConfig.path + '/' + dateFilename);
}else{
resolve(data.Location);
}

// if cdnHost not exists
reject(data.Location);
}
});
})
Expand Down
6 changes: 3 additions & 3 deletions src/components/CodemirrorEditor/uploadImgDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<el-input v-model.trim="formTxCOS.region"
placeholder="如:ap-guangzhou"></el-input>
</el-form-item>
<el-form-item label="自定义CDN域名" :required="false">
<el-form-item label="自定义 CDN 域名" :required="false">
<el-input v-model.trim="formTxCOS.cdnHost"
placeholder="如:https://imagecdn.alidaodao.com"></el-input>
</el-form-item>
Expand Down Expand Up @@ -131,15 +131,15 @@ export default {
bucket: "",
region: "",
path: "",
cdnHost:""
cdnHost: "",
},
formTxCOS: {
secretId: "",
secretKey: "",
bucket: "",
region: "",
path: "",
cdnHost:""
cdnHost: "",
},
options: [{
value: "default",
Expand Down

0 comments on commit b344d67

Please sign in to comment.