(url, stream, contentType)
| 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() |
no test coverage detected