Skip to content

Commit 3b690a4

Browse files
committed
Triggers websocket when a folder is recursively created
1 parent dd47499 commit 3b690a4

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

lib/create-server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ function createServer (argv, app) {
100100
// Setup Express app
101101
if (ldp.live) {
102102
const solidWs = SolidWs(server, ldpApp)
103+
console.log(solidWs.publish)
103104
ldpApp.locals.ldp.live = solidWs.publish.bind(solidWs)
105+
// setInterval(() => {
106+
// console.log("publishing")
107+
// ldpApp.locals.ldp.live("https://jackson.localhost:8443/profile/card#me")
108+
// }, 1000)
104109
}
105110

106111
return server

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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,12 @@ class LDP {
229229

230230
// First check if we are above quota
231231
let isOverQuota
232+
// For some reason someone thought it was a bright idea to make url actually a
233+
// req sometimes but not all the time 🙄. So Now I have to account for that
234+
const hostname = typeof url !== 'string' ? url.hostname : URL.parse(url).hostname
235+
console.log(url.url)
236+
console.log(hostname)
232237
try {
233-
const { hostname } = URL.parse(url.url || url)
234238
isOverQuota = await overQuota(this.resourceMapper.resolveFilePath(hostname), this.serverUri)
235239
} catch (err) {
236240
throw error(500, 'Error finding user quota')
@@ -243,7 +247,19 @@ class LDP {
243247
const { path } = await this.resourceMapper.mapUrlToFile({ url, contentType, createIfNotExists: true })
244248
const dirName = dirname(path)
245249
try {
246-
await promisify(mkdirp)(dirName)
250+
const firstDirectoryRecursivelyCreated = await promisify(mkdirp)(dirName)
251+
if (this.live && firstDirectoryRecursivelyCreated) {
252+
// Get parent for the directory made
253+
const parentDirectoryPath =
254+
utilPath.dirname(firstDirectoryRecursivelyCreated) + utilPath.sep
255+
// Get the url for the parent
256+
const parentDirectoryUrl = (await this.resourceMapper.mapFileToUrl({
257+
path: parentDirectoryPath,
258+
hostname
259+
})).url
260+
// Update websockets
261+
this.live(URL.parse(parentDirectoryUrl).pathname)
262+
}
247263
} catch (err) {
248264
debug.handlers('PUT -- Error creating directory: ' + err)
249265
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)