Skip to content

Commit

Permalink
fix: wrong file name
Browse files Browse the repository at this point in the history
  • Loading branch information
fish.yu committed Apr 18, 2024
1 parent 6c5a5a9 commit 8a423e5
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Downloader {
// 按照cdn的url规则,获取图片名称
// https://cdn.nlark.com/yuque/0/2024/png/394019/1713409985862-2a651a60-380a-4227-852c-e81d193340c5.png#averageHue=%23f8f9fa&clientId=u7b4c5a0c-9e50-4&from=ui&id=u0437de00&originHeight=48&originWidth=48&originalType=binary&ratio=2&rotation=0&showTitle=false&size=1428&status=done&style=none&taskId=ufab6517c-e059-4e38-8cef-b2ddf4390f8&title=
const pathname = new URL(src).pathname;
const fileName = pathname.split('.').shift();
const fileName = pathname.split('/').pop();
const name = `${repo_name}_${slug}_image_${fileName}`
const imageUrl = await this.uploadImageFromUrl(src, name);
mdBody = mdBody.replace(src, imageUrl);
Expand All @@ -163,10 +163,15 @@ class Downloader {
async uploadImageFromUrl(url, name) {
// 获取图片后缀
const pathname = new URL(url).pathname;
const suffix = '.' + pathname.split('.').pop();
// 下载图片
const content = await downloadImage(url);
const imageName = (name || crypto.randomUUID()) + suffix;
let imageName;
if (name?.includes('.')) {
imageName = name;
} else {
const suffix = '.' + pathname.split('.').pop();
imageName = (name || crypto.randomUUID()) + suffix
}
const imageUrl = `_images/${imageName}`;
// 上传到github
const result = await this.gh.writeFile(imageUrl, content, { encode: false });
Expand Down

0 comments on commit 8a423e5

Please sign in to comment.