|
| 1 | +var fs = require('fs'); |
| 2 | +var $rdf = require('rdflib'); |
| 3 | +var path = require('path'); |
| 4 | +var uuid = require('node-uuid'); |
| 5 | + |
| 6 | +var logging = require('./logging.js'); |
| 7 | +var metadata = require('./metadata.js'); |
| 8 | +var options = require('./options.js'); |
| 9 | + |
| 10 | +var rdfVocab = require('./vocab/rdf.js'); |
| 11 | +var ldpVocab = require('./vocab/ldp.js'); |
| 12 | + |
| 13 | +var addUriTriple = function(kb, s, o, p) { |
| 14 | + kb.add(kb.sym(s), kb.sym(o), kb.sym(p)); |
| 15 | +}; |
| 16 | + |
| 17 | +var usedURIs = {}; |
| 18 | + |
| 19 | +module.exports.createRootContainer = function() { |
| 20 | + if (!metadata.hasMetadata(options.fileBase)) { |
| 21 | + logging.log("Creating root metadata"); |
| 22 | + var rootMetadata = new metadata.Metadata(); |
| 23 | + rootMetadata.filename = options.fileBase; |
| 24 | + rootMetadata.isResource = true; |
| 25 | + rootMetadata.isContainer = true; |
| 26 | + rootMetadata.isSourceResource = true; |
| 27 | + rootMetadata.isBasicContainer = true; |
| 28 | + metadata.writeMetadata(options.fileBase, rootMetadata, |
| 29 | + writeCallback); |
| 30 | + } |
| 31 | + //TODO handle case when .container file does not exist |
| 32 | + |
| 33 | + function writeCallback(err) { |
| 34 | + logging.log(options.pathStart); |
| 35 | + if (err) { |
| 36 | + process.exit(1); |
| 37 | + } else if (!metadata.hasContainerMetadata(options.fileBase)) { |
| 38 | + var rootContainer = $rdf.graph(); |
| 39 | + addUriTriple(rootContainer, options.pathStart, rdfVocab.type, |
| 40 | + ldpVocab.Resource); |
| 41 | + addUriTriple(rootContainer, options.pathStart, rdfVocab.type, |
| 42 | + ldpVocab.RDFSource); |
| 43 | + addUriTriple(rootContainer, options.pathStart, rdfVocab.type, |
| 44 | + ldpVocab.Container); |
| 45 | + addUriTriple(rootContainer, options.pathStart, rdfVocab.type, |
| 46 | + ldpVocab.BasicContainer); |
| 47 | + rootContainer.add(rootContainer.sym(options.pathStart), |
| 48 | + rootContainer.sym('http://purl.org/dc/terms/title'), |
| 49 | + '"Root Container"'); |
| 50 | + var serializedContainer = $rdf.serialize(undefined, rootContainer, |
| 51 | + options.pathStart, 'text/turtle'); |
| 52 | + logging.log("Root container: ", serializedContainer); |
| 53 | + metadata.writeContainerMetadata(options.fileBase, |
| 54 | + serializedContainer, function(err) { |
| 55 | + if (err) { |
| 56 | + //TODO handle error |
| 57 | + logging.log("Could not write root container"); |
| 58 | + } else { |
| 59 | + logging.log("Wrote root container to " + options.fileBase); |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +module.exports.createNewContainer = function(container, type, callback) { |
| 67 | + fs.mkdir(container, function(err) { |
| 68 | + if (err) { |
| 69 | + this.releaseResourceUri(container); |
| 70 | + callback(err); |
| 71 | + } else { |
| 72 | + var containerMetadata = new metadata.Metadata(); |
| 73 | + containerMetadata.filename = container; |
| 74 | + containerMetadata.isResource = true; |
| 75 | + containerMetadata.isContainer = true; |
| 76 | + containerMetadata.isSourceResource = true; |
| 77 | + if (type === ldpVocab.BasicContainer) |
| 78 | + containerMetadata.isBasicContainer = true; |
| 79 | + if (type === ldpVocab.DirectContainer) |
| 80 | + containerMetadata.isDirectContainer = true; |
| 81 | + metadata.writeMetadata(options.fileBase, rootMetadata, writeCallback); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + function writeCallback(err) { |
| 86 | + if (err) { |
| 87 | + this.releaseResourceUri(container); |
| 88 | + callback(err); |
| 89 | + } else { |
| 90 | + var newContainer = $rdf.graph(); |
| 91 | + addUriTriple(newContainer, options.pathStart, rdfVocab.type, |
| 92 | + ldpVocab.Resource); |
| 93 | + addUriTriple(newContainer, options.pathStart, rdfVocab.type, |
| 94 | + ldpVocab.RDFSource); |
| 95 | + addUriTriple(newContainer, options.pathStart, rdfVocab.type, |
| 96 | + ldpVocab.Container); |
| 97 | + if (type === ldpVocab.BasicContainer) |
| 98 | + addUriTriple(newContainer, options.pathStart, rdfVocab.type, |
| 99 | + ldpVocab.BasicContainer); |
| 100 | + else if (type === ldpVocab.DirectContainer) |
| 101 | + addUriTriple(newContainer, options.pathStart, rdfVocab.type, |
| 102 | + ldpVocab.DirectContainer); |
| 103 | + |
| 104 | + newContainer.add(newContainer.sym(options.pathStart), |
| 105 | + newContainer.sym('http://purl.org/dc/terms/title'), |
| 106 | + container); |
| 107 | + var serializedContainer = $rdf.serialize(undefined, newContainer, |
| 108 | + container, 'text/turtle'); |
| 109 | + metadata.writeContainerMetadata(container, serializedContainer, |
| 110 | + function(err) { |
| 111 | + this.releaseResourceUri(container); |
| 112 | + if (err) { |
| 113 | + callback(err); |
| 114 | + } else { |
| 115 | + logging.log("Wrote new container"); |
| 116 | + callback(err); |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +}; |
| 123 | + |
| 124 | +module.exports.createResourceUri = function(containerURI, slug) { |
| 125 | + var newPath; |
| 126 | + if (slug) { |
| 127 | + newPath = path.join(containerURI, slug); |
| 128 | + } else { |
| 129 | + newPath = path.join(containerURI, uuid.v1()); |
| 130 | + } |
| 131 | + if (!(fs.existsSync(newPath) || containerURI in usedURIs)) { |
| 132 | + usedURIs[newPath] = true; |
| 133 | + } else { |
| 134 | + return null; |
| 135 | + } |
| 136 | + return newPath; |
| 137 | +}; |
| 138 | + |
| 139 | +module.exports.releaseResourceUri = function(uri) { |
| 140 | + delete usedURIs[uri]; |
| 141 | +}; |
| 142 | + |
| 143 | +module.exports.verify = function(containerGraph, type) { |
| 144 | + //TODO work on this method |
| 145 | + var results = containerGraph.each(undefined, "a", type); |
| 146 | + if (results.length === 1) { |
| 147 | + return true; |
| 148 | + } else { |
| 149 | + return false; |
| 150 | + } |
| 151 | +}; |
| 152 | + |
| 153 | +module.exports.verifyDirectContainer = function(containerGraph) { |
| 154 | + |
| 155 | +}; |
| 156 | + |
| 157 | +module.exports.createNewResource = function(containerPath, containerGraph, |
| 158 | + resourcePath, resourceGraph, resourceMetadata, callback) { |
| 159 | + var containerURI = path.relative(options.fileBase, containerPath); |
| 160 | + var resourceURI = path.relative(options.fileBase, resourcePath); |
| 161 | + //TODO replace url with resource url |
| 162 | + var rawResource = $rdf.serialize(undefined, |
| 163 | + resourceGraph, options.baseUri + resourceURI, 'text/turtle'); |
| 164 | + logging.log("Writing new resource to ", resourcePath); |
| 165 | + logging.log(rawResource); |
| 166 | + fs.writeFile(resourcePath, rawResource, writeResourceCallback); |
| 167 | + |
| 168 | + function writeResourceCallback(err) { |
| 169 | + if (err) { |
| 170 | + container.releaseResourceUri(resoucePath); |
| 171 | + callback(err); |
| 172 | + } else { |
| 173 | + addUriTriple(containerGraph, containerURI, ldpVocab.contains, |
| 174 | + resourceURI); |
| 175 | + var rawContainer = $rdf.serialize(undefined, containerGraph, |
| 176 | + options.uriBase, 'text/turtle'); |
| 177 | + metadata.writeContainerMetadata(containerPath, rawContainer, |
| 178 | + writeContainerCallback); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + function writeContainerCallback(err) { |
| 183 | + if (err) { |
| 184 | + module.exports.releaseResourceUri(resourcePath); |
| 185 | + return callback(err); |
| 186 | + } else { |
| 187 | + metadata.writeMetadata(resourcePath, resourceMetadata, |
| 188 | + writeMetadataCallback); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + function writeMetadataCallback(err) { |
| 193 | + module.exports.releaseResourceUri(resourcePath); |
| 194 | + return callback(err); |
| 195 | + } |
| 196 | +}; |
| 197 | + |
| 198 | +module.exports.createNewContainer = function(containerPath, containerGraph, |
| 199 | + containerMetadata, callback) { |
| 200 | + fs.mkdir(containerPath, mkdirCallback); |
| 201 | + |
| 202 | + function mkdirCallback(err) { |
| 203 | + if (err) { |
| 204 | + module.exports.releaseResourceUri(containerPath); |
| 205 | + return callback(err); |
| 206 | + } else { |
| 207 | + var rawContainer = $rdf.serialize(undefined, containerGraph, |
| 208 | + options.uriBase, 'text/turtle'); |
| 209 | + metadata.writeContainerMetadata(containerPath, rawContainer, |
| 210 | + writeContainerCallback); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + function writeContainerCallback(err) { |
| 215 | + if (err) { |
| 216 | + module.exports.releaseResourceUri(containerPath); |
| 217 | + return callback(err); |
| 218 | + } else { |
| 219 | + metadata.writeMetadata(containerPath, containerMetadata, |
| 220 | + writeMetadataCallback); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + function writeMetadataCallback(err) { |
| 225 | + module.exports.releaseResourceUri(containerPath); |
| 226 | + return callback(err); |
| 227 | + } |
| 228 | +}; |
0 commit comments