@@ -21,19 +21,17 @@ import { Workspace } from '@/cmd/workspace'
21
21
import { fsUtil } from '@/infra/fs/fsUtil'
22
22
23
23
async function parseFileUri ( fileUri ?: Uri ) {
24
- if ( fileUri !== undefined && fileUri . scheme !== 'file' ) return undefined
24
+ if ( fileUri !== undefined && fileUri . scheme !== 'file' ) return
25
25
if ( fileUri !== undefined ) return fileUri
26
26
27
27
const { activeTextEditor } = window
28
- if ( activeTextEditor === undefined ) return undefined
28
+ if ( activeTextEditor == null ) return
29
29
30
30
const { document } = activeTextEditor
31
31
if ( document . languageId === 'markdown' && ! document . isUntitled ) {
32
32
await document . save ( )
33
33
return document . uri
34
34
}
35
-
36
- return undefined
37
35
}
38
36
39
37
export async function saveLocalPost ( localPost : LocalPost ) {
@@ -202,43 +200,45 @@ export async function uploadPost(input?: Post | PostTreeItem | PostEditDto, conf
202
200
203
201
export async function uploadPostFile ( fileUri ?: Uri , confirm = true ) {
204
202
const parsedFileUri = await parseFileUri ( fileUri )
205
- if ( parsedFileUri === undefined ) return
206
-
207
- const { fsPath : filePath } = parsedFileUri
208
- const postId = PostFileMapManager . getPostId ( parsedFileUri . path )
209
-
210
- if ( postId !== undefined && postId >= 0 ) {
211
- const dto = await PostService . getPostEditDto ( postId )
212
- if ( dto !== undefined ) await uploadPost ( dto , confirm )
213
- return
214
- }
203
+ if ( parsedFileUri == null ) return
215
204
216
205
const fileContent = Buffer . from ( await workspace . fs . readFile ( parsedFileUri ) ) . toString ( )
217
206
if ( isEmptyBody ( fileContent ) ) return
218
207
219
- const selected = await Alert . info (
220
- '本地文件尚未关联到博客园博文' ,
221
- {
222
- modal : true ,
223
- detail : `您可以选择新建一篇博文或将本地文件关联到一篇博客园博文(您可以根据标题搜索您在博客园博文)` ,
224
- } ,
225
- '新建博文' ,
226
- '关联已有博文'
227
- )
228
- if ( selected === '关联已有博文' ) {
229
- const selectedPost = await searchPostByTitle (
230
- path . basename ( filePath , path . extname ( filePath ) ) ,
231
- '搜索要关联的博文'
208
+ const { fsPath : fsPath } = parsedFileUri
209
+ let postId = PostFileMapManager . getPostId ( parsedFileUri . path )
210
+
211
+ if ( postId == null ) {
212
+ const createPost = '新建博文'
213
+ const mapPost = '关联已有博文并上传'
214
+ const selected = await Alert . info (
215
+ '本地文件尚未关联到博客园博文' ,
216
+ {
217
+ modal : true ,
218
+ detail : `您可以选择新建一篇博文或将当前本地文件关联到已有博客园博文` ,
219
+ } ,
220
+ createPost ,
221
+ mapPost
232
222
)
233
- if ( selectedPost === undefined ) return
234
223
235
- await PostFileMapManager . updateOrCreate ( selectedPost . id , parsedFileUri . path )
236
- const postEditDto = await PostService . getPostEditDto ( selectedPost . id )
237
- if ( postEditDto === undefined ) return
238
- if ( fileContent === '' ) await workspace . fs . writeFile ( parsedFileUri , Buffer . from ( postEditDto . post . postBody ) )
224
+ if ( selected === mapPost ) {
225
+ const filenName = path . basename ( fsPath , path . extname ( fsPath ) )
226
+ postId = PostFileMapManager . extractPostId ( filenName )
227
+ if ( postId == null ) {
228
+ const selectedPost = await searchPostByTitle ( filenName , '搜索要关联的博文' )
239
229
240
- await uploadPost ( postEditDto . post , confirm )
241
- } else if ( selected === '新建博文' ) {
242
- await saveLocalPost ( new LocalPost ( filePath ) )
230
+ if ( selectedPost == null ) return Alert . info ( '未选择要关联的博文' )
231
+ postId = selectedPost . id
232
+ }
233
+ if ( postId != null ) await PostFileMapManager . updateOrCreate ( postId , parsedFileUri . path )
234
+ } else if ( selected === createPost ) {
235
+ await saveLocalPost ( new LocalPost ( fsPath ) )
236
+ }
237
+ }
238
+
239
+ if ( postId != null ) {
240
+ const dto = await PostService . getPostEditDto ( postId )
241
+ if ( dto == null ) return Alert . err ( `对应的博文不存在(Id: ${ postId } )` )
242
+ await uploadPost ( dto , confirm )
243
243
}
244
244
}
0 commit comments