Added example.
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
var cache = {};
|
||||
|
||||
exports.put = function (key, value) {
|
||||
cache[key] = value;
|
||||
}
|
||||
|
||||
exports.containsKey = function (key) {
|
||||
return typeof cache[key] != "undefined";
|
||||
}
|
||||
|
||||
exports.get = function (key) {
|
||||
return cache[key];
|
||||
}
|
||||
|
||||
exports.delete = function (key) {
|
||||
delete cache[key];
|
||||
}
|
||||
|
||||
exports.clear = function () {
|
||||
cache = {};
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
var path = require("path");
|
||||
var fileSystem = require("fs");
|
||||
var fileUtility = require("./fileUtility");
|
||||
var serviceConverter = require("./serviceConverter");
|
||||
var cacheManager = require("./cacheManager");
|
||||
var guidManager = require("./guidManager");
|
||||
var config = require("../config");
|
||||
|
||||
var docManager = {};
|
||||
|
||||
docManager.dir = null;
|
||||
docManager.req = null;
|
||||
docManager.res = null;
|
||||
|
||||
docManager.init = function (dir, req, res) {
|
||||
docManager.dir = dir;
|
||||
docManager.req = req;
|
||||
docManager.res = res;
|
||||
}
|
||||
|
||||
docManager.getCorrectName = function (fileName)
|
||||
{
|
||||
var baseName = fileUtility.getFileName(fileName, true);
|
||||
var ext = fileUtility.getFileExtension(fileName);
|
||||
var name = baseName + ext;
|
||||
var index = 1;
|
||||
|
||||
while(fileSystem.existsSync(docManager.storagePath(name))) {
|
||||
name = baseName + " (" + index + ")" + ext;
|
||||
index++;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
docManager.createDemo = function (fileExt)
|
||||
{
|
||||
var demoName = "sample." + fileExt;
|
||||
|
||||
var fileName = docManager.getCorrectName(demoName);
|
||||
|
||||
docManager.copyFile(path.join(docManager.dir, "public", "samples", demoName), docManager.storagePath(fileName));
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
docManager.getFileUri = function (fileName)
|
||||
{
|
||||
var filePath = docManager.getlocalFileUri(fileName);
|
||||
|
||||
if (config.haveExternalIp) {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
return docManager.getExternalUri(filePath);
|
||||
}
|
||||
|
||||
docManager.getlocalFileUri = function (fileName) {
|
||||
var serverPath = docManager.req.protocol + "://" + docManager.req.get("host");
|
||||
var storagePath = config.storageFolder;
|
||||
var hostAddress = docManager.curUserHostAddress();
|
||||
|
||||
return serverPath + "/" + storagePath + "/" + hostAddress + "/" + encodeURIComponent(fileName);
|
||||
}
|
||||
|
||||
docManager.getCallback = function (fileName)
|
||||
{
|
||||
var server = docManager.req.protocol + "://" + docManager.req.get("host");
|
||||
var hostAddress = docManager.curUserHostAddress();
|
||||
var handler = "/track?useraddress=" + encodeURIComponent(hostAddress) + "&filename=" + encodeURIComponent(fileName);
|
||||
|
||||
return server + handler;
|
||||
}
|
||||
|
||||
docManager.storagePath = function (fileName, userAddress)
|
||||
{
|
||||
var directory = path.join(docManager.dir, "public", config.storageFolder, docManager.curUserHostAddress(userAddress));
|
||||
if (!fileSystem.existsSync(directory)) {
|
||||
fileSystem.mkdirSync(directory);
|
||||
}
|
||||
return path.join(directory, fileName);
|
||||
}
|
||||
|
||||
docManager.curUserHostAddress = function (userAddress)
|
||||
{
|
||||
if (!userAddress)
|
||||
userAddress = docManager.req.headers["x-forwarded-for"] || docManager.req.connection.remoteAddress;
|
||||
|
||||
return userAddress.replace(new RegExp("[^0-9a-zA-Z.=]", "g"), "_");
|
||||
}
|
||||
|
||||
docManager.copyFile = function (exist, target)
|
||||
{
|
||||
fileSystem.writeFileSync(target, fileSystem.readFileSync(exist));
|
||||
}
|
||||
|
||||
docManager.getExternalUri = function (localUri)
|
||||
{
|
||||
var documentRevisionId = serviceConverter.generateRevisionId(localUri);
|
||||
|
||||
if (cacheManager.containsKey(documentRevisionId))
|
||||
return cacheManager.get(documentRevisionId);
|
||||
|
||||
try {
|
||||
|
||||
var fileName = fileUtility.getFileName(localUri);
|
||||
var storagePath = docManager.storagePath(decodeURIComponent(fileName));
|
||||
var content = fileSystem.readFileSync(storagePath);
|
||||
|
||||
var externalUri = serviceConverter.getExternalUri(content, content.length, null, documentRevisionId);
|
||||
|
||||
cacheManager.put(documentRevisionId, externalUri);
|
||||
|
||||
return externalUri;
|
||||
}
|
||||
catch (ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
docManager.getInternalExtension = function (fileType)
|
||||
{
|
||||
if (fileType == fileUtility.fileType.text)
|
||||
return ".docx";
|
||||
|
||||
if (fileType == fileUtility.fileType.spreadsheet)
|
||||
return ".xlsx";
|
||||
|
||||
if (fileType == fileUtility.fileType.presentation)
|
||||
return ".pptx";
|
||||
|
||||
return ".docx";
|
||||
}
|
||||
|
||||
docManager.getKey = function (fileName)
|
||||
{
|
||||
var uri = docManager.getlocalFileUri(fileName);
|
||||
return serviceConverter.generateRevisionId(uri);
|
||||
}
|
||||
|
||||
docManager.getValidateKey = function (fileName)
|
||||
{
|
||||
var key = docManager.getKey(fileName);
|
||||
return serviceConverter.generateValidateKey(key, false);
|
||||
}
|
||||
|
||||
module.exports = docManager;
|
||||
@@ -0,0 +1,78 @@
|
||||
var config = require("../config");
|
||||
|
||||
var fileUtility = {}
|
||||
|
||||
fileUtility.getFileName = function (url, withoutExtension) {
|
||||
if (!url) return null;
|
||||
|
||||
var filename;
|
||||
|
||||
if (config.tempStorageUrl && url.indexOf(config.tempStorageUrl) == 0) {
|
||||
var params = getUrlParams(url);
|
||||
filename = params == null ? null : params["filename"];
|
||||
} else {
|
||||
var parts = url.toLowerCase().split("/");
|
||||
fileName = parts.pop();
|
||||
}
|
||||
|
||||
if (withoutExtension) {
|
||||
var ext = fileUtility.getFileExtension(fileName);
|
||||
return fileName.replace(ext, "");
|
||||
}
|
||||
|
||||
return fileName;
|
||||
};
|
||||
|
||||
fileUtility.getFileExtension = function (url, withoutDot) {
|
||||
if (!url) return null;
|
||||
|
||||
var fileName = fileUtility.getFileName(url);
|
||||
|
||||
var parts = fileName.toLowerCase().split(".");
|
||||
|
||||
return withoutDot ? parts.pop() : "." + parts.pop();
|
||||
};
|
||||
|
||||
fileUtility.getFileType = function(url)
|
||||
{
|
||||
var ext = fileUtility.getFileExtension(url);
|
||||
|
||||
if (fileUtility.documentExts.indexOf(ext) != -1) return fileUtility.fileType.text;
|
||||
if (fileUtility.spreadsheetExts.indexOf(ext) != -1) return fileUtility.fileType.spreadsheet;
|
||||
if (fileUtility.presentationExts.indexOf(ext) != -1) return fileUtility.fileType.presentation;
|
||||
|
||||
return fileUtility.fileType.text;
|
||||
}
|
||||
|
||||
fileUtility.fileType = {
|
||||
text: "text",
|
||||
spreadsheet: "spreadsheet",
|
||||
presentation: "presentation"
|
||||
}
|
||||
|
||||
fileUtility.documentExts = [".docx", ".doc", ".odt", ".rtf", ".txt",".html", ".htm", ".mht", ".pdf", ".djvu",".fb2", ".epub", ".xps"];
|
||||
|
||||
fileUtility.spreadsheetExts = [".xls", ".xlsx", ".ods", ".csv"];
|
||||
|
||||
fileUtility.presentationExts = [".ppt", ".pptx", ".odp"];
|
||||
|
||||
function getUrlParams(url)
|
||||
{
|
||||
try {
|
||||
var query = url.split("?").pop();
|
||||
var params = query.split("&");
|
||||
var map = {};
|
||||
for (var i = 0; i < params.length; i++)
|
||||
{
|
||||
var parts = param.split("=");
|
||||
map[parts[0]] = parts[1];
|
||||
}
|
||||
return map;
|
||||
}
|
||||
catch (ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = fileUtility;
|
||||
@@ -0,0 +1,7 @@
|
||||
var s4 = function () {
|
||||
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
|
||||
};
|
||||
|
||||
exports.newGuid = function () {
|
||||
return (s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4());
|
||||
};
|
||||
@@ -0,0 +1,209 @@
|
||||
var path = require("path");
|
||||
var urllib = require("urllib");
|
||||
var syncRequest = require("sync-request");
|
||||
var fileSystem = require("fs");
|
||||
var xml2js = require("xml2js");
|
||||
var fileUtility = require("./fileUtility");
|
||||
var cacheManager = require("./cacheManager");
|
||||
var guidManager = require("./guidManager");
|
||||
var signatureManager = require("./signatureManager");
|
||||
var config = require("../config");
|
||||
|
||||
var serviceConverter = {};
|
||||
|
||||
serviceConverter.convertParams = "?url={0}&outputtype={1}&filetype={2}&title={3}&key={4}&vkey={5}";
|
||||
serviceConverter.userIp = null;
|
||||
|
||||
serviceConverter.getConvertedUri = function (documentUri, fromExtension, toExtension, documentRevisionId)
|
||||
{
|
||||
var xml = serviceConverter.sendRequestToConvertService(documentUri, fromExtension, toExtension, documentRevisionId);
|
||||
|
||||
var res = serviceConverter.getResponseUri(xml);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
serviceConverter.getConvertedUriAsync = function (documentUri, fromExtension, toExtension, documentRevisionId, callback) {
|
||||
|
||||
fromExtension = fromExtension || fileUtility.getFileExtension(documentUri);
|
||||
|
||||
var title = fileUtility.getFileName(documentUri) || guidManager.newGuid();
|
||||
|
||||
documentRevisionId = serviceConverter.generateRevisionId(documentRevisionId || documentUri);
|
||||
|
||||
var validateKey = serviceConverter.generateValidateKey(documentRevisionId, false);
|
||||
|
||||
var params = serviceConverter.convertParams.format(
|
||||
encodeURIComponent(documentUri),
|
||||
toExtension.replace(".", ""),
|
||||
fromExtension.replace(".", ""),
|
||||
title,
|
||||
documentRevisionId,
|
||||
validateKey);
|
||||
|
||||
urllib.request(config.converterUrl + params, callback);
|
||||
}
|
||||
|
||||
serviceConverter.getExternalUri = function (fileStream, contentLength, contentType, documentRevisionId)
|
||||
{
|
||||
var validateKey = serviceConverter.generateValidateKey(documentRevisionId, false);
|
||||
|
||||
var params = serviceConverter.convertParams.format("", "", "", "", documentRevisionId, validateKey);
|
||||
|
||||
var urlTostorage = config.storageUrl + params;
|
||||
|
||||
var response = syncRequest("POST", urlTostorage, {
|
||||
headers: {
|
||||
"Content-Type": contentType == null ? "application/octet-stream" : contentType,
|
||||
"Content-Length": contentLength.toString(),
|
||||
"charset": "utf-8"
|
||||
},
|
||||
body : fileStream
|
||||
});
|
||||
|
||||
var res = serviceConverter.getResponseUri(response.body.toString());
|
||||
|
||||
return res.value;
|
||||
}
|
||||
|
||||
serviceConverter.generateRevisionId = function (expectedKey)
|
||||
{
|
||||
if (expectedKey.length > 20) {
|
||||
expectedKey = expectedKey.hashCode().toString();
|
||||
}
|
||||
|
||||
var key = expectedKey.replace(new RegExp("[^0-9-.a-zA-Z_=]", "g"), "_");
|
||||
|
||||
return key.substring(0, Math.min(key.length, 20));
|
||||
}
|
||||
|
||||
serviceConverter.generateValidateKey = function (documentRevisionId, addHostForValidate)
|
||||
{
|
||||
if (!documentRevisionId) return "";
|
||||
|
||||
var expdate = new Date();
|
||||
var key = config.tenantId || "";
|
||||
var userCount = 0;
|
||||
var userIp = addHostForValidate ? serviceConverter.userIp : null;
|
||||
var skey = config.key || "";
|
||||
|
||||
return signatureManager.create(expdate, documentRevisionId, key, userCount, userIp, skey);
|
||||
}
|
||||
|
||||
serviceConverter.sendRequestToConvertService = function (documentUri, fromExtension, toExtension, documentRevisionId)
|
||||
{
|
||||
fromExtension = fromExtension || fileUtility.getFileExtension(documentUri);
|
||||
|
||||
var title = fileUtility.getFileName(documentUri) || guidManager.newGuid();
|
||||
|
||||
documentRevisionId = serviceConverter.generateRevisionId(documentRevisionId || documentUri);
|
||||
|
||||
var validateKey = serviceConverter.generateValidateKey(documentRevisionId, false);
|
||||
|
||||
var params = serviceConverter.convertParams.format(
|
||||
encodeURIComponent(documentUri),
|
||||
toExtension.replace(".", ""),
|
||||
fromExtension.replace(".", ""),
|
||||
title,
|
||||
documentRevisionId,
|
||||
validateKey);
|
||||
|
||||
var res = syncRequest("GET", config.converterUrl + params);
|
||||
return res.getBody("utf8");
|
||||
}
|
||||
|
||||
serviceConverter.processConvertServiceResponceError = function (errorCode)
|
||||
{
|
||||
var errorMessage = "";
|
||||
var errorMessageTemplate = "Error occurred in the ConvertService: ";
|
||||
|
||||
switch (errorCode)
|
||||
{
|
||||
case -20:
|
||||
errorMessage = errorMessageTemplate + "vkey deciphering error";
|
||||
break;
|
||||
case -8:
|
||||
errorMessage = errorMessageTemplate + "Error document VKey";
|
||||
break;
|
||||
case -7:
|
||||
errorMessage = errorMessageTemplate + "Error document request";
|
||||
break;
|
||||
case -6:
|
||||
errorMessage = errorMessageTemplate + "Error database";
|
||||
break;
|
||||
case -5:
|
||||
errorMessage = errorMessageTemplate + "Error unexpected guid";
|
||||
break;
|
||||
case -4:
|
||||
errorMessage = errorMessageTemplate + "Error download error";
|
||||
break;
|
||||
case -3:
|
||||
errorMessage = errorMessageTemplate + "Error convertation error";
|
||||
break;
|
||||
case -2:
|
||||
errorMessage = errorMessageTemplate + "Error convertation timeout";
|
||||
break;
|
||||
case -1:
|
||||
errorMessage = errorMessageTemplate + "Error convertation unknown";
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
errorMessage = "ErrorCode = " + errorCode;
|
||||
break;
|
||||
}
|
||||
|
||||
throw { message: errorMessage };
|
||||
}
|
||||
|
||||
serviceConverter.getResponseUri = function (xml)
|
||||
{
|
||||
var json = serviceConverter.convertXmlStringToJson(xml);
|
||||
|
||||
if (!json.FileResult)
|
||||
throw { message: "FileResult node is null" };
|
||||
|
||||
var fileResult = json.FileResult;
|
||||
|
||||
if (fileResult.Error)
|
||||
serviceConverter.processConvertServiceResponceError(parseInt(fileResult.Error[0]));
|
||||
|
||||
if (!fileResult.EndConvert)
|
||||
throw { message: "EndConvert node is null" };
|
||||
|
||||
var isEndConvert = fileResult.EndConvert[0].toLowerCase() === "true";
|
||||
|
||||
if (!fileResult.Percent)
|
||||
throw { message: "Percent node is null" };
|
||||
|
||||
var percent = parseInt(fileResult.Percent[0]);
|
||||
var uri = null;
|
||||
|
||||
if (isEndConvert) {
|
||||
if (!fileResult.FileUrl)
|
||||
throw { message: "FileUrl node is null" };
|
||||
|
||||
uri = fileResult.FileUrl[0];
|
||||
percent = 100;
|
||||
} else {
|
||||
percent = percent >= 100 ? 99 : percent;
|
||||
}
|
||||
|
||||
return {
|
||||
key: percent,
|
||||
value: uri
|
||||
};
|
||||
}
|
||||
|
||||
serviceConverter.convertXmlStringToJson = function (xml)
|
||||
{
|
||||
var res;
|
||||
|
||||
xml2js.parseString(xml, function (err, result) {
|
||||
res = result;
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
module.exports = serviceConverter;
|
||||
@@ -0,0 +1,232 @@
|
||||
var crypto = require("crypto");
|
||||
|
||||
var signature = {};
|
||||
|
||||
exports.create = function (expire, key, key_id, user_count, ip, secret)
|
||||
{
|
||||
try
|
||||
{
|
||||
var strFormat = '{"expire":"\\/Date({0})\\/","key":"{1}","key_id":"{2}","user_count":{3},"ip":"{4}"}';
|
||||
|
||||
var str = strFormat.format(expire.getTime(), key, key_id, user_count, (ip || ""));
|
||||
|
||||
var payload = getHashBase64(str + secret) + "?" + str;
|
||||
|
||||
var data = Base64.encode(payload);
|
||||
|
||||
var cnt = 0;
|
||||
while (data.indexOf('=') !== -1) {
|
||||
cnt++;
|
||||
data = data.replace('=', '');
|
||||
}
|
||||
|
||||
data = (data + cnt).replace(/\+/g, '/').replace(/-/g, '_').replace(/\//g, '_');
|
||||
|
||||
return data;
|
||||
}
|
||||
catch(ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
var getHashBase64 = function (str)
|
||||
{
|
||||
var sha256 = crypto.createHash("sha256");
|
||||
sha256.update(str, "utf8");
|
||||
var result = sha256.digest("base64");
|
||||
return result;
|
||||
}
|
||||
|
||||
var Base64 = {
|
||||
|
||||
// private property
|
||||
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
||||
|
||||
// public method for encoding
|
||||
encode : function (input) {
|
||||
var output = "";
|
||||
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
|
||||
var i = 0;
|
||||
|
||||
input = Base64._utf8_encode(input);
|
||||
|
||||
while (i < input.length) {
|
||||
|
||||
chr1 = input.charCodeAt(i++);
|
||||
chr2 = input.charCodeAt(i++);
|
||||
chr3 = input.charCodeAt(i++);
|
||||
|
||||
enc1 = chr1 >> 2;
|
||||
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
||||
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
||||
enc4 = chr3 & 63;
|
||||
|
||||
if (isNaN(chr2)) {
|
||||
enc3 = enc4 = 64;
|
||||
} else if (isNaN(chr3)) {
|
||||
enc4 = 64;
|
||||
}
|
||||
|
||||
output = output +
|
||||
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
|
||||
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
|
||||
|
||||
}
|
||||
|
||||
return output;
|
||||
},
|
||||
|
||||
encode_arr : function (input) {
|
||||
var output = "";
|
||||
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
|
||||
var i = 0;
|
||||
|
||||
input = Base64._utf8_encode_arr(input);
|
||||
|
||||
while (i < input.length) {
|
||||
|
||||
chr1 = input[i++];
|
||||
chr2 = input[i++];
|
||||
chr3 = input[i++];
|
||||
|
||||
enc1 = chr1 >> 2;
|
||||
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
||||
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
||||
enc4 = chr3 & 63;
|
||||
|
||||
if (isNaN(chr2)) {
|
||||
enc3 = enc4 = 64;
|
||||
} else if (isNaN(chr3)) {
|
||||
enc4 = 64;
|
||||
}
|
||||
|
||||
output = output +
|
||||
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
|
||||
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
|
||||
|
||||
}
|
||||
|
||||
return output;
|
||||
},
|
||||
|
||||
// public method for decoding
|
||||
decode : function (input) {
|
||||
var output = "";
|
||||
var chr1, chr2, chr3;
|
||||
var enc1, enc2, enc3, enc4;
|
||||
var i = 0;
|
||||
|
||||
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
|
||||
|
||||
while (i < input.length) {
|
||||
|
||||
enc1 = this._keyStr.indexOf(input.charAt(i++));
|
||||
enc2 = this._keyStr.indexOf(input.charAt(i++));
|
||||
enc3 = this._keyStr.indexOf(input.charAt(i++));
|
||||
enc4 = this._keyStr.indexOf(input.charAt(i++));
|
||||
|
||||
chr1 = (enc1 << 2) | (enc2 >> 4);
|
||||
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
|
||||
chr3 = ((enc3 & 3) << 6) | enc4;
|
||||
|
||||
output = output + String.fromCharCode(chr1);
|
||||
|
||||
if (enc3 != 64) {
|
||||
output = output + String.fromCharCode(chr2);
|
||||
}
|
||||
if (enc4 != 64) {
|
||||
output = output + String.fromCharCode(chr3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
output = Base64._utf8_decode(output);
|
||||
|
||||
return output;
|
||||
|
||||
},
|
||||
|
||||
// private method for UTF-8 encoding
|
||||
_utf8_encode : function (string) {
|
||||
string = string.replace(/\r\n/g, "\n");
|
||||
var utftext = "";
|
||||
|
||||
for (var n = 0; n < string.length; n++) {
|
||||
|
||||
var c = string.charCodeAt(n);
|
||||
|
||||
if (c < 128) {
|
||||
utftext += String.fromCharCode(c);
|
||||
}
|
||||
else if ((c > 127) && (c < 2048)) {
|
||||
utftext += String.fromCharCode((c >> 6) | 192);
|
||||
utftext += String.fromCharCode((c & 63) | 128);
|
||||
}
|
||||
else {
|
||||
utftext += String.fromCharCode((c >> 12) | 224);
|
||||
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
|
||||
utftext += String.fromCharCode((c & 63) | 128);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return utftext;
|
||||
},
|
||||
_utf8_encode_arr : function (string) {
|
||||
//string = string.replace(/\r\n/g,"\n");
|
||||
var utftext = "";
|
||||
|
||||
for (var n = 0; n < string.length; n++) {
|
||||
|
||||
var c = string[n];
|
||||
|
||||
if (c < 128) {
|
||||
utftext += String.fromCharCode(c);
|
||||
}
|
||||
else if ((c > 127) && (c < 2048)) {
|
||||
utftext += String.fromCharCode((c >> 6) | 192);
|
||||
utftext += String.fromCharCode((c & 63) | 128);
|
||||
}
|
||||
else {
|
||||
utftext += String.fromCharCode((c >> 12) | 224);
|
||||
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
|
||||
utftext += String.fromCharCode((c & 63) | 128);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return utftext;
|
||||
},
|
||||
|
||||
// private method for UTF-8 decoding
|
||||
_utf8_decode : function (utftext) {
|
||||
var string = "";
|
||||
var i = 0;
|
||||
var c = c1 = c2 = 0;
|
||||
|
||||
while (i < utftext.length) {
|
||||
|
||||
c = utftext.charCodeAt(i);
|
||||
|
||||
if (c < 128) {
|
||||
string += String.fromCharCode(c);
|
||||
i++;
|
||||
}
|
||||
else if ((c > 191) && (c < 224)) {
|
||||
c2 = utftext.charCodeAt(i + 1);
|
||||
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
|
||||
i += 2;
|
||||
}
|
||||
else {
|
||||
c2 = utftext.charCodeAt(i + 1);
|
||||
c3 = utftext.charCodeAt(i + 2);
|
||||
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
|
||||
i += 3;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return string;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user