forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldp.js
More file actions
208 lines (173 loc) · 5.41 KB
/
Copy pathldp.js
File metadata and controls
208 lines (173 loc) · 5.41 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
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
import fs from 'fs/promises'
import { createReadStream, existsSync } from 'fs'
import path from 'path'
import { v4 as uuid } from 'uuid'
import { HttpError } from './error.js'
import { isAuxiliary, debugLDP as debug } from './utils.js'
import * as rdf from './rdf.js'
export class LDP {
constructor ({ rootPath, rootUrl, mapper }) {
this.rootPath = rootPath
this.rootUrl = rootUrl
this.mapper = mapper
}
resolve (pathname) {
return this.mapper.urlToFilePath(this.rootUrl + pathname)
}
resourceUrl (pathname) {
return this.rootUrl + pathname
}
async exists (pathname) {
const filePath = this.resolve(pathname)
try {
await fs.access(filePath)
return true
} catch {
return false
}
}
async stat (pathname) {
return fs.stat(this.resolve(pathname))
}
async isContainer (pathname) {
try {
const stat = await fs.stat(this.resolve(pathname))
return stat.isDirectory()
} catch {
return false
}
}
async get (pathname) {
const filePath = this.resolve(pathname)
const stat = await fs.stat(filePath).catch(() => {
throw new HttpError(404)
})
if (stat.isDirectory()) {
return this.getContainer(pathname, filePath, stat)
}
return {
stream: createReadStream(filePath),
contentType: this.mapper.getContentType(filePath),
stat,
isContainer: false
}
}
async getContainer (pathname, filePath, stat) {
if (!pathname.endsWith('/')) {
pathname += '/'
}
const dirEntries = await fs.readdir(filePath, { withFileTypes: true })
const entries = dirEntries
.filter(e => !e.name.startsWith('.'))
.map(e => ({
name: e.name,
isDir: e.isDirectory()
}))
for (const entry of entries) {
try {
entry.stat = await fs.stat(path.join(filePath, entry.name))
} catch { /* ignore */ }
}
const containerUri = this.resourceUrl(pathname)
const turtle = await rdf.containerListing(containerUri, entries, stat)
return {
body: turtle,
contentType: 'text/turtle',
stat,
isContainer: true
}
}
async put (pathname, body, contentType) {
const filePath = this.resolve(pathname)
if (!contentType) {
throw new HttpError(400, 'Content-Type is required')
}
if (isAuxiliary(pathname) && contentType !== 'text/turtle') {
if (pathname.endsWith('.acl')) {
throw new HttpError(415, 'ACL resources must be text/turtle')
}
}
if (pathname.endsWith('.acl')) {
try {
rdf.parse(body, this.resourceUrl(pathname), 'text/turtle')
} catch (e) {
throw new HttpError(400, 'Invalid Turtle in ACL: ' + e.message)
}
}
const existed = existsSync(filePath)
await fs.mkdir(path.dirname(filePath), { recursive: true })
await fs.writeFile(filePath, body)
return { status: existed ? 204 : 201 }
}
async post (pathname, body, contentType, slug, linkType) {
const filePath = this.resolve(pathname)
const stat = await fs.stat(filePath).catch(() => {
throw new HttpError(404)
})
if (!stat.isDirectory()) {
throw new HttpError(405, 'POST only allowed on containers')
}
const isContainer = linkType && linkType.includes('BasicContainer')
let name = slug || uuid()
if (isAuxiliary(name)) {
throw new HttpError(403, 'Cannot POST auxiliary resources')
}
if (!isContainer && contentType && !slug) {
const ext = this.mapper.extensionForType(contentType)
if (ext && !name.endsWith(ext)) {
name += ext
}
} else if (!isContainer && contentType && slug) {
const ext = this.mapper.extensionForType(contentType)
const slugExt = path.extname(slug)
if (ext && !slugExt) {
name += ext
}
}
let targetPath = path.join(filePath, name)
if (existsSync(targetPath)) {
const base = path.basename(name, path.extname(name))
const ext = path.extname(name)
name = base + '-' + uuid().slice(0, 8) + ext
targetPath = path.join(filePath, name)
}
if (isContainer) {
await fs.mkdir(targetPath, { recursive: true })
} else {
await fs.writeFile(targetPath, body || '')
}
const resourcePathname = pathname.endsWith('/') ? pathname + name : pathname + '/' + name
const location = this.resourceUrl(resourcePathname + (isContainer ? '/' : ''))
return { status: 201, location }
}
async delete (pathname) {
if (pathname === '/' || pathname === '') {
throw new HttpError(405, 'Cannot delete root')
}
if (pathname === '/.acl') {
throw new HttpError(405, 'Cannot delete root ACL')
}
const filePath = this.resolve(pathname)
const stat = await fs.stat(filePath).catch(() => {
throw new HttpError(404)
})
if (stat.isDirectory()) {
const entries = await fs.readdir(filePath)
const meaningful = entries.filter(e => e !== '.acl' && e !== '.meta' && e !== '.meta.acl')
if (meaningful.length > 0) {
throw new HttpError(409, 'Container is not empty')
}
for (const e of entries) {
await fs.unlink(path.join(filePath, e)).catch(() => {})
}
await fs.rmdir(filePath)
} else {
await fs.unlink(filePath)
const aclPath = filePath + '.acl'
const metaPath = filePath + '.meta'
await fs.unlink(aclPath).catch(() => {})
await fs.unlink(metaPath).catch(() => {})
}
return { status: 200 }
}
}