Skip to content

Commit

Permalink
Added better ETag generator
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 14, 2017
1 parent 9614be9 commit 1506637
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/resource/IResource.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export declare class ResourceType {
static NoResource: ResourceType;
constructor(isFile: boolean, isDirectory: boolean);
}
export declare abstract class ETag {
static createETag(date: number | string): string;
}
export interface IResource {
parent: IResource;
fsManager: FSManager;
Expand Down
10 changes: 10 additions & 0 deletions lib/resource/IResource.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var crypto = require("crypto");
var ResourceType = (function () {
function ResourceType(isFile, isDirectory) {
this.isFile = isFile;
Expand All @@ -12,3 +13,12 @@ ResourceType.Directory = new ResourceType(false, true);
ResourceType.Hibrid = new ResourceType(true, true);
ResourceType.NoResource = new ResourceType(false, false);
exports.ResourceType = ResourceType;
var ETag = (function () {
function ETag() {
}
ETag.createETag = function (date) {
return '"' + crypto.createHash('md5').update(date.toString()).digest('hex') + '"';
};
return ETag;
}());
exports.ETag = ETag;
3 changes: 2 additions & 1 deletion lib/server/commands/Propfind.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var Resource_1 = require("../../resource/Resource");
var xml = require("xmlbuilder");
function default_1(arg, callback) {
arg.getResource(function (e, resource) {
Expand Down Expand Up @@ -84,10 +85,10 @@ function default_1(arg, callback) {
nbOut();
});
}
prop.ele('D:getetag', null, 'zzyzx');
nbOut();
});
resource.lastModifiedDate(function (e, lastModifiedDate) {
prop.ele('D:getetag', null, '\"' + Resource_1.ETag.createETag(lastModifiedDate) + '\"');
prop.ele('D:getlastmodified', new Date(lastModifiedDate).toUTCString());
nbOut();
});
Expand Down
9 changes: 9 additions & 0 deletions src/resource/IResource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FSManager, FSPath } from '../manager/FSManager'
import { LockKind } from './lock/LockKind'
import { Lock } from './lock/Lock'
import * as crypto from 'crypto'

export type SimpleCallback = (error : Error) => void
export type ReturnCallback<T> = (error : Error, data : T) => void
Expand All @@ -18,6 +19,14 @@ export class ResourceType
{ }
}

export abstract class ETag
{
static createETag(date : number | string) : string
{
return '"' + crypto.createHash('md5').update(date.toString()).digest('hex') + '"';
}
}

export interface IResource
{
parent : IResource
Expand Down
4 changes: 2 additions & 2 deletions src/server/commands/Propfind.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HTTPCodes, MethodCallArgs, WebDAVRequest } from '../WebDAVRequest'
import { IResource } from '../../resource/Resource'
import { IResource, ETag } from '../../resource/Resource'
import * as xml from 'xmlbuilder'

export default function(arg : MethodCallArgs, callback)
Expand Down Expand Up @@ -114,11 +114,11 @@ export default function(arg : MethodCallArgs, callback)
})
}

prop.ele('D:getetag', null, 'zzyzx')
nbOut();
})

resource.lastModifiedDate((e, lastModifiedDate) => {
prop.ele('D:getetag', null, '\"' + ETag.createETag(lastModifiedDate) + '\"')
prop.ele('D:getlastmodified', new Date(lastModifiedDate).toUTCString())
nbOut();
})
Expand Down

0 comments on commit 1506637

Please sign in to comment.