Skip to content

Commit

Permalink
Added a new helper : 'StandardResource.standardMimeType(...)'
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 15, 2017
1 parent 8b1caad commit 695a93a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/resource/std/StandardResource.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ export declare abstract class StandardResource implements IResource {
protected removeFromParent(callback: SimpleCallback): void;
static standardRemoveFromParent(resource: IResource, callback: SimpleCallback): void;
static standardMoveTo(resource: IResource, parent: IResource, newName: string, overwrite: boolean, setName: (name: string) => void, callback: SimpleCallback): void;
static standardMimeType(resource: IResource, targetSource: boolean, callback: ReturnCallback<string>): void;
}
19 changes: 19 additions & 0 deletions lib/resource/std/StandardResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var LockType_1 = require("../lock/LockType");
var LockKind_1 = require("../lock/LockKind");
var LockBag_1 = require("../lock/LockBag");
var Errors_1 = require("../../Errors");
var mimeTypes = require("mime-types");
var StandardResource = (function () {
function StandardResource(parent, fsManager) {
this.dateCreation = Date.now();
Expand Down Expand Up @@ -139,6 +140,24 @@ var StandardResource = (function () {
});
});
};
StandardResource.standardMimeType = function (resource, targetSource, callback) {
resource.type(function (e, type) {
if (e)
callback(e, null);
else if (type.isFile) {
resource.webName(function (e, name) {
if (e)
callback(e, null);
else {
var mt = mimeTypes.contentType(name);
callback(null, mt ? mt : 'application/octet-stream');
}
});
}
else
callback(Errors_1.Errors.NoMimeTypeForAFolder, null);
});
};
return StandardResource;
}());
exports.StandardResource = StandardResource;
22 changes: 22 additions & 0 deletions src/resource/std/StandardResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { LockKind } from '../lock/LockKind'
import { LockBag } from '../lock/LockBag'
import { Errors } from '../../Errors'
import { Lock } from '../lock/Lock'
import * as mimeTypes from 'mime-types'

export abstract class StandardResource implements IResource
{
Expand Down Expand Up @@ -205,4 +206,25 @@ export abstract class StandardResource implements IResource
})
})
}
public static standardMimeType(resource : IResource, targetSource : boolean, callback : ReturnCallback<string>)
{
resource.type((e, type) => {
if(e)
callback(e, null);
else if(type.isFile)
{
resource.webName((e, name) => {
if(e)
callback(e, null);
else
{
const mt = mimeTypes.contentType(name);
callback(null, mt ? mt as string : 'application/octet-stream');
}
})
}
else
callback(Errors.NoMimeTypeForAFolder, null);
})
}
}

0 comments on commit 695a93a

Please sign in to comment.