Skip to content

Instantly share code, notes, and snippets.

@brandon-beacher
Created August 4, 2009 13:54
Show Gist options
  • Select an option

  • Save brandon-beacher/161238 to your computer and use it in GitHub Desktop.

Select an option

Save brandon-beacher/161238 to your computer and use it in GitHub Desktop.
system.use("com.github.brandon-beacher.smart-library.com.joyent.Sammy");
system.use("com.joyent.Resource");
system.use("com.stevenlevithan.parseUri");
system.use("org.json.json2");
system.use("resources.DavFile");
system.use("resources.DavFolder");
GET("/dav-files", function () {
this.davFiles = DavFile.search({});
return template("DavFiles/index.html");
});
GET("/dav-folders", function () {
this.davFolders = DavFolder.search({});
this.rootFolder = DavFolder.root();
return template("DavFolders/index.html");
});
POST("/dav-folders", function() {
this.davFolder = new DavFolder();
this.davFolder.parentId = this.request.body.parentId;
this.davFolder.name = this.request.body.name;
this.davFolder.save();
return redirect("/dav-folders/" + this.davFolder.id);
});
GET("/dav-folders/new", function () {
this.parentId = this.request.query.parentId;
return template("DavFolders/new.html");
});
OPTIONS(/.*/, function() {
this.response.headers["Allow"] = "GET, PUT, DELETE, MKCOL, OPTIONS, COPY, MOVE, PROPFIND, PROPPATCH, HEAD";
this.response.headers["DAV"] = "1";
this.response.headers["MS-Author-Via"] = "DAV";
throw this.response;
});
PROPFIND(/.*/, function() {
var request_content = new XML(this.request.content.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, ""));
var response_content = <D:multistatus xmlns:D="DAV:">
<D:response xmlns:D="DAV:">
<D:href>/</D:href>
<D:propstat>
<D:prop>
<D:getlastmodified xmlns:D="DAV:">2009-08-02T19:46:46Z</D:getlastmodified>
<D:getcontentlength xmlns:D="DAV:">0</D:getcontentlength>
<D:resourcetype xmlns:D="DAV:"><D:collection/></D:resourcetype>
</D:prop>
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>
</D:multistatus>;
this.response.code = 207;
this.response.mime = "text/xml";
return response_content.toString();
});
PUT(/.*/, function() {
var uri = parseUri(this.request.uri);
var fileName = uri.file;
var folderNames = uri.directory.split("/").filter(function(folderName) { return folderName.length > 0 });
if (!fileName) {
this.response.code = 405;
throw this.response;
}
var davFolder = DavFolder.root();
folderNames.forEach(function(folderName) {
var davFolders = DavFolder.search({ parentId: davFolder.id, name: folderName });
switch (davFolders.length) {
case 0:
this.response.code = 409;
throw this.response;
break;
case 1:
davFolder = davFolders[0];
break;
default:
throw "Got an unexpected number of folders.";
}
});
var davFiles = DavFile.search([{ davFolderId: davFolder.id, name: fileName }]);
switch (davFiles.length) {
case 0:
// creating a new DavFile
var davFile = new DavFile();
davFile.name = fileName;
davFile.davFolderId = davFolder.id;
davFile.content = this.request.content;
davFile.save();
this.response = 201;
throw this.response;
break;
case 1:
// updating an existing DavFile
var davFile = davFiles[0];
davFile.content = this.request.content;
davFile.save();
this.response = 201;
throw this.response;
break;
default:
throw "Got an unexpected number of files.";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment