dl
Version:
DreamLab Libs
64 lines (51 loc) • 1.82 kB
JavaScript
var Class = require("core").Class;
var OcdnUrl = require('./OcdnUrl.js').OcdnUrl;
var OcdnTransformation = require('./OcdnTransformation.js').OcdnTransformation;
var OcdnFilesUrl = function(){
this.Extends = OcdnUrl;
this.initialize = function (key, url) {
if (arguments.length == 1) {
url = key;
key = undefined;
}
this.owner = null;
this._transformations = [];
this.name = null;
this.parent(key, url, true);
this.init(url);
};
this.init = function (url) {
if (url) {
this.parent(url);
this.parse(url);
} else {
this.setPlugin('files');
}
};
this.parse = function (url) {
this.parent(url);
var parts = url.split('/');
if (parts.length == 7) {
this.owner = parts[4];
var nameExt = parts[6].split('.');
this.name = nameExt[0];
this.ext = nameExt[1];
this._transformations = OcdnTransformation.decodeTransformations(
OcdnTransformation.decrypt(this._key, this.name, parts[5])
);
} else {
throw OcdnFilesUrl.Error.INCORRECT_URL;
}
};
this.toString = function () {
var url = this.parent() + '/' + this.owner + '/' + OcdnTransformation.crypt(this._key, this.name, OcdnTransformation.encodeTransformations(this._transformations)) + '/' + this.name
if (this.ext && this.ext.length > 0) {
url += '.' + this.ext;
}
return url;
};
};
OcdnFilesUrl = new Class(new OcdnFilesUrl());
OcdnFilesUrl.Error = {};
OcdnFilesUrl.Error.INCORRECT_URL = "incorrect file url";
exports.OcdnFilesUrl = OcdnFilesUrl;