Skip to content

Commit 890b110

Browse files
authored
Merge pull request #1523 from solid/recursive-create-websocket
Triggers websocket when a folder is recursively created
2 parents dd47499 + 463057f commit 890b110

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

lib/handlers/patch.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ module.exports = handler
44

55
const bodyParser = require('body-parser')
66
const fs = require('fs')
7-
const fx = require('mkdir-recursive')
7+
const mkdirp = require('fs-extra').mkdirp
88
const debug = require('../debug').handlers
99
const error = require('../http-error')
1010
const $rdf = require('rdflib')
1111
const crypto = require('crypto')
1212
const overQuota = require('../utils').overQuota
1313
const getContentType = require('../utils').getContentType
1414
const withLock = require('../lock')
15+
const { promisify } = require('util')
16+
const pathUtility = require('path')
1517

1618
// Patch parsers by request body content type
1719
const PATCH_PARSERS = {
@@ -60,7 +62,19 @@ async function patchHandler (req, res, next) {
6062
const pathSplit = path.split('/')
6163
const directory = pathSplit.slice(0, pathSplit.length - 1).join('/')
6264
if (!fs.existsSync(directory)) {
63-
fx.mkdirSync(directory, { recursive: true })
65+
const firstDirectoryRecursivelyCreated = await promisify(mkdirp)(directory)
66+
if (ldp.live) {
67+
// Get parent for the directory made
68+
const parentDirectoryPath =
69+
pathUtility.dirname(firstDirectoryRecursivelyCreated) + pathUtility.sep
70+
// Get the url for the parent
71+
const parentDirectoryUrl = (await ldp.resourceMapper.mapFileToUrl({
72+
path: parentDirectoryPath,
73+
hostname: req.hostname
74+
})).url
75+
// Update websockets
76+
ldp.live(new URL(parentDirectoryUrl).pathname)
77+
}
6478
}
6579

6680
// Patch the graph and write it back to the file

lib/ldp.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,10 @@ class LDP {
229229

230230
// First check if we are above quota
231231
let isOverQuota
232+
// Someone had a reason to make url actually a req sometimes but not
233+
// all the time. So now we have to account for that, as done below.
234+
const hostname = typeof url !== 'string' ? url.hostname : URL.parse(url).hostname
232235
try {
233-
const { hostname } = URL.parse(url.url || url)
234236
isOverQuota = await overQuota(this.resourceMapper.resolveFilePath(hostname), this.serverUri)
235237
} catch (err) {
236238
throw error(500, 'Error finding user quota')
@@ -243,7 +245,19 @@ class LDP {
243245
const { path } = await this.resourceMapper.mapUrlToFile({ url, contentType, createIfNotExists: true })
244246
const dirName = dirname(path)
245247
try {
246-
await promisify(mkdirp)(dirName)
248+
const firstDirectoryRecursivelyCreated = await promisify(mkdirp)(dirName)
249+
if (this.live && firstDirectoryRecursivelyCreated) {
250+
// Get parent for the directory made
251+
const parentDirectoryPath =
252+
utilPath.dirname(firstDirectoryRecursivelyCreated) + utilPath.sep
253+
// Get the url for the parent
254+
const parentDirectoryUrl = (await this.resourceMapper.mapFileToUrl({
255+
path: parentDirectoryPath,
256+
hostname
257+
})).url
258+
// Update websockets
259+
this.live(URL.parse(parentDirectoryUrl).pathname)
260+
}
247261
} catch (err) {
248262
debug.handlers('PUT -- Error creating directory: ' + err)
249263
throw error(err,

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
"li": "^1.3.0",
8787
"mashlib": "^1.6.6",
8888
"mime-types": "^2.1.27",
89-
"mkdir-recursive": "^0.4.0",
9089
"negotiator": "^0.6.2",
9190
"node-fetch": "^2.6.1",
9291
"node-forge": "^0.10.0",

0 commit comments

Comments
 (0)