MCPcopy Index your code
hub / github.com/nodeSolidServer/node-solid-server / put

Method put

lib/ldp.mjs:213–273  ·  view source on GitHub ↗
(url, stream, contentType)

Source from the content-addressed store, hash-verified

211 }
212
213 async put (url, stream, contentType) {
214 const container = (url.url || url).endsWith('/')
215 // PUT without content type is forbidden, unless PUTting container
216 if (!contentType && !container) {
217 throw error(400,
218 'PUT request requires a content-type via the Content-Type header')
219 }
220 // reject resource with percent-encoded $ extension
221 const dollarExtensionRegex = /%(?:24)\.[^%(?:24)]*$/
222 if ((url.url || url).match(dollarExtensionRegex)) {
223 throw error(400, 'Resource with a $.ext is not allowed by the server')
224 }
225 // First check if we are above quota
226 let isOverQuota
227 // Someone had a reason to make url actually a req sometimes but not
228 // all the time. So now we have to account for that, as done below.
229 const hostname = typeof url !== 'string' ? url.hostname : urlModule.parse(url).hostname
230 try {
231 isOverQuota = await overQuota(this.resourceMapper.resolveFilePath(hostname), this.serverUri)
232 } catch (err) {
233 throw error(500, 'Error finding user quota')
234 }
235 if (isOverQuota) {
236 throw error(413, 'User has exceeded their storage quota')
237 }
238 // Set url using folder/.meta
239 let { path } = await this.resourceMapper.mapUrlToFile({
240 url,
241 contentType,
242 createIfNotExists: true,
243 searchIndex: false
244 })
245
246 if (container) { path += suffixMeta }
247 // check if file exists, and in that case that it has the same extension
248 if (!container) { await this.checkFileExtension(url, path) }
249 // Create the enclosing directory, if necessary, do not create pubsub if PUT create container
250 await this.createDirectory(path, hostname, !container)
251 // clear cache
252 if (path.endsWith(this.suffixAcl)) {
253 const { url: aclUrl } = await this.resourceMapper.mapFileToUrl({ path, hostname })
254 clearAclCache(aclUrl)
255 // clearAclCache()
256 }
257 // Directory created, now write the file
258 return withLock(path, () => new Promise((resolve, reject) => {
259 // HACK: the middleware in webid-oidc.js uses body-parser, thus ending the stream of data
260 // for JSON bodies. So, the stream needs to be reset
261 if (contentType && contentType.includes && contentType.includes('application/json')) {
262 stream = intoStream(JSON.stringify(stream.body))
263 }
264 const file = stream.pipe(fs.createWriteStream(path))
265 file.on('error', function () {
266 reject(error(500, 'Error writing data'))
267 })
268 file.on('finish', function () {
269 debug.handlers('PUT -- Wrote data to: ' + path)
270 resolve()

Callers 12

putGraphMethod · 0.95
postMethod · 0.80
LdpMiddlewareFunction · 0.80
saveProfileGraphMethod · 0.80
putStreamFunction · 0.80
multiFunction · 0.80
ldp-test.mjsFile · 0.80
acl-tls-test.mjsFile · 0.80
acl-oidc-test.mjsFile · 0.80
createTestResourceFunction · 0.80
http-test.mjsFile · 0.80

Calls 8

checkFileExtensionMethod · 0.95
createDirectoryMethod · 0.95
overQuotaFunction · 0.90
clearAclCacheFunction · 0.90
withLockFunction · 0.85
resolveFilePathMethod · 0.80
mapUrlToFileMethod · 0.80
mapFileToUrlMethod · 0.80

Tested by

no test coverage detected