diff --git a/lib/resource/Resource.1.d.ts b/lib/resource/Resource.1.d.ts deleted file mode 100644 index 5789736e..00000000 --- a/lib/resource/Resource.1.d.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { LockKind, Lock } from './lock/Lock'; -import { FSManager, FSPath } from '../manager/FSManager'; -export { PhysicalFile, PhysicalFolder, PhysicalResource } from './physical/PhysicalResource'; -export { VirtualFile, VirtualFolder, VirtualResource } from './virtual/VirtualResource'; -export { Lock, LockBag, LockKind, LockScope, LockType } from './lock/Lock'; -export { StandardResource } from './std/StandardResource'; -export { ResourceChildren } from './std/ResourceChildren'; -export { RootResource } from './std/RootResource'; -export declare type SimpleCallback = (error: Error) => void; -export declare type ReturnCallback = (error: Error, data: T) => void; -export declare type Return2Callback = (error: Error, x: T, y: Q) => void; -export declare class ResourceType { - isFile: boolean; - isDirectory: boolean; - static File: ResourceType; - static Directory: ResourceType; - static Hibrid: ResourceType; - static NoResource: ResourceType; - constructor(isFile: boolean, isDirectory: boolean); -} -export interface IResource { - parent: IResource; - fsManager: FSManager; - create(callback: SimpleCallback): any; - delete(callback: SimpleCallback): any; - moveTo(to: FSPath, callback: Return2Callback): any; - rename(newName: string, callback: Return2Callback): any; - isSame(resource: IResource, callback: ReturnCallback): any; - isOnTheSameFSWith(resource: IResource, callback: ReturnCallback): any; - append(data: Int8Array, callback: SimpleCallback): any; - write(data: Int8Array, callback: SimpleCallback): any; - read(callback: ReturnCallback): any; - mimeType(callback: ReturnCallback): any; - size(callback: ReturnCallback): any; - getLocks(lockKind: LockKind, callback: ReturnCallback): any; - setLock(lock: Lock, callback: SimpleCallback): any; - removeLock(uuid: string, owner: string, callback: ReturnCallback): any; - canLock(lockKind: LockKind, callback: ReturnCallback): any; - getAvailableLocks(callback: ReturnCallback): any; - canRemoveLock(uuid: string, owner: string, callback: ReturnCallback): any; - addChild(resource: IResource, callback: SimpleCallback): any; - removeChild(resource: IResource, callback: SimpleCallback): any; - getChildren(callback: ReturnCallback): any; - setProperty(name: string, value: string, callback: SimpleCallback): any; - getProperty(name: string, callback: ReturnCallback): any; - removeProperty(name: string, callback: SimpleCallback): any; - getProperties(callback: ReturnCallback): any; - creationDate(callback: ReturnCallback): any; - lastModifiedDate(callback: ReturnCallback): any; - webName(callback: ReturnCallback): any; - type(callback: ReturnCallback): any; -} diff --git a/lib/resource/Resource.1.js b/lib/resource/Resource.1.js deleted file mode 100644 index 1da6aae6..00000000 --- a/lib/resource/Resource.1.js +++ /dev/null @@ -1,34 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -var PhysicalResource_1 = require("./physical/PhysicalResource"); -exports.PhysicalFile = PhysicalResource_1.PhysicalFile; -exports.PhysicalFolder = PhysicalResource_1.PhysicalFolder; -exports.PhysicalResource = PhysicalResource_1.PhysicalResource; -var VirtualResource_1 = require("./virtual/VirtualResource"); -exports.VirtualFile = VirtualResource_1.VirtualFile; -exports.VirtualFolder = VirtualResource_1.VirtualFolder; -exports.VirtualResource = VirtualResource_1.VirtualResource; -var Lock_1 = require("./lock/Lock"); -exports.Lock = Lock_1.Lock; -exports.LockBag = Lock_1.LockBag; -exports.LockKind = Lock_1.LockKind; -exports.LockScope = Lock_1.LockScope; -exports.LockType = Lock_1.LockType; -var StandardResource_1 = require("./std/StandardResource"); -exports.StandardResource = StandardResource_1.StandardResource; -var ResourceChildren_1 = require("./std/ResourceChildren"); -exports.ResourceChildren = ResourceChildren_1.ResourceChildren; -var RootResource_1 = require("./std/RootResource"); -exports.RootResource = RootResource_1.RootResource; -var ResourceType = (function () { - function ResourceType(isFile, isDirectory) { - this.isFile = isFile; - this.isDirectory = isDirectory; - } - return ResourceType; -}()); -ResourceType.File = new ResourceType(true, false); -ResourceType.Directory = new ResourceType(false, true); -ResourceType.Hibrid = new ResourceType(true, true); -ResourceType.NoResource = new ResourceType(false, false); -exports.ResourceType = ResourceType; diff --git a/lib/server/commands/Commands.js b/lib/server/commands/Commands.js index fc2c2e23..028cb12d 100644 --- a/lib/server/commands/Commands.js +++ b/lib/server/commands/Commands.js @@ -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 }; diff --git a/lib/server/commands/Post.d.ts b/lib/server/commands/Post.d.ts deleted file mode 100644 index c7eb8dba..00000000 --- a/lib/server/commands/Post.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -import * as Get from './Get'; -export default Get; diff --git a/lib/server/commands/Post.js b/lib/server/commands/Post.js deleted file mode 100644 index 8a4e1efb..00000000 --- a/lib/server/commands/Post.js +++ /dev/null @@ -1,4 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -var Get = require("./Get"); -exports.default = Get; diff --git a/lib/server/commands/Put.d.ts b/lib/server/commands/Put.d.ts new file mode 100644 index 00000000..f8724433 --- /dev/null +++ b/lib/server/commands/Put.d.ts @@ -0,0 +1,2 @@ +import { MethodCallArgs } from '../WebDAVRequest'; +export default function (arg: MethodCallArgs, callback: any): void; diff --git a/lib/server/commands/Put.js b/lib/server/commands/Put.js new file mode 100644 index 00000000..f38741cc --- /dev/null +++ b/lib/server/commands/Put.js @@ -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; diff --git a/src/server/commands/Commands.ts b/src/server/commands/Commands.ts index 8ddf77df..cd2bfe90 100644 --- a/src/server/commands/Commands.ts +++ b/src/server/commands/Commands.ts @@ -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 } diff --git a/src/server/commands/Put.ts b/src/server/commands/Put.ts new file mode 100644 index 00000000..53ba3d12 --- /dev/null +++ b/src/server/commands/Put.ts @@ -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(); + }) + } + }) +}