mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-02-06 15:04:58 +00:00
fix
This commit is contained in:
parent
9322e6cdd2
commit
e0e2088e40
232
lib/file.js
232
lib/file.js
|
@ -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;
|
||||
|
|
20
lib/http.js
20
lib/http.js
|
@ -106,10 +106,15 @@ var HTTPObject = function() {
|
|||
};
|
||||
|
||||
this.getHeader = function(key) {
|
||||
try {
|
||||
return this.interface.getResponseHeader(key);
|
||||
} catch (e) {
|
||||
console.error("HTTPObject.getHeader() -> " + e.message);
|
||||
}
|
||||
};
|
||||
|
||||
this.getHeaders = function() {
|
||||
try {
|
||||
var raw = this.interface.getAllResponseHeaders();
|
||||
|
||||
return raw.split(/[\r\n]+/).filter(function(s) {
|
||||
|
@ -120,6 +125,9 @@ var HTTPObject = function() {
|
|||
acc[c[0].trim()] = c[1].trim();
|
||||
return acc;
|
||||
}, {});
|
||||
} catch (e) {
|
||||
console.error("HTTPObject.getHeaders() -> " + e.message);
|
||||
}
|
||||
};
|
||||
|
||||
this.setBearerAuth = function(token) {
|
||||
|
@ -146,6 +154,7 @@ var HTTPObject = function() {
|
|||
this.open = function(method, url, isAsync) {
|
||||
this.setMethod(method.toUpperCase());
|
||||
|
||||
try {
|
||||
switch (this.method) {
|
||||
case "POST":
|
||||
this.interface.open(method, url, isAsync);
|
||||
|
@ -161,6 +170,9 @@ var HTTPObject = function() {
|
|||
default:
|
||||
console.error("Not supported HTTP method: " + method);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("HTTPObject.open() -> " + e.message);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
@ -168,6 +180,7 @@ var HTTPObject = function() {
|
|||
this.send = function(callback) {
|
||||
this.setHeader("Content-Type", this.contentType);
|
||||
|
||||
try {
|
||||
for (var key in this.headers) {
|
||||
this.interface.setRequestHeader(key, this.headers[key]);
|
||||
}
|
||||
|
@ -187,6 +200,9 @@ var HTTPObject = function() {
|
|||
if (typeof(callback) === "function") {
|
||||
callback(this.responseBody);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("HTTPObject.send() -> " + e.message);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
@ -200,6 +216,7 @@ var HTTPObject = function() {
|
|||
};
|
||||
|
||||
this.patch = function(url, callback) {
|
||||
try {
|
||||
var options = {
|
||||
type: "PATCH",
|
||||
headers: this.headers,
|
||||
|
@ -220,6 +237,9 @@ var HTTPObject = function() {
|
|||
}
|
||||
|
||||
return this;
|
||||
} catch (e) {
|
||||
console.error("HTTPObject.patch() -> " + e.message);
|
||||
}
|
||||
};
|
||||
|
||||
this.create();
|
||||
|
|
Loading…
Reference in New Issue
Block a user