-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
233 lines (187 loc) · 5.7 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import {
App,
Editor,
MarkdownView,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
TFile,
TFolder,
addIcon,
Vault,
requestUrl,
TAbstractFile
} from 'obsidian';
import { FolderSuggest } from "./src/FolderSuggester";
import AdmZip from 'adm-zip';
// Remember to rename these classes and interfaces!
interface AskifyPluginSettings {
AskifySyncKeySetting: string;
AskifyLocalFilePathSetting: string;
}
const ASKIFY_DEFAULT_SETTINGS: AskifyPluginSettings = {
AskifySyncKeySetting: 'default',
AskifyLocalFilePathSetting: 'Askify'
}
async function unzipFile(filePath, destPath) {
try {
const zip = new AdmZip(filePath);
zip.extractAllTo(destPath, true);
} catch (e) {
console.log("error in unzipping the file")
console.log(e);
}
}
export default class AskifyPlugin extends Plugin {
settings: AskifyPluginSettings;
async onload() {
console.log("plugin loadded..");
await this.loadSettings();
const ribbonIconEl = this.addRibbonIcon('circle', 'Askify Sync Plugin', async (evt: MouseEvent) => {
// Called when the user clicks the icon.
new Notice('Askify Sync started');
console.log("sync initiated");
const askifySyncVal = await this.loadData();
if (askifySyncVal == null || askifySyncVal.AskifySyncKeySetting == '' || askifySyncVal.AskifySyncKeySetting == 'default') {
new Notice('Askify Sync Failed! Please add the Askify sync key in the plugin settings.');
return
}
const {
vault
} = this.app;
//Step 1. Create file and get its name from cloud storage
let sync_key = askifySyncVal.AskifySyncKeySetting;
let zipFileName = await this.getNotesZipFileName(sync_key);
console.log("zipname is " + zipFileName);
let fileUrl = "https://storage.googleapis.com/temporary_exports/" + zipFileName
//Step 2 : Download the file as zip
//before downloading delete the file if it exists
const files = this.app.vault.getFiles()
const file = this.app.vault.getAbstractFileByPath(`${zipFileName}`)
if (file) {
this.app.vault.delete(file);
}
await this.downloadAskifyNotesAsZip(vault, fileUrl, zipFileName);
//@ts-ignore
let folderPath = this.app.vault.adapter.basePath;
let zipFilePath = folderPath + "/" + zipFileName;
// Step 3: create a folder of Askify
try {
if (!(this.app.vault.getAbstractFileByPath(askifySyncVal.AskifyLocalFilePathSetting) instanceof TFolder)) {
await vault.createFolder(askifySyncVal.AskifyLocalFilePathSetting)
}
} catch (e) {
console.log("error in creating the folder")
console.log(e);
}
let unzip_folder = folderPath + '/' + askifySyncVal.AskifyLocalFilePathSetting + '/'
// Step 4: unzip file in the Askify folder
await unzipFile(zipFilePath, unzip_folder);
//Step 5: delete the zip file
const file2 = this.app.vault.getAbstractFileByPath(`${zipFileName}`)
if (file2) {
this.app.vault.delete(file2);
} else {
console.log("Unable to delete file");
}
new Notice('Sync complete');
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new AskifySettingTab(this.app, this));
}
private async getNotesZipFileName(apikey) {
console.log("preparing for api call");
var data = JSON.stringify({
"apiKey": apikey
});
var config = {
method: 'post',
url: 'https://us-central1-talk-to-videos.cloudfunctions.net/obsidianPluginSync',
headers: {
'Content-Type': 'application/json'
},
body: data
};
try {
let resp = await requestUrl(config);
return resp.text;
} catch (e) {
if (e.status == 417) {
new Notice('Free limit of 12 Syncs reached! Upgrade to Askify Essential for unlimited syncs');
} else {
new Notice('Please add correct Askify sync key');
}
}
}
private downloadAskifyNotesAsZip(vault, fileUrl, fileName) {
let fileData: ArrayBuffer;
return new Promise(async (resolve) => {
console.log("starting the download");
const response = await requestUrl({ url: fileUrl });
fileData = response.arrayBuffer;
if (fileData != null) {
console.log("file data is not null and file name is " + fileName);
try {
await vault.createBinary(fileName, fileData);
}
catch (e) {
console.log("error in creating file");
console.log(e);
}
console.log("file created");
resolve("success");
} else {
console.log("fie data is null");
resolve("error");
}
});
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, ASKIFY_DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class AskifySettingTab extends PluginSettingTab {
plugin: AskifyPlugin;
constructor(app: App, plugin: AskifyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {
containerEl
} = this;
containerEl.empty();
containerEl.createEl('h2', {
text: 'Settings for Askify Sync plugin.'
});
new Setting(containerEl)
.setName('Askify Obsidian sync key')
.setDesc('Get this key from the Askify website')
.addText(text => text
.setPlaceholder('Enter your key')
.setValue(this.plugin.settings.AskifySyncKeySetting)
.onChange(async (value) => {
this.plugin.settings.AskifySyncKeySetting = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName("Askify Local Folder Path")
.setDesc("Enter the folder path where you want to sync the notes")
.addSearch((text) => {
new FolderSuggest(text.inputEl);
text.setPlaceholder("Example: Inbox/Askify")
.setValue(this.plugin.settings.AskifyLocalFilePathSetting)
.onChange(async (value) => {
this.plugin.settings.AskifyLocalFilePathSetting = value;
await this.plugin.saveSettings();
});
});
}
}