Skip to content

Commit 3943fee

Browse files
authored
Merge pull request #230 from cnblogs/fix-post-map
fix: file path in post map
2 parents 391e4fa + f142666 commit 3943fee

12 files changed

+24
-15
lines changed

src/cmd/pdf/export-pdf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function handlePostInput(post: Post | PostTreeItem) {
144144

145145
async function handleUriInput(uri: Uri) {
146146
const { fsPath } = uri
147-
const postId = PostFileMapManager.getPostId(fsPath)
147+
const postId = PostFileMapManager.getPostId(uri.path)
148148
if (postId === undefined) return []
149149
const { post: inputPost } = await PostService.getPostEditDto(postId)
150150

src/cmd/post-list/copy-link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function copyPostLink(input: Post | PostTreeItem | Uri) {
3535
post = input.post
3636
} else {
3737
// input instanceof Uri
38-
const postId = PostFileMapManager.findByFilePath(input.fsPath)?.[0]
38+
const postId = PostFileMapManager.findByFilePath(input.path)?.[0]
3939
if (postId === undefined || postId <= 0) {
4040
void Alert.fileNotLinkedToPost(input)
4141
return

src/cmd/post-list/modify-post-setting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function modifyPostSetting(input: Post | PostTreeItem | Uri) {
2222
postId = input.id
2323
} else {
2424
//type of input is Uri
25-
postId = PostFileMapManager.getPostId(input.fsPath) ?? -1
25+
postId = PostFileMapManager.getPostId(input.path) ?? -1
2626
if (postId < 0) return Alert.fileNotLinkedToPost(input)
2727
}
2828

src/cmd/post-list/open-post-in-vscode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function openPostInVscode(postId: number, forceUpdateLocalPostFile
5252

5353
// 博文内容写入本地文件, 若文件不存在, 会自动创建对应的文件
5454
await workspace.fs.writeFile(fileUri, Buffer.from(post.postBody))
55-
await PostFileMapManager.updateOrCreate(postId, fileUri.fsPath)
55+
await PostFileMapManager.updateOrCreate(postId, fileUri.path)
5656
await openPostFile(post)
5757
return fileUri
5858
}

src/cmd/post-list/post-pull-all.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ProgressLocation, Uri, window, workspace } from 'vscode'
66
import { buildLocalPostFileUri } from '@/cmd/post-list/open-post-in-vscode'
77
import { UserService } from '@/service/user.service'
88
import { fsUtil } from '@/infra/fs/fsUtil'
9+
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
910

1011
enum ConflictStrategy {
1112
ask,
@@ -67,7 +68,11 @@ export async function postPullAll() {
6768
const path = PostFileMapManager.getFilePath(post.id)
6869

6970
// 本地没有博文或关联到的文件不存在
70-
if (path === undefined || !(await fsUtil.exists(path))) {
71+
if (
72+
path === undefined ||
73+
!(await fsUtil.exists(path)) ||
74+
path.indexOf(WorkspaceCfg.getWorkspaceUri().path) < 0
75+
) {
7176
const uri = buildLocalPostFileUri(post, false)
7277
const buf = Buffer.from(post.postBody)
7378
await workspace.fs.writeFile(uri, buf)

src/cmd/post-list/post-pull.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu
3030
await handlePostInput(input, ctxList, uri.path)
3131
} else {
3232
isFreshPull = !(await fsUtil.exists(path))
33+
if (!path.startsWith('/')) await PostFileMapManager.updateOrCreate(post.id, Uri.file(path).path)
3334
await handlePostInput(input, ctxList, path)
3435
}
3536
} else {
@@ -82,7 +83,7 @@ function parseUriInput(input: InputType): Uri | undefined {
8283
}
8384

8485
function handleUriInput(fileUri: Uri, contexts: CmdCtx[]) {
85-
const postId = PostFileMapManager.getPostId(fileUri.fsPath)
86+
const postId = PostFileMapManager.getPostId(fileUri.path)
8687
if (postId === undefined) return Alert.fileNotLinkedToPost(fileUri)
8788

8889
contexts.push({ postId, fileUri })

src/cmd/post-list/rename-post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function renameLinkedFile(post: Post): Promise<void> {
2929
const ext = path.extname(fileName)
3030
const newFilePath = filePath.replace(new RegExp(`${escapeRegExp(fileName)}$`), `${post.title}${ext}`)
3131
await workspace.fs.rename(fileUri, Uri.file(newFilePath))
32-
await PostFileMapManager.updateOrCreate(post.id, newFilePath)
32+
await PostFileMapManager.updateOrCreate(post.id, fileUri.path)
3333
postDataProvider.fireTreeDataChangedEvent(post)
3434
}
3535
}

src/cmd/post-list/upload-post.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export async function uploadPostFile(fileUri?: Uri, confirm = true) {
205205
if (parsedFileUri === undefined) return
206206

207207
const { fsPath: filePath } = parsedFileUri
208-
const postId = PostFileMapManager.getPostId(filePath)
208+
const postId = PostFileMapManager.getPostId(parsedFileUri.path)
209209

210210
if (postId !== undefined && postId >= 0) {
211211
const dto = await PostService.getPostEditDto(postId)
@@ -232,7 +232,7 @@ export async function uploadPostFile(fileUri?: Uri, confirm = true) {
232232
)
233233
if (selectedPost === undefined) return
234234

235-
await PostFileMapManager.updateOrCreate(selectedPost.id, filePath)
235+
await PostFileMapManager.updateOrCreate(selectedPost.id, parsedFileUri.path)
236236
const postEditDto = await PostService.getPostEditDto(selectedPost.id)
237237
if (postEditDto === undefined) return
238238
if (fileContent === '') await workspace.fs.writeFile(parsedFileUri, Buffer.from(postEditDto.post.postBody))

src/cmd/show-local-file-to-post-info.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function showLocalFileToPostInfo(input: Uri | number): Promise<void
1818
let filePath: string | undefined
1919
let postId: number | undefined
2020
if (input instanceof Uri && input.scheme === 'file') {
21-
postId = PostFileMapManager.getPostId(input.fsPath)
21+
postId = PostFileMapManager.getPostId(input.path)
2222
filePath = input.fsPath
2323
if (postId === undefined) {
2424
const options = ['现在去关联']
@@ -36,7 +36,7 @@ export async function showLocalFileToPostInfo(input: Uri | number): Promise<void
3636
'搜索要关联的博文'
3737
)
3838
if (selectedPost !== undefined) {
39-
await PostFileMapManager.updateOrCreate(selectedPost.id, filePath)
39+
await PostFileMapManager.updateOrCreate(selectedPost.id, input.path)
4040
void Alert.info(`本地文件已与博文(${selectedPost.title}, Id: ${selectedPost.id})建立关联`)
4141
}
4242
}

src/cmd/view-post-online.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export async function viewPostOnline(input?: Post | PostTreeItem | Uri) {
1010
if (input === undefined) input = window.activeTextEditor?.document.uri
1111

1212
if (input instanceof Uri) {
13-
const postId = PostFileMapManager.getPostId(input.fsPath)
13+
const postId = PostFileMapManager.getPostId(input.path)
1414
if (postId !== undefined) post = (await PostService.getPostEditDto(postId))?.post
1515
}
1616

0 commit comments

Comments
 (0)