Skip to content

Commit 695a93a

Browse files
committed
Added a new helper : 'StandardResource.standardMimeType(...)'
1 parent 8b1caad commit 695a93a

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

lib/resource/std/StandardResource.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ export declare abstract class StandardResource implements IResource {
4242
protected removeFromParent(callback: SimpleCallback): void;
4343
static standardRemoveFromParent(resource: IResource, callback: SimpleCallback): void;
4444
static standardMoveTo(resource: IResource, parent: IResource, newName: string, overwrite: boolean, setName: (name: string) => void, callback: SimpleCallback): void;
45+
static standardMimeType(resource: IResource, targetSource: boolean, callback: ReturnCallback<string>): void;
4546
}

lib/resource/std/StandardResource.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var LockType_1 = require("../lock/LockType");
66
var LockKind_1 = require("../lock/LockKind");
77
var LockBag_1 = require("../lock/LockBag");
88
var Errors_1 = require("../../Errors");
9+
var mimeTypes = require("mime-types");
910
var StandardResource = (function () {
1011
function StandardResource(parent, fsManager) {
1112
this.dateCreation = Date.now();
@@ -139,6 +140,24 @@ var StandardResource = (function () {
139140
});
140141
});
141142
};
143+
StandardResource.standardMimeType = function (resource, targetSource, callback) {
144+
resource.type(function (e, type) {
145+
if (e)
146+
callback(e, null);
147+
else if (type.isFile) {
148+
resource.webName(function (e, name) {
149+
if (e)
150+
callback(e, null);
151+
else {
152+
var mt = mimeTypes.contentType(name);
153+
callback(null, mt ? mt : 'application/octet-stream');
154+
}
155+
});
156+
}
157+
else
158+
callback(Errors_1.Errors.NoMimeTypeForAFolder, null);
159+
});
160+
};
142161
return StandardResource;
143162
}());
144163
exports.StandardResource = StandardResource;

src/resource/std/StandardResource.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { LockKind } from '../lock/LockKind'
88
import { LockBag } from '../lock/LockBag'
99
import { Errors } from '../../Errors'
1010
import { Lock } from '../lock/Lock'
11+
import * as mimeTypes from 'mime-types'
1112

1213
export abstract class StandardResource implements IResource
1314
{
@@ -205,4 +206,25 @@ export abstract class StandardResource implements IResource
205206
})
206207
})
207208
}
209+
public static standardMimeType(resource : IResource, targetSource : boolean, callback : ReturnCallback<string>)
210+
{
211+
resource.type((e, type) => {
212+
if(e)
213+
callback(e, null);
214+
else if(type.isFile)
215+
{
216+
resource.webName((e, name) => {
217+
if(e)
218+
callback(e, null);
219+
else
220+
{
221+
const mt = mimeTypes.contentType(name);
222+
callback(null, mt ? mt as string : 'application/octet-stream');
223+
}
224+
})
225+
}
226+
else
227+
callback(Errors.NoMimeTypeForAFolder, null);
228+
})
229+
}
208230
}

0 commit comments

Comments
 (0)