From 8a423e5d892fb3ee888f8598c823c82bacd34032 Mon Sep 17 00:00:00 2001 From: "fish.yu" Date: Thu, 18 Apr 2024 14:59:48 +0800 Subject: [PATCH] fix: wrong file name --- lib/download.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/download.js b/lib/download.js index b697c9b..c46e8a1 100644 --- a/lib/download.js +++ b/lib/download.js @@ -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); @@ -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 });