11import * as vscode from 'vscode' ;
22import * as path from 'path' ;
33import { findMarkdownFilesWithImageFolders } from './utils/fs' ;
4+ import { buildArticleEndpoint , buildImageEndpoint } from './net/api' ;
45
56function extractMeta ( md : string ) : { author ?: string ; category ?: string ; content : string } {
67 let author : string | undefined ;
@@ -56,11 +57,10 @@ function extractMeta(md: string): { author?: string; category?: string; content:
5657
5758export async function uploadFolderAsBlog ( uri ?: vscode . Uri ) {
5859 const cfg = vscode . workspace . getConfiguration ( ) ;
59- const uploadArticleUrl = cfg . get < string > ( 'mkBlog.uploadArticleUrl' ) ;
60- const uploadImageUrl = cfg . get < string > ( 'mkBlog.uploadImageUrl' ) ;
60+ const baseUrl = ( cfg . get < string > ( 'mkBlog.baseUrl' ) || '' ) . trim ( ) ;
6161 const token = cfg . get < string > ( 'mkBlog.authToken' ) || '' ;
62- if ( ! uploadArticleUrl && ! uploadImageUrl ) {
63- throw new Error ( '未配置上传接口:mkBlog.uploadArticleUrl 或 mkBlog.uploadImageUrl ' ) ;
62+ if ( ! baseUrl ) {
63+ throw new Error ( '未配置 mkBlog.baseUrl ' ) ;
6464 }
6565
6666 // 优先使用传入目录;否则使用已打开的工作区根目录;若都没有,再让用户选择
@@ -85,6 +85,8 @@ export async function uploadFolderAsBlog(uri?: vscode.Uri) {
8585 const headers : Record < string , string > = { } ;
8686 if ( token ) headers [ 'Authorization' ] = `Bearer ${ token } ` ;
8787
88+ const imageEndpoint = buildImageEndpoint ( baseUrl ) ;
89+
8890 for ( const t of tasks ) {
8991 const title = path . basename ( t . mdPath , '.md' ) ;
9092 // 分离上传:文章 PUT /article/{title},图片逐张 PUT /image
@@ -100,31 +102,29 @@ export async function uploadFolderAsBlog(uri?: vscode.Uri) {
100102 content : meta . content ,
101103 } ;
102104 if ( meta . author ) article . author = meta . author ;
103- else article . author = cfg . get < string > ( 'mkBlog.defaultAuthor' ) ;
105+ else article . author = cfg . get < string > ( 'mkBlog.defaultAuthor' ) ?? cfg . get < string > ( 'mkBlog.author' ) ;
104106
105107 if ( meta . category ) article . category = meta . category ;
106108 else article . category = cfg . get < string > ( 'mkBlog.defaultCategory' ) ;
107109
108- if ( uploadArticleUrl ) {
109- try {
110- const articleUrl = uploadArticleUrl . replace ( '{title}' , encodeURIComponent ( title ) ) ;
110+ try {
111+ const articleUrl = buildArticleEndpoint ( baseUrl , title ) ;
111112 const jsonHeaders : Record < string , string > = { ...headers , 'Content-Type' : 'application/json' } ;
112113 const res = await fetch ( articleUrl , { method : 'PUT' , body : JSON . stringify ( article ) , headers : jsonHeaders } ) ;
113114 if ( ! res . ok ) {
114115 const text = await res . text ( ) . catch ( ( ) => '' ) ;
115116 vscode . window . showWarningMessage ( `上传文章失败(JSON): ${ path . basename ( t . mdPath ) } -> HTTP ${ res . status } ${ res . statusText } ${ text } ,将继续上传图片。` ) ;
116117 }
117- } catch ( err : any ) {
118- vscode . window . showWarningMessage ( `上传文章异常: ${ path . basename ( t . mdPath ) } -> ${ err ?. message || err } ,将继续上传图片。` ) ;
119- }
118+ } catch ( err : any ) {
119+ vscode . window . showWarningMessage ( `上传文章异常: ${ path . basename ( t . mdPath ) } -> ${ err ?. message || err } ,将继续上传图片。` ) ;
120120 }
121121
122- if ( uploadImageUrl ) {
122+ if ( imageEndpoint ) {
123123 for ( const img of t . images ) {
124124 const base64 = Buffer . from ( img . buffer ) . toString ( 'base64' ) ;
125125 const payload = { title, data : base64 , name : img . name } ;
126126 const jsonHeaders : Record < string , string > = { ...headers , 'Content-Type' : 'application/json' } ;
127- const res = await fetch ( uploadImageUrl , { method : 'PUT' , body : JSON . stringify ( payload ) , headers : jsonHeaders } ) ;
127+ const res = await fetch ( imageEndpoint , { method : 'PUT' , body : JSON . stringify ( payload ) , headers : jsonHeaders } ) ;
128128 if ( ! res . ok ) {
129129 const text = await res . text ( ) . catch ( ( ) => '' ) ;
130130 throw new Error ( `上传图片失败(JSON): ${ img . name } -> HTTP ${ res . status } ${ res . statusText } ${ text } ` ) ;
0 commit comments