From 15066377a977965ac493330f961a3bcd709835ee Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Sun, 14 May 2017 11:52:40 +0200 Subject: [PATCH] Added better ETag generator --- lib/resource/IResource.d.ts | 3 +++ lib/resource/IResource.js | 10 ++++++++++ lib/server/commands/Propfind.js | 3 ++- src/resource/IResource.ts | 9 +++++++++ src/server/commands/Propfind.ts | 4 ++-- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/resource/IResource.d.ts b/lib/resource/IResource.d.ts index 94dbbf10..9362bb1f 100644 --- a/lib/resource/IResource.d.ts +++ b/lib/resource/IResource.d.ts @@ -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; diff --git a/lib/resource/IResource.js b/lib/resource/IResource.js index dfaccb99..57f87a05 100644 --- a/lib/resource/IResource.js +++ b/lib/resource/IResource.js @@ -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; @@ -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; diff --git a/lib/server/commands/Propfind.js b/lib/server/commands/Propfind.js index 5ffef5e2..111b2dfa 100644 --- a/lib/server/commands/Propfind.js +++ b/lib/server/commands/Propfind.js @@ -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) { @@ -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(); }); diff --git a/src/resource/IResource.ts b/src/resource/IResource.ts index f776b09d..dbc41baa 100644 --- a/src/resource/IResource.ts +++ b/src/resource/IResource.ts @@ -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 = (error : Error, data : T) => void @@ -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 diff --git a/src/server/commands/Propfind.ts b/src/server/commands/Propfind.ts index 70f83a6f..72deefd5 100644 --- a/src/server/commands/Propfind.ts +++ b/src/server/commands/Propfind.ts @@ -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) @@ -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(); })