Skip to content

Commit

Permalink
Added PUT method
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 13, 2017
1 parent 5422148 commit 3f11227
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 92 deletions.
52 changes: 0 additions & 52 deletions lib/resource/Resource.1.d.ts

This file was deleted.

34 changes: 0 additions & 34 deletions lib/resource/Resource.1.js

This file was deleted.

2 changes: 2 additions & 0 deletions lib/server/commands/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
var NotImplemented_1 = require("./NotImplemented");
var Propfind_1 = require("./Propfind");
var Mkcol_1 = require("./Mkcol");
var Put_1 = require("./Put");
var Get_1 = require("./Get");
exports.default = {
NotImplemented: NotImplemented_1.default,
Propfind: Propfind_1.default,
Mkcol: Mkcol_1.default,
Put: Put_1.default,
Get: Get_1.default
};
2 changes: 0 additions & 2 deletions lib/server/commands/Post.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions lib/server/commands/Post.js

This file was deleted.

2 changes: 2 additions & 0 deletions lib/server/commands/Put.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { MethodCallArgs } from '../WebDAVRequest';
export default function (arg: MethodCallArgs, callback: any): void;
78 changes: 78 additions & 0 deletions lib/server/commands/Put.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var Resource_1 = require("../../resource/Resource");
var path = require("path");
function createResource(arg, callback, validCallback) {
arg.server.getResourceFromPath(arg.path.getParent(), function (e, r) {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
callback();
return;
}
if (!r.fsManager) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
var resource = r.fsManager.newResource(arg.uri, path.basename(arg.uri), Resource_1.ResourceType.File, r);
resource.create(function (e) {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
r.addChild(resource, function (e) {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
}
else
validCallback(resource);
});
});
});
}
function default_1(arg, callback) {
arg.getResource(function (e, r) {
if (arg.contentLength === 0) {
if (r) {
r.write(new Buffer(0), function (e) {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
callback();
});
return;
}
createResource(arg, callback, function (r) {
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
callback();
});
}
else {
var data = new Buffer(arg.data);
if (e) {
createResource(arg, callback, function (r) {
r.write(data, function (e) {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
callback();
});
});
return;
}
r.write(data, function (e) {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
callback();
});
}
});
}
exports.default = default_1;
2 changes: 2 additions & 0 deletions src/server/commands/Commands.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import NotImplemented from './NotImplemented'
import Propfind from './Propfind'
import Mkcol from './Mkcol'
import Put from './Put'
import Get from './Get'

export default {
NotImplemented,
Propfind,
Mkcol,
Put,
Get
}
94 changes: 94 additions & 0 deletions src/server/commands/Put.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { HTTPCodes, MethodCallArgs, WebDAVRequest } from '../WebDAVRequest'
import { IResource, ResourceType } from '../../resource/Resource'
import * as path from 'path'

function createResource(arg : MethodCallArgs, callback, validCallback : (resource : IResource) => void)
{
arg.server.getResourceFromPath(arg.path.getParent(), (e, r) => {
if(e)
{
arg.setCode(HTTPCodes.MethodNotAllowed)
callback()
return;
}

if(!r.fsManager)
{
arg.setCode(HTTPCodes.InternalServerError)
callback();
return;
}

const resource = r.fsManager.newResource(arg.uri, path.basename(arg.uri), ResourceType.File, r);
resource.create((e) => {
if(e)
{
arg.setCode(HTTPCodes.InternalServerError)
callback();
return;
}

r.addChild(resource, (e) => {
if(e)
{
arg.setCode(HTTPCodes.InternalServerError)
callback();
}
else
validCallback(resource);
})
})
})
}

export default function(arg : MethodCallArgs, callback)
{

arg.getResource((e, r) => {
if(arg.contentLength === 0)
{ // Create file
if(r)
{ // Resource exists => empty it
r.write(new Buffer(0), (e) => {
if(e)
arg.setCode(HTTPCodes.InternalServerError)
else
arg.setCode(HTTPCodes.OK)
callback()
})
return;
}

createResource(arg, callback, (r) => {
arg.setCode(HTTPCodes.OK)
callback();
})
}
else
{ // Write to a file
var data = new Buffer(arg.data);

if(e)
{ // Resource not found
createResource(arg, callback, (r) => {
r.write(data, (e) => {
if(e)
arg.setCode(HTTPCodes.InternalServerError)
else
arg.setCode(HTTPCodes.OK)
callback();
})
})
return;
}

r.write(data, (e) => {
if(e)
arg.setCode(HTTPCodes.InternalServerError)
else
arg.setCode(HTTPCodes.OK)
callback();
})
}
})
}

0 comments on commit 3f11227

Please sign in to comment.