Skip to content

Commit 4cac168

Browse files
authored
Merge pull request #1164 from solid/fix/#1162
Fixed folders not being created recursively
2 parents 273f6b5 + 4933982 commit 4cac168

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

lib/handlers/patch.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = handler
44

55
const bodyParser = require('body-parser')
66
const fs = require('fs')
7+
const fx = require('mkdir-recursive')
78
const debug = require('../debug').handlers
89
const error = require('../http-error')
910
const $rdf = require('rdflib')
@@ -55,6 +56,13 @@ async function patchHandler (req, res, next) {
5556
const patchObject = await parsePatch(url, patch.uri, patch.text)
5657
await checkPermission(req, patchObject)
5758

59+
// Check to see if folder exists
60+
const pathSplit = path.split('/')
61+
const directory = pathSplit.slice(0, pathSplit.length - 1).join('/')
62+
if (!fs.existsSync(directory)) {
63+
fx.mkdirSync(directory, { recursive: true })
64+
}
65+
5866
// Patch the graph and write it back to the file
5967
const result = await withLock(path, { mustExist: false }, async () => {
6068
const graph = await readGraph(resource)

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"li": "^1.0.1",
8686
"mashlib": "^0.7.18",
8787
"mime-types": "^2.1.11",
88+
"mkdir-recursive": "^0.4.0",
8889
"negotiator": "^0.6.0",
8990
"node-fetch": "^2.1.2",
9091
"node-forge": "^0.7.4",

test/integration/patch-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ describe('PATCH', () => {
112112
text: 'Patch applied successfully',
113113
result: '@prefix : </write-only.ttl#>.\n@prefix tim: </>.\n\ntim:a tim:b tim:c.\n\ntim:d tim:e tim:f.\n\ntim:x tim:y tim:z.\n\n'
114114
}))
115+
116+
describe('on a resource with parent folders that do not exist', describePatch({
117+
path: '/folder/cool.ttl',
118+
exists: false,
119+
patch: `<> solid:patches <https://tim.localhost:7777/folder/cool.ttl>;
120+
solid:inserts { <x> <y> <z>. }.`
121+
}, {
122+
status: 200,
123+
text: 'Patch applied successfully',
124+
result: '@prefix : <#>.\n@prefix fol: <./>.\n\nfol:x fol:y fol:z.\n\n'
125+
}))
115126
})
116127

117128
describe('with insert and where', () => {

0 commit comments

Comments
 (0)