generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfileUtils.ts
140 lines (124 loc) · 3.61 KB
/
fileUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { moment, normalizePath, type TFile } from 'obsidian';
import type ScribePlugin from 'src';
import type { LLMSummary } from './openAiUtils';
export async function saveAudioRecording(
plugin: ScribePlugin,
recordingBuffer: ArrayBuffer,
baseFileName: string,
) {
const pathToSave = plugin.settings.recordingDirectory;
let fullPath = normalizePath(
`${pathToSave}/${baseFileName}.${plugin.state.audioRecord?.fileExtension}`,
);
const fileAlreadyExists = await plugin.app.vault.adapter.exists(
fullPath,
true,
);
if (fileAlreadyExists) {
const uuid = Math.floor(Math.random() * 1000);
fullPath = `${pathToSave}/${baseFileName}.${uuid}.${plugin.state.audioRecord?.fileExtension}`;
}
try {
const savedFile = await plugin.app.vault.createBinary(
fullPath,
recordingBuffer,
);
return savedFile;
} catch (error) {
console.error(`Failed to save file in: ${fullPath}`, error);
throw error;
}
}
export async function createNewNote(
plugin: ScribePlugin,
fileName: string,
): Promise<TFile> {
try {
const pathToSave = plugin.settings.transcriptDirectory;
const fullPath = `${pathToSave}/${fileName}.md`;
let notePath = normalizePath(fullPath);
const fileAlreadyExists = await plugin.app.vault.adapter.exists(
notePath,
true,
);
if (fileAlreadyExists) {
const uuid = Math.floor(Math.random() * 1000);
notePath = normalizePath(`${pathToSave}/${fileName}.${uuid}.md`);
}
const savedFile = await plugin.app.vault.create(notePath, '');
return savedFile;
} catch (error) {
console.error('Failed to save file', error);
if (error === 'Error: File already exists') {
createNewNote(plugin, `${fileName}.${Math.random() * 100}`);
}
throw error;
}
}
export async function renameFile(
plugin: ScribePlugin,
originalNote: TFile,
newFileName: string,
) {
const filePath = originalNote.path.replace(originalNote.name, '');
let preferredFullFileNameAndPath = `${filePath}/${newFileName}.md`;
const fileAlreadyExists = await plugin.app.vault.adapter.exists(
preferredFullFileNameAndPath,
true,
);
if (fileAlreadyExists) {
const uuid = Math.floor(Math.random() * 1000);
preferredFullFileNameAndPath = `${filePath}/${newFileName}.${uuid}.md`;
}
await plugin.app.fileManager.renameFile(
originalNote,
`${preferredFullFileNameAndPath}`,
);
}
export async function setupFileFrontmatter(
plugin: ScribePlugin,
noteFile: TFile,
audioFile?: TFile,
) {
try {
await plugin.app.fileManager.processFrontMatter(noteFile, (frontMatter) => {
const newFrontMatter = {
...frontMatter,
source: audioFile
? [...(frontMatter.source || []), `[[${audioFile.path}]]`]
: frontMatter.source,
created_by: '[[Scribe]]',
};
Object.assign(frontMatter, newFrontMatter);
});
return noteFile;
} catch (error) {
console.error('Failed to addAudioSourceToFrontmatter', error);
throw error;
}
}
export async function appendTextToNote(
plugin: ScribePlugin,
noteFile: TFile,
text: string,
textToReplace?: string,
) {
try {
await plugin.app.vault.process(noteFile, (data) => {
try {
if (textToReplace) {
return data.replace(textToReplace, text);
}
} catch (error) {
console.error('Failed to replace text', error);
// Append anyway
return `${data}\n${text}`;
}
return `${data.length && `${data}\n`}${text}`;
});
return noteFile;
} catch (error) {
console.error('Failed to addAudioSourceToFrontmatter', error);
throw error;
}
}