-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.js
218 lines (185 loc) · 5.71 KB
/
lib.js
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
/* global fetch */
// TODO
// - decide where to update window.localstorage.{lastCid,ipns,ipnsCid}
// - make function names consistent nounVerb or verbNoun
export async function publish (cid) {
const makeKeyResponse = await fetch('ipns://localhost/?key=ipmb', {
method: 'POST'
})
const ipnsRoot = makeKeyResponse.headers.get('Location')
const publishResponse = await fetch(ipnsRoot, {
method: 'POST',
body: cid
})
if(!publishResponse.ok) throw new Error("Unable to publish: " + await publishResponse.text())
window.localStorage.ipns = ipnsRoot
window.localStorage.ipnsCid = cid
return ipnsRoot
}
export function parseFilename (filename) {
const date = filename.slice(0, 10)
const title = decodeURIComponent(filename.slice(11)).replace(/\.md$/, '')
return { date, title }
}
function excerpt (content) {
let excerpt_ = content.slice(0, 100)
if (excerpt_.indexOf('\n') > 0) {
excerpt_ = excerpt_.slice(0, excerpt_.indexOf('\n') + 1)
}
return excerpt_
}
function parseMeta (file) {
const { date, title } = parseFilename(file.filename)
return {
...file,
title,
date,
excerpt: excerpt(file.content)
}
}
// Create markdown blog based on data stored in lastCid
export async function createBlogIndex (contentUpdateFunction) {
let lastCid = window.localStorage.lastCid
const previousCid = lastCid
// make the update
lastCid = await contentUpdateFunction()
// get content and update index
const files = await _fetchPosts(lastCid)
let indexBody = ''
for (const post of files) {
// this should probably be a relative link
indexBody += `- **[${post.title}](/ipmb-db/${post.filename})** (posted ${post.date}) - ${post.excerpt}\n`
}
if (previousCid) {
indexBody += `\n[previous version of this blog](ipfs://${previousCid}/index.md)`
}
// delete old index.md
let url = `ipfs://${lastCid}/index.md`
let response = await fetch(url, {
method: 'DELETE',
mode: 'cors'
})
if (response.ok) {
const contentUrl = response.headers.get('Location')
lastCid = new URL(contentUrl).host
}
// write index.md
url = `ipfs://${lastCid}/index.md`
response = await fetch(url, {
method: 'PUT',
body: indexBody,
mode: 'cors'
})
const contentUrl = response.headers.get('Location')
lastCid = new URL(contentUrl).host
window.localStorage.lastCid = lastCid
console.log(contentUrl)
return lastCid
}
export async function removeFile (filename) {
let lastCid = window.localStorage.lastCid
const _removeFile = async () => {
const EMPTY_DIRECTORY_CID = 'bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354'
const url = `ipfs://${lastCid || EMPTY_DIRECTORY_CID}/ipmb-db/${filename}`
console.log(`DELETING ${url}`)
const response = await fetch(url, {
method: 'DELETE',
mode: 'cors'
})
const contentUrl = response.headers.get('Location')
const newCid = new URL(contentUrl).host
return newCid
}
lastCid = await createBlogIndex(_removeFile)
return lastCid
}
// Adds a post and regenerates the index
export async function postAdd (file, filename) {
let lastCid = window.localStorage.lastCid
const _addFile = async () => {
const EMPTY_DIRECTORY_CID = 'bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354'
const url = `ipfs://${lastCid || EMPTY_DIRECTORY_CID}/ipmb-db/${filename}`
console.log(`ADDING ${url}`)
const response = await fetch(url, {
method: 'PUT',
body: file,
mode: 'cors'
})
const contentUrl = response.headers.get('Location')
return new URL(contentUrl).host
}
lastCid = await createBlogIndex(_addFile)
return lastCid
}
export async function postUpdate (file, filename, originalFilename) {
let lastCid = window.localStorage.lastCid
const _removeFile = async cid => {
const url = `ipfs://${cid}/ipmb-db/${originalFilename}`
console.log(`DELETING ${url}`)
const response = await fetch(url, {
method: 'DELETE',
mode: 'cors'
})
const contentUrl = response.headers.get('Location')
const newCid = new URL(contentUrl).host
return newCid
}
const _addFile = async cid => {
const url = `ipfs://${cid}/ipmb-db/${filename}`
console.log(`ADDING ${url}`)
const response = await fetch(url, {
method: 'PUT',
body: file,
mode: 'cors'
})
const contentUrl = response.headers.get('Location')
return new URL(contentUrl).host
}
const _chain = async () => {
const cid = await _removeFile(lastCid)
return await _addFile(cid)
}
lastCid = await createBlogIndex(_chain)
return lastCid
}
// Add media
export async function mediaAdd (file) {
const lastCid = window.localStorage.lastCid
const url = `ipfs://${lastCid || ''}/media/${file.name}`
console.log(`ADDING ${url}`)
const response = await fetch(url, {
method: 'PUT',
body: file,
mode: 'cors'
})
const contentUrl = response.headers.get('Location')
window.localStorage.lastCid = new URL(contentUrl).host
return window.localStorage.lastCid
}
async function _fetchPosts (cid) {
const request = await fetch(`ipfs://${cid}/ipmb-db/?noResolve`, {
headers: {
Accept: "application/json"
}
})
let dirList = await request.json()
dirList = dirList.filter(e => !!e) // empty dir returns `[ null ]`
const files = await Promise.all(dirList.map(async filename => {
const fileRequest = await fetch(`ipfs://${cid}/ipmb-db/${filename}`)
const content = await fileRequest.text()
return parseMeta({
filename,
content,
link: `ipfs://${cid}/ipmb-db/${filename}`
})
}))
return files
}
export async function loadContent () {
const lastCid = window.localStorage.lastCid
if (!lastCid) {
return []
}
const files = await _fetchPosts(lastCid)
return files
}