dl
Version:
DreamLab Libs
122 lines (100 loc) • 2.91 kB
JavaScript
var OcdnUrl = function (key, url, noInit) {
if (arguments.length == 1) {
url = key;
noInit = undefined;
key = undefined;
}
// klucz do szyfrowania: podany lub domyslny dla ocdn
this.setKey(key);
this.protocol = 'http';
this.port = 80;
this.domain = 'ocdn.eu';
this.plugin = null;
this._isRelative = false;
if (noInit == undefined) {
this.init(url);
}
};
OcdnUrl.prototype.init = function (url) {
if (url) {
this.parse(url);
} else {
this.plugin = '';
}
};
OcdnUrl.prototype.setKey = function (key) {
this._key = key || 'J&D87ftasd67SFD%&^asdf';
};
OcdnUrl.prototype.relative = function () {
this._isRelative = !this._isRelative;
return this;
};
OcdnUrl.prototype.withoutProtocol = function () {
this.protocol = '';
return this;
};
OcdnUrl.prototype.parse = function (url) {
var parts = url.split('/');
if (parts.length>=4) {
this.protocol = parts[0].replace(':', '');
var domainHost = parts[2].split(':');
this.setDomain(domainHost[0]);
if (domainHost[1]) {
this.setPort(domainHost[1]);
}
this.setBucket(parts[4]);
this.setPlugin(parts[3]);
} else {
throw OcdnUrl.Error.INCORRECT_URL;
}
};
OcdnUrl.prototype.toString = function () {
if (!this._isRelative) {
var port = (this.port && this.port==80) ? "" : ':'+this.port;
if(this.protocol) {
var url = this.protocol + '://' + this.domain + port + '/'+ this.plugin;
} else {
var url = '//' + this.domain + port + '/'+ this.plugin;
}
return url;
} else {
return '/'+ this.plugin;
}
};
OcdnUrl.prototype.setDomain = function (domain) {
if (domain.indexOf('http://') == 0) {
domain = domain.replace('http://', '');
}
this.domain = domain;
};
OcdnUrl.prototype.setPort = function (port) {
this.port = +port;
};
OcdnUrl.prototype.setPlugin = function (plugin) {
this.plugin = plugin;
};
OcdnUrl.prototype.getPlugin = function () {
return this.plugin;
};
OcdnUrl.prototype.setBucket = function (bucket) {
this.bucket = bucket;
};
OcdnUrl.prototype.getBucket = function () {
return this.bucket;
};
OcdnUrl.prototype.setHttpHostPort = function (url) {
var parts = url.split('/');
if (parts.length == 3) {
this.protocol = parts[0].replace(':', '');
var domainHost = parts[2].split(':');
this.setDomain(domainHost[0]);
if (domainHost[1]) {
this.setPort(domainHost[1]);
}
} else {
throw OcdnUrl.Error.INCORRECT_URL;
}
}
OcdnUrl.Error = {};
OcdnUrl.Error.INCORRECT_URL = "incorrect url";
exports.OcdnUrl = OcdnUrl;