generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.ts
214 lines (186 loc) · 4.9 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
import axios from "axios";
import * as E from "fp-ts/lib/Either";
import { Lazy, pipe } from "fp-ts/lib/function";
import * as O from "fp-ts/lib/Option";
import * as RA from "fp-ts/lib/ReadonlyArray";
import * as Str from "fp-ts/lib/string";
import * as TE from "fp-ts/lib/TaskEither";
import {
App,
FileSystemAdapter,
Notice,
Plugin,
PluginSettingTab,
Setting,
TAbstractFile,
TFile,
Vault,
} from "obsidian";
interface Settings {
url: string;
apiKey: string;
apiSecret: string;
}
const DEFAULT_SETTINGS: Settings = {
url: "",
apiKey: "",
apiSecret: "",
};
const PUBLISHED_DIR = "published";
export default class FilePublisher extends Plugin {
settings: Settings;
async onload() {
await this.loadSettings();
const adapter = this.app.vault.adapter as FileSystemAdapter;
const basePath = adapter.getBasePath();
this.registerEvent(
this.app.workspace.on("file-menu", (menu, file) => {
if (!("extension" in file)) {
return;
}
const { url, apiKey, apiSecret } = this.settings;
const token = Buffer.from(`${apiKey}:${apiSecret}`).toString("base64");
menu.addItem((item) => {
item
.setTitle("Publish file")
.setIcon("document")
.onClick(() =>
pipe(
file,
log("Publishing file..."),
TE.fromNullable(new Error("File not found")),
TE.chain(publishFile(this.app.vault, url, token)),
TE.chain(moveFile(this.app.vault)),
TE.match(
(e) => notify(e, "File failed to publish"),
() => notify(undefined, "File has been published")
)
)()
);
});
})
);
this.addSettingTab(new FilePublisherTab(this.app, this));
}
onunload() {}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
const TEthunk = <A>(f: Lazy<Promise<A>>) => TE.tryCatch(f, E.toError);
const publishFile =
(vault: Vault, url: string, token: string) => (file: TFile) =>
pipe(
TE.right({ fileName: file.name }),
TE.bind("file", () => TEthunk(() => vault.read(file))),
TE.chain((data) =>
TEthunk(() =>
axios.post(url, data, {
headers: {
"Content-Type": "multipart/form-data",
Authorization: "Basic " + token,
},
})
)
),
TE.chain(() => TE.right(file))
);
const log =
(msg: string) =>
<A>(a: A) => {
console.log(msg);
return a;
};
const moveFile = (vault: Vault) => (file: TAbstractFile) =>
file.path.contains(PUBLISHED_DIR)
? TE.right(file)
: pipe(
file.path,
Str.split("/"),
(parts) => O.some(parts),
O.chain((parts) =>
pipe(parts, RA.insertAt(parts.length - 1, PUBLISHED_DIR))
),
TE.fromOption(() => new Error("unable to build file path")),
TE.bindTo("parts"),
TE.let("base", ({ parts }) => parts.slice(0, -1).join("/")),
TE.let("path", ({ parts }) => parts.join("/")),
TE.bind("created", ({ base }) =>
pipe(
pipe(
TEthunk(() => vault.createFolder(base)),
TE.match(
() => base,
() => base
)
),
TE.fromTask
)
),
TE.chain(({ path }) => TEthunk(() => vault.rename(file, path))),
TE.chain(() => TE.right(file))
);
const notify = (e: Error | undefined, msg: string) => {
console.log(msg);
if (e) {
console.error(e);
}
new Notice(msg);
};
class FilePublisherTab extends PluginSettingTab {
plugin: FilePublisher;
constructor(app: App, plugin: FilePublisher) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h3", { text: "File Publisher Settings" });
new Setting(containerEl)
.setName("Publisher url")
.setDesc(
"This should be a POST url where the file is sent as a multipart/form-data body to."
)
.addText((text) =>
text
.setPlaceholder("Enter the url")
.setValue(this.plugin.settings.url)
.onChange(async (value) => {
this.plugin.settings.url = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("API Key")
.setDesc(
"API Key used when posting a file to the URL. NOTE: this is passed as the user of the basic authorization header."
)
.addText((text) =>
text
.setPlaceholder("Enter your key")
.setValue(this.plugin.settings.apiKey)
.onChange(async (value) => {
this.plugin.settings.apiKey = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("API Secret")
.setDesc(
"API Secret used when posting a file to the URL. NOTE: this is passed as the password of the basic authorization header."
)
.addText((text) =>
text
.setPlaceholder("Enter your secret")
.setValue(this.plugin.settings.apiSecret)
.onChange(async (value) => {
this.plugin.settings.apiSecret = value;
await this.plugin.saveSettings();
})
);
}
}