(graph, resource, root, serverUri)
| 198 | |
| 199 | // Writes the RDF graph to the given resource |
| 200 | function writeGraph (graph, resource, root, serverUri) { |
| 201 | debug('PATCH -- Writing patched file') |
| 202 | return new Promise((resolve, reject) => { |
| 203 | const resourceSym = graph.sym(resource.url) |
| 204 | |
| 205 | function doWrite (serialized) { |
| 206 | // First check if we are above quota |
| 207 | overQuota(root, serverUri).then((isOverQuota) => { |
| 208 | if (isOverQuota) { |
| 209 | return reject(error(413, |
| 210 | 'User has exceeded their storage quota')) |
| 211 | } |
| 212 | |
| 213 | fs.writeFile(resource.path, serialized, { encoding: 'utf8' }, function (err) { |
| 214 | if (err) { |
| 215 | return reject(error(500, `Failed to write file after patch: ${err}`)) |
| 216 | } |
| 217 | debug('PATCH -- applied successfully') |
| 218 | resolve('Patch applied successfully.\n') |
| 219 | }) |
| 220 | }).catch(() => reject(error(500, 'Error finding user quota'))) |
| 221 | } |
| 222 | |
| 223 | if (resource.contentType === 'application/ld+json') { |
| 224 | $rdf.serialize(resourceSym, graph, resource.url, resource.contentType, function (err, result) { |
| 225 | if (err) return reject(error(500, `Failed to serialize after patch: ${err}`)) |
| 226 | doWrite(result) |
| 227 | }) |
| 228 | } else { |
| 229 | const serialized = $rdf.serialize(resourceSym, graph, resource.url, resource.contentType) |
| 230 | debug(`PATCH -- Serialized graph:\n${serialized}`) |
| 231 | doWrite(serialized) |
| 232 | } |
| 233 | }) |
| 234 | } |
| 235 | |
| 236 | // Creates a hash of the given text |
| 237 | function hash (text) { |
no test coverage detected