This commit is contained in:
Namhyeon Go 2020-11-25 11:46:20 +09:00
parent 9322e6cdd2
commit e0e2088e40
2 changed files with 195 additions and 161 deletions

View File

@ -9,93 +9,12 @@
var LIB = require('lib/std');
var FileObject = function() {
this.interfaces = null;
this.interface = null;
this.filename = null;
this.charset = "utf-8";
this.isExists = false;
this.isFile = false;
this.isDirectory = false;
this.setInterface = function(interfaceName) {
this.interface = this.interfaces[interfaceName];
return this;
};
this.setCharset = function(charset) {
this.charset = charset;
return this;
};
this.create = function() {
this.interfaces = {
fso: CreateObject("Scripting.FileSystemObject"),
ado: CreateObject("ADODB.Stream")
};
this.setInterface("fso");
return this;
};
this.exists = function() {
try {
if (this.interface.FileExists(this.filename)) {
this.isExists = true;
this.isFile = true;
} else if (this.interface.folderExists(this.filename)) {
this.isExists = true;
this.isDirectory = true;
}
} catch (e) {
console.error("FileObject.exists() -> " + e.message);
}
return this.isExists;
};
this.open = function(filename) {
this.filename = filename;
if (!this.exists()) {
console.warn("FileObject.open() -> The file does not exists: " + this.filename);
}
return this;
};
this.create();
};
exports.fileExists = function(FN) {
return (new FileObject()).open(FN).exists();
};
exports.folderExists = function(FN) {
return (new FileObject()).open(FN).exists();
};
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
/////////////////////////////////////////////////////////////////////////////////
exports.VERSIONINFO = "File Lib (file.js) version 0.2";
exports.global = global;
exports.require = global.require;
/////////////////////////////////////////////////////////////////////////////////
// exports.fileGet
/////////////////////////////////////////////////////////////////////////////////
exports.fileGet = function(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var file = FSO.GetFile(FN);
FSO = null;
return file;
};
/////////////////////////////////////////////////////////////////////////////////
// exports.readFile
// readFile
// Read the conents of the pass filename and return as a string
/////////////////////////////////////////////////////////////////////////////////
exports.readFile = function(FN, charset) {
var readFile = function(FN, charset) {
if(typeof(charset) === "undefined") {
var FSO = CreateObject("Scripting.FileSystemObject");
var T = null;
@ -127,11 +46,11 @@ exports.readFile = function(FN, charset) {
};
/////////////////////////////////////////////////////////////////////////////////
// exports.writeFile
// writeFile
// Write the passed content to named disk file
/////////////////////////////////////////////////////////////////////////////////
exports.writeFile = function(FN, content, charset) {
var writeFile = function(FN, content, charset) {
var Stream_No_UTF8_BOM = function(objStream) {
var _objStream = CreateObject("ADODB.Stream");
_objStream.Type = 1;
@ -179,37 +98,132 @@ exports.writeFile = function(FN, content, charset) {
return ok;
};
/////////////////////////////////////////////////////////////////////////////////
// exports.moveFile
/////////////////////////////////////////////////////////////////////////////////
var FileObject = function() {
this.interfaces = null;
this.interface = null;
this.filename = null;
this.charset = "utf-8";
this.isExists = false;
this.isFile = false;
this.isDirectory = false;
this.setInterface = function(interfaceName) {
this.interface = this.interfaces[interfaceName];
return this;
};
this.setCharset = function(charset) {
this.charset = charset;
return this;
};
this.create = function() {
this.interfaces = {
fso: CreateObject("Scripting.FileSystemObject"),
ado: CreateObject("ADODB.Stream")
};
this.setInterface("fso");
return this;
};
this.exists = function() {
try {
if (this.interface.FileExists(this.filename)) {
this.isExists = true;
this.isFile = true;
} else if (this.interface.folderExists(this.filename)) {
this.isExists = true;
this.isDirectory = true;
}
} catch (e) {
console.error("FileObject.exists() -> " + e.message);
}
return this.isExists;
};
this.open = function(filename) {
this.filename = filename;
if (!this.exists()) {
console.warn("FileObject.open() -> The file or directory does not exists: " + this.filename);
}
return this;
};
this.getDetails = function() {
if (this.isFile) {
return this.interface.GetFile(this.filename);
} else if (this.isDirectory) {
return this.interface.GetFolder(this.filename);
}
};
this.remove = function() {
if (this.isFile) {
return this.interface.DeleteFile(this.filename);
} else {
return this.interface.DeleteFolder(this.filename);
}
};
this.moveTo = function(dst) {
if (this.isFile) {
return this.interface.MoveFile(this.filename, dst);
} else {
return this.interface.MoveFolder(this.filename, dst);
}
};
this.read = function() {
if (this.isFile) {
return readFile(this.filename, this.charset);
}
};
this.write = function(content) {
if (this.isFile) {
return writeFile(this.filename, content, this.charset);
}
};
this.mkdir = function() {
return this.interface.CreateFolder(this.filename);
};
this.create();
};
exports.getFile = function(FN) {
return (new FileObject()).getDetails();
};
exports.fileExists = function(FN) {
return (new FileObject()).open(FN).exists();
};
exports.folderExists = function(FN) {
return (new FileObject()).open(FN).exists();
};
exports.moveFile = function(FROM, TO) {
var FSO = CreateObject("Scripting.FileSystemObject");
var res = FSO.MoveFile(FROM, TO);
FSO = null;
return res;
return (new FileObject()).open(FROM).moveTo(TO);
};
/////////////////////////////////////////////////////////////////////////////////
// exports.createFolder
/////////////////////////////////////////////////////////////////////////////////
exports.createFolder = function(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var res = FSO.CreateFolder(FN);
FSO = null;
return res;
return (new FileObject()).open(FN).mkdir();
};
/////////////////////////////////////////////////////////////////////////////////
// exports.deleteFile
/////////////////////////////////////////////////////////////////////////////////
exports.deleteFile = function(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var res = FSO.DeleteFile(FN);
FSO = null;
return res;
return (new FileObject()).open(FN).remove();
};
/////////////////////////////////////////////////////////////////////////////////
exports.readFile = function(FN, charset) {
return (new FileObject()).setCharset(charset).open(FN).read();
};
exports.writeFile = function(FN, content, charset) {
return (new FileObject()).setCharset(charset).open(FN).write(content);
};
exports.VERSIONINFO = "File interface (file.js) version 0.2";
exports.global = global;
exports.require = global.require;

View File

@ -106,20 +106,28 @@ var HTTPObject = function() {
};
this.getHeader = function(key) {
return this.interface.getResponseHeader(key);
try {
return this.interface.getResponseHeader(key);
} catch (e) {
console.error("HTTPObject.getHeader() -> " + e.message);
}
};
this.getHeaders = function() {
var raw = this.interface.getAllResponseHeaders();
try {
var raw = this.interface.getAllResponseHeaders();
return raw.split(/[\r\n]+/).filter(function(s) {
return s.trim().length > 0;
}).map(function(s) {
return s.trim().split(": ");
}).reduce(function(acc, c) {
acc[c[0].trim()] = c[1].trim();
return acc;
}, {});
return raw.split(/[\r\n]+/).filter(function(s) {
return s.trim().length > 0;
}).map(function(s) {
return s.trim().split(": ");
}).reduce(function(acc, c) {
acc[c[0].trim()] = c[1].trim();
return acc;
}, {});
} catch (e) {
console.error("HTTPObject.getHeaders() -> " + e.message);
}
};
this.setBearerAuth = function(token) {
@ -146,20 +154,24 @@ var HTTPObject = function() {
this.open = function(method, url, isAsync) {
this.setMethod(method.toUpperCase());
switch (this.method) {
case "POST":
this.interface.open(method, url, isAsync);
break;
try {
switch (this.method) {
case "POST":
this.interface.open(method, url, isAsync);
break;
case "GET":
this.interface.open(method, url, isAsync);
break;
case "GET":
this.interface.open(method, url, isAsync);
break;
case "PATCH":
break;
case "PATCH":
break;
default:
console.error("Not supported HTTP method: " + method);
default:
console.error("Not supported HTTP method: " + method);
}
} catch (e) {
console.error("HTTPObject.open() -> " + e.message);
}
return this;
@ -168,24 +180,28 @@ var HTTPObject = function() {
this.send = function(callback) {
this.setHeader("Content-Type", this.contentType);
for (var key in this.headers) {
this.interface.setRequestHeader(key, this.headers[key]);
}
try {
for (var key in this.headers) {
this.interface.setRequestHeader(key, this.headers[key]);
}
if (this.isJSONRequest() && typeof(this.requestBody) === "object") {
this.interface.send(JSON.stringify(this.requestBody));
} else {
this.interface.send(this.requestBody);
}
if (this.isJSONRequest() && typeof(this.requestBody) === "object") {
this.interface.send(JSON.stringify(this.requestBody));
} else {
this.interface.send(this.requestBody);
}
if (this.isJSONResponse()) {
this.setResponseBody(JSON.parse(this.interface.responseText));
} else {
this.setResponseBody(this.interface.responseText);
}
if (this.isJSONResponse()) {
this.setResponseBody(JSON.parse(this.interface.responseText));
} else {
this.setResponseBody(this.interface.responseText);
}
if (typeof(callback) === "function") {
callback(this.responseBody);
if (typeof(callback) === "function") {
callback(this.responseBody);
}
} catch (e) {
console.error("HTTPObject.send() -> " + e.message);
}
return this;
@ -200,26 +216,30 @@ var HTTPObject = function() {
};
this.patch = function(url, callback) {
var options = {
type: "PATCH",
headers: this.headers,
url: url,
data: this.requestBody,
contentType: this.contentType,
success: callback
};
try {
var options = {
type: "PATCH",
headers: this.headers,
url: url,
data: this.requestBody,
contentType: this.contentType,
success: callback
};
if (typeof(window) !== "undefined") {
if (typeof(window.jQuery) !== "undefined") {
window.jQuery.ajax(options);
if (typeof(window) !== "undefined") {
if (typeof(window.jQuery) !== "undefined") {
window.jQuery.ajax(options);
} else {
console.error("PATCH is required jQuery library");
}
} else {
console.error("PATCH is required jQuery library");
console.error("PATCH dose not supported on console mode");
}
} else {
console.error("PATCH dose not supported on console mode");
}
return this;
return this;
} catch (e) {
console.error("HTTPObject.patch() -> " + e.message);
}
};
this.create();