forked from OpenNeuroOrg/openneuro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadme.ts
More file actions
41 lines (38 loc) · 1.31 KB
/
Copy pathreadme.ts
File metadata and controls
41 lines (38 loc) · 1.31 KB
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
import { addFileString, commitFiles } from "./dataset"
import { getRedis } from "../libs/redis"
import CacheItem, { CacheType } from "../cache/item"
import { getDatasetWorker } from "../libs/datalad-service"
import { datasetOrSnapshot } from "../utils/datasetOrSnapshot"
export const readmeUrl = (datasetId, revision) => {
return `http://${
getDatasetWorker(
datasetId,
)
}/datasets/${datasetId}/snapshots/${revision}/files/README`
}
export const readme = (obj) => {
const { datasetId, revision } = datasetOrSnapshot(obj)
const cache = new CacheItem(getRedis(), CacheType.readme, [
datasetId,
revision.substring(0, 7),
])
return cache.get(async () => {
/** Why are we using fetch here instead of superagent?
* The backend guesses wrong at the MIME type so fetch lets us get the string
* without messing around with superagent parsers
*
* We may want to use less superagent anyways just to avoid library weight
*/
const readmeReq = await fetch(readmeUrl(datasetId, revision))
if (readmeReq.status === 200) {
return await readmeReq.text()
} else {
return null
}
})
}
export const setReadme = (datasetId, readme, filename, user) => {
return addFileString(datasetId, filename, "text/plain", readme).then(() =>
commitFiles(datasetId, user)
)
}