From 3a754133dbc138390503341fd2e9beba3e43aa4b Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Fri, 27 Jan 2012 12:05:17 -0800 Subject: Merged old FileIO --- js/io/system/coreioapi.js | 819 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 819 insertions(+) create mode 100755 js/io/system/coreioapi.js (limited to 'js/io/system/coreioapi.js') diff --git a/js/io/system/coreioapi.js b/js/io/system/coreioapi.js new file mode 100755 index 00000000..c920d8cd --- /dev/null +++ b/js/io/system/coreioapi.js @@ -0,0 +1,819 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +/* ///////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////// +NOTES: +These methods should only be access through the file and project IO classes. +//////////////////////////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////////// */ +var Montage = require("montage/core/core").Montage, + Component = require("montage/ui/component").Component; +//////////////////////////////////////////////////////////////////////// +//Exporting as Project I/O +exports.CoreIoApi = Montage.create(Component, { + //////////////////////////////////////////////////////////////////// + // private property containing the file service URL to use for all file IO calls + _fileServiceURL: { + enumerable: false, + value: "http://localhost:16380/file" + }, + //////////////////////////////////////////////////////////////////// + // private property containing the directory service URL to use for all file IO calls + _directoryServiceURL: { + enumerable: false, + value: "http://localhost:16380/directory" + }, + //////////////////////////////////////////////////////////////////// + // private helper to parse URIs and append them to the service URL + _prepareServiceURL: { + enumerable: false, + value: function(serviceURL, path) { + var urlOut = path.replace(/\\/g,"/"); + urlOut = urlOut.replace(/:/g,""); + urlOut = encodeURI(urlOut); + //add leading / if not already there + if((urlOut.length > 0) && (urlOut.charAt(0) !== "/")){ + urlOut = "/" + urlOut; + } + //remove extra / at the end + if((urlOut.length > 1) && (urlOut.charAt(urlOut.length - 1) === "/")){ + urlOut = urlOut.substring(0, (urlOut.length - 1)); + } + + return serviceURL + urlOut; + } + }, + //////////////////////////////////////////////////////////////////// + // Checks for the existence of a file + // Parameters: + // the file parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - the file exists + // 404 - the file does not exist + // 500 - unknown server error occurred + fileExists: { + enumerable: false, + value: function(file) { + // + var retValue = { success:null, status:null }; + // + if(file && file.uri && file.uri.length) { + try { + var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + xhr = new XMLHttpRequest(); + // + xhr.open("GET", serviceURL, false); + xhr.setRequestHeader("check-existence-only", "true"); + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + // + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // Creates a new file at the specified path + // Parameters: + // the file parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" + // it can optionally contain the following properties + // contentType: string with the content type i.e. "text/plain". "text/plain" is assumed if this property is not specified + // contents: string containing the file contents. These contents will be saved to the new file. + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 201 - the file was created and contents were saved if they were passed + // 400 - the file already exists and could not be created + // 500 - unknown server error occurred + createFile: { + enumerable: false, + value: function(file) { + var retValue = { success:null, status:null }; + if(file && file.uri && file.uri.length) { + try { + var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + xhr = new XMLHttpRequest(); + // + xhr.open("POST", serviceURL, false); + if(file.contentType && file.contentType.length) + xhr.setRequestHeader("Content-Type", file.contentType); + else + xhr.setRequestHeader("Content-Type", "text/plain"); + + if(file.contents && file.contents.length) + xhr.send(file.contents); + else + xhr.send(); + + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // Save contents into an existing file at the specified path + // Parameters: + // the file parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" + // it can optionally contain the following properties + // contentType: string with the content type i.e. "text/plain". "text/plain" is assumed if this property is not specified + // contents: string containing the file contents. These contents will be saved to the new file. + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - the file was saved + // 404 - the file specified does not exist + // 500 - unknown server error occurred + updateFile: { + enumerable: false, + value: function(file) { + var retValue = { success:null, status:null }; + if(file && file.uri && file.uri.length && file.contents && file.contents.length) { + try { + var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + xhr = new XMLHttpRequest(); + // + xhr.open("PUT", serviceURL, false); + if(file.contentType && file.contentType.length) + xhr.setRequestHeader("Content-Type", file.contentType); + else + xhr.setRequestHeader("Content-Type", "text/plain"); + + xhr.send(file.contents); + + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // Copies a file from one location to another + // Parameters: + // the file parameter must contain the following properties + // sourceUri: string value containing the full file path/URI to copy from i.e. "c:/foo/bar.html" + // destUri: string containing the full path/URI to copy to + // it can optionally contain the following properties + // overwriteDestination: bool indicating whether it is okay to overwrite the file specified at destUri if it already exists + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - the file was copied + // 404 - the file specified in sourceUri does not exist + // 500 - unknown server error occurred + copyFile: { + enumerable: false, + value: function(file) { + var retValue = { success:null, status:null }; + if(file && file.sourceUri && file.sourceUri.length && file.destUri && file.destUri.length) { + try { + var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.destUri), + xhr = new XMLHttpRequest(); + // + xhr.open("PUT", serviceURL, false); + xhr.setRequestHeader("sourceURI", file.sourceUri); + // + if(file.overwriteDestination && file.overwriteDestination === true) { + xhr.setRequestHeader("overwrite-destination", "true"); + } + // + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + // + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // Moves a file from one location to another + // Parameters: + // the file parameter must contain the following properties + // sourceUri: string value containing the full file path/URI to copy from i.e. "c:/foo/bar.html" + // destUri: string containing the full path/URI to copy to + // it can optionally contain the following properties + // overwriteDestination: bool indicating whether it is okay to overwrite the file specified at destUri if it already exists + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - the file was moved + // 404 - the file specified in sourceUri does not exist + // 500 - unknown server error occurred + moveFile: { + enumerable: false, + value: function(file) { + var retValue = { success:null, status:null }; + if(file && file.sourceUri && file.sourceUri.length && file.destUri && file.destUri.length) { + try { + var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.destUri), + xhr = new XMLHttpRequest(); + // + xhr.open("PUT", serviceURL, false); + xhr.setRequestHeader("sourceURI", file.sourceUri); + xhr.setRequestHeader("delete-source", "true"); + // + if(file.overwriteDestination && file.overwriteDestination === true) { + xhr.setRequestHeader("overwrite-destination", "true"); + } + // + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + // + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // Deletes an existing file + // Parameters: + // the file parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - the file was deleted + // 404 - the file does not exist + // 500 - unknown server error occurred + deleteFile: { + enumerable: false, + value: function(file) { + var retValue = { success:null, status:null }; + if(file && file.uri && file.uri.length) { + try { + var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + xhr = new XMLHttpRequest(); + // + xhr.open("DELETE", serviceURL, false); + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + // + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // Reads an existing file + // Parameters: + // the file parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // content: string containing the file contents + // status: int indicating the request HTTP status code + // 200 - the file was read and its contents were returned + // 404 - the file does not exist + // 500 - unknown server error occurred + openFile: { + enumerable: false, + value: function(file) { + // + var retValue = { success:null, content:null, status:null}; + // + if(file && file.uri && file.uri.length) { + try { + var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + xhr = new XMLHttpRequest(); + // + xhr.open("GET", serviceURL, false); + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + if(xhr.status == 200) { + retValue.content = xhr.responseText; + } + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + // + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // Create a new directory/folder + // Parameters: + // the dir parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/dir/subdir" + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 201 - the directory was created + // 400 - the directory was unable to be created + // 500 - unknown server error occurred + createDirectory: { + enumerable: false, + value: function(dir) { + var retValue = { success:null, status:null }; + if(dir && dir.uri && dir.uri.length) { + try { + var serviceURL = this._prepareServiceURL(this._directoryServiceURL, dir.uri), + xhr = new XMLHttpRequest(); + // + xhr.open("POST", serviceURL, false); + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + // + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // Delete a directory/folder + // Parameters: + // the dir parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/dir/subdir" + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - the directory was deleted + // 404 - the directory does not exist + // 500 - unknown server error occurred + deleteDirectory: { + enumerable: false, + value: function(dir) { + var retValue = { success:null, status:null }; + if(dir && dir.uri && dir.uri.length) { + try { + var serviceURL = this._prepareServiceURL(this._directoryServiceURL, dir.uri), + xhr = new XMLHttpRequest(); + // + xhr.open("DELETE", serviceURL, false); + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + // + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // List the contents of a directory/folder + // Parameters: + // the dir parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/dir/subdir" + // recursive: boolean true to list contents of all subdirectories as well. if this is not specified "false" is the default. + // returnType: string "all", "files", "directories". Specifies the types to return. if this is not specified, the default is "all" + // fileFilters: string containing the file extensions to include in the return listing. This list is semi-colon separated. i.e. "xml;html" + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // content: string containing the JSON structure of the file contents + // status: int indicating the request HTTP status code + // 200 - the directory was read and the content JSON string was returned in dir.content + // 404 - the directory does not exist + // 500 - unknown server error occurred + getDirectoryContents: { + enumerable: false, + value: function(dir) { + var retValue = { success:null, content:null, status:null }; + if(!!dir && (typeof dir.uri !== "undefined") && (dir.uri !== null) ) { + try { + var serviceURL = this._prepareServiceURL(this._directoryServiceURL, dir.uri), + xhr = new XMLHttpRequest(); + // + xhr.open("GET", serviceURL, false); + // + if(dir.recursive) { + xhr.setRequestHeader("recursive", dir.recursive.toString()); + } + + // + if (dir.fileFilters) { + xhr.setRequestHeader("file-filters", dir.fileFilters.toString()); + } + // + + // + if(dir.returnType) { + xhr.setRequestHeader("return-type", dir.returnType.toString()); + } + // + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + if(xhr.status == 200) { + retValue.content = xhr.responseText; + } + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + // + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // Check if a directory/folder exists + // Parameters: + // the dir parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/dir/subdir" + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - the directory exists + // 404 - the directory does not exist + // 500 - unknown server error occurred + directoryExists: { + enumerable: false, + value: function(dir) { + var retValue = { success:null, content:null, status:null }; + if(dir && dir.uri && dir.uri.length) { + try { + var serviceURL = this._prepareServiceURL(this._directoryServiceURL, dir.uri), + xhr = new XMLHttpRequest(); + // + xhr.open("GET", serviceURL, false); + xhr.setRequestHeader("check-existence-only", "true"); + // + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + // + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // copies an existing directory/folder to a new location + // Parameters: + // the dir parameter must contain the following properties + // sourceUri: string value containing the full file path/URI to copy from i.e. "c:/foo/bar" + // destUri: string containing the full path/URI to copy to + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - the operation succeeded + // 400 - the operation could not be performed because the destUri existed + // 404 - the source directory does not exist + // 500 - unknown server error occurred + copyDirectory: { + enumerable: false, + value: function(dir) { + return this._copyMoveDirHelper(dir.sourceUri, dir.destUri, "copy"); + } + }, + //////////////////////////////////////////////////////////////////// + // Moves an existing directory/folder to a new location + // Parameters: + // the dir parameter must contain the following properties + // sourceUri: string value containing the full file path/URI to copy from i.e. "c:/foo/bar" + // destUri: string containing the full path/URI to move to + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - the operation succeeded + // 400 - the operation could not be performed because the destUri existed + // 404 - the source directory does not exist + // 500 - unknown server error occurred + moveDirectory: { + enumerable: false, + value: function(dir) { + return this._copyMoveDirHelper(dir.sourceUri, dir.destUri, "move"); + } + }, + //////////////////////////////////////////////////////////////////// + // Moves an existing directory/folder to a new location + // Parameters: + // the dir parameter must contain the following properties + // sourceUri: string value containing the full file path/URI to copy from i.e. "c:/foo/bar" + // newDirectoryName: string containing the new name of the directory i.e. "bar2" + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - the operation succeeded + // 400 - the operation could not be performed because the destUri existed + // 404 - the source directory does not exist + // 500 - unknown server error occurred + renameDirectory: { + enumerable: false, + value: function(dir) { + return this._copyMoveDirHelper(dir.sourceUri, dir.sourceUri + "/" + dir.newDirectoryName, "move"); + } + }, + //////////////////////////////////////////////////////////////////// + //Helper that is used by copyDirectory, moveDirectory, renameDirectory + _copyMoveDirHelper: { + enumerable: false, + value: function(sourceDir, destDir, operation) { + var retValue = {}; + if(sourceDir && sourceDir.length && destDir && destDir.length && operation && operation.length) { + try { + var serviceURL = this._prepareServiceURL(this._directoryServiceURL, destDir), + xhr = new XMLHttpRequest(); + // + xhr.open("PUT", serviceURL, false); + xhr.setRequestHeader("sourceURI", sourceDir); + xhr.setRequestHeader("operation", operation); + // + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + // + return retValue; + } + }, + //////////////////////////////////////////////////////////////////// + // Checks if the file has been modified since it was last queried + // Parameters: + // the file parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" + // recursive: boolean true to check the modified date of all subdirectories as well. if this is not specified "false" is the default. + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 200 - the file has been modified + // 304 - the file has not been modified + // 404 - the file does not exist + // 500 - unknown server error occurred + + isFileModified:{ + enumerable:true, + writable:false, + value:function(file, lastQueriedTimestamp){ + var retValue = { success:null, status:null }; + if(file && file.uri && (typeof lastQueriedTimestamp !== "undefined")) { + try { + var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + xhr = new XMLHttpRequest(); + xhr.open("GET", serviceURL, false); + xhr.setRequestHeader("if-modified-since", lastQueriedTimestamp); + xhr.send(); + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + return retValue; + } + }, + + //////////////////////////////////////////////////////////////////// + // Checks if the directory content has been modified since it was last queried + // Parameters: + // the file parameter must contain the following properties + // uri: string value containing the full directory path/URI i.e. "c:/foo/bar.html" + // recursive: boolean true to check the modified date of all subdirectories as well. if this is not specified "false" is the default. + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 200 - the file has been modified + // 304 - the file has not been modified + // 404 - the file does not exist + // 500 - unknown server error occurred + + isDirectoryModified:{ + enumerable:true, + writable:false, + value:function(file, lastQueriedTimestamp){ + var retValue = { success:null, status:null }; + if(file && file.uri && (typeof lastQueriedTimestamp !== "undefined")) { + try { + var serviceURL = this._prepareServiceURL(this._directoryServiceURL, file.uri), + xhr = new XMLHttpRequest(); + xhr.open("GET", serviceURL, false); + xhr.setRequestHeader("if-modified-since", lastQueriedTimestamp); + xhr.send(); + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + return retValue; + } + }, + + //////////////////////////////////////////////////////////////////// + // Checks if the file is writable + // Parameters: + // the file parameter must contain the following properties + // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - The file exists and response body has writable flag + // 404 - the file does not exist + // 500 - unknown server error occurred + //TODO:to be finalized + isFileWritable:{ + enumerable:true, + writable:false, + value:function(file){ + var retValue = { success:null, status:null }; + if(file && file.uri) { + try { + var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + xhr = new XMLHttpRequest(); + xhr.open("GET", serviceURL, false); + xhr.setRequestHeader("get-attributes", "true"); + xhr.send(); + if (xhr.readyState === 4) { + retValue.status = xhr.status; + if(xhr.status == 200) { + retValue.content = xhr.responseText; + } + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + return retValue; + } + }, + + //////////////////////////////////////////////////////////////////// + // Checks if the directory is writable + // Parameters: + // the file parameter must contain the following properties + // uri: string value containing the full directory path/URI i.e. "c:/foo" + // + // Return values: + // returns an object with two properties + // success: boolean indicating if the call succeeded or failed + // status: int indicating the request HTTP status code + // 204 - The file exists and response body has writable flag + // 404 - the file does not exist + // 500 - unknown server error occurred + //TODO:to be finalized + isDirectoryWritable:{ + enumerable:true, + writable:false, + value:function(file){ + var retValue = { success:null, status:null }; + if(file && file.uri) { + try { + var serviceURL = this._prepareServiceURL(this._directoryServiceURL, file.uri), + xhr = new XMLHttpRequest(); + xhr.open("GET", serviceURL, false); + xhr.setRequestHeader("get-attributes", "true"); + xhr.send(); + if (xhr.readyState === 4) { + retValue.status = xhr.status; + if(xhr.status == 200) { + retValue.content = xhr.responseText; + } + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + } + return retValue; + } + } + + + +}); +//////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////// \ No newline at end of file -- cgit v1.2.3 From 4d5f9c451524829f55ddf26642cb9fc28228b6ce Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Sun, 29 Jan 2012 17:27:29 -0800 Subject: Core API clean up Cleaning up File IO core API and setting up to use URL from welcome screen or local storage. Class will perform an automatic check for IO API to be active, otherwise prompt user. --- js/io/system/coreioapi.js | 127 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 20 deletions(-) (limited to 'js/io/system/coreioapi.js') diff --git a/js/io/system/coreioapi.js b/js/io/system/coreioapi.js index c920d8cd..1585fc33 100755 --- a/js/io/system/coreioapi.js +++ b/js/io/system/coreioapi.js @@ -16,16 +16,103 @@ var Montage = require("montage/core/core").Montage, //Exporting as Project I/O exports.CoreIoApi = Montage.create(Component, { //////////////////////////////////////////////////////////////////// - // private property containing the file service URL to use for all file IO calls + // + deserializedFromTemplate: { + enumerable: false, + value: function () { + //////////////////////////////////////////////////////////// + + //TODO: Add logic for getting rooUrl from local storage + + //////////////////////////////////////////////////////////// + + + + //Checking for status of I/O API + this.ioDetected = this.isActive(); + //TODO: Add welcome screen logic, probably externally + } + }, + //////////////////////////////////////////////////////////////////// + //Method to check status of I/O API, will return false if not active + isActive: { + enumerable: false, + value: function () { + //Doing a directory root check, a 200 status means running + if (this.getDirectoryContents({uri:'/'}).status === 200) { + return true; + } else { + return false; + } + } + }, + //////////////////////////////////////////////////////////////////// + //Root API URL + _ioDetected: { + enumerable: false, + value: false + }, + //////////////////////////////////////////////////////////////////// + // + ioDetected: { + enumerable: false, + get: function() { + return this._ioDetected; + }, + set: function(value) { + this._ioDetected = value; + } + }, + //////////////////////////////////////////////////////////////////// + //Root API URL + _rootUrl: { + enumerable: false, + value: 'http://localhost:16380' + }, + //////////////////////////////////////////////////////////////////// + // + rootUrl: { + enumerable: false, + get: function() { + return this._rootUrl; + }, + set: function(value) { + this._rootUrl = value; + } + }, + //////////////////////////////////////////////////////////////////// + //File service API URL _fileServiceURL: { enumerable: false, - value: "http://localhost:16380/file" + value: '/file' + }, + //////////////////////////////////////////////////////////////////// + // + fileServiceURL: { + enumerable: false, + get: function() { + return this.rootUrl+this._fileServiceURL; + }, + set: function(value) { + this._fileServiceURL = value; + } }, //////////////////////////////////////////////////////////////////// - // private property containing the directory service URL to use for all file IO calls + //Directory service API URL _directoryServiceURL: { enumerable: false, - value: "http://localhost:16380/directory" + value: '/directory' + }, + //////////////////////////////////////////////////////////////////// + // + directoryServiceURL: { + enumerable: false, + get: function() { + return this.rootUrl+this._directoryServiceURL; + }, + set: function(value) { + this._directoryServiceURL = value; + } }, //////////////////////////////////////////////////////////////////// // private helper to parse URIs and append them to the service URL @@ -68,7 +155,7 @@ exports.CoreIoApi = Montage.create(Component, { // if(file && file.uri && file.uri.length) { try { - var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.uri), xhr = new XMLHttpRequest(); // xhr.open("GET", serviceURL, false); @@ -111,7 +198,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(file && file.uri && file.uri.length) { try { - var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.uri), xhr = new XMLHttpRequest(); // xhr.open("POST", serviceURL, false); @@ -161,7 +248,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(file && file.uri && file.uri.length && file.contents && file.contents.length) { try { - var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.uri), xhr = new XMLHttpRequest(); // xhr.open("PUT", serviceURL, false); @@ -208,7 +295,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(file && file.sourceUri && file.sourceUri.length && file.destUri && file.destUri.length) { try { - var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.destUri), + var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.destUri), xhr = new XMLHttpRequest(); // xhr.open("PUT", serviceURL, false); @@ -256,7 +343,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(file && file.sourceUri && file.sourceUri.length && file.destUri && file.destUri.length) { try { - var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.destUri), + var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.destUri), xhr = new XMLHttpRequest(); // xhr.open("PUT", serviceURL, false); @@ -302,7 +389,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(file && file.uri && file.uri.length) { try { - var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.uri), xhr = new XMLHttpRequest(); // xhr.open("DELETE", serviceURL, false); @@ -344,7 +431,7 @@ exports.CoreIoApi = Montage.create(Component, { // if(file && file.uri && file.uri.length) { try { - var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.uri), xhr = new XMLHttpRequest(); // xhr.open("GET", serviceURL, false); @@ -386,7 +473,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(dir && dir.uri && dir.uri.length) { try { - var serviceURL = this._prepareServiceURL(this._directoryServiceURL, dir.uri), + var serviceURL = this._prepareServiceURL(this.directoryServiceURL, dir.uri), xhr = new XMLHttpRequest(); // xhr.open("POST", serviceURL, false); @@ -425,7 +512,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(dir && dir.uri && dir.uri.length) { try { - var serviceURL = this._prepareServiceURL(this._directoryServiceURL, dir.uri), + var serviceURL = this._prepareServiceURL(this.directoryServiceURL, dir.uri), xhr = new XMLHttpRequest(); // xhr.open("DELETE", serviceURL, false); @@ -468,7 +555,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, content:null, status:null }; if(!!dir && (typeof dir.uri !== "undefined") && (dir.uri !== null) ) { try { - var serviceURL = this._prepareServiceURL(this._directoryServiceURL, dir.uri), + var serviceURL = this._prepareServiceURL(this.directoryServiceURL, dir.uri), xhr = new XMLHttpRequest(); // xhr.open("GET", serviceURL, false); @@ -526,7 +613,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, content:null, status:null }; if(dir && dir.uri && dir.uri.length) { try { - var serviceURL = this._prepareServiceURL(this._directoryServiceURL, dir.uri), + var serviceURL = this._prepareServiceURL(this.directoryServiceURL, dir.uri), xhr = new XMLHttpRequest(); // xhr.open("GET", serviceURL, false); @@ -619,7 +706,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = {}; if(sourceDir && sourceDir.length && destDir && destDir.length && operation && operation.length) { try { - var serviceURL = this._prepareServiceURL(this._directoryServiceURL, destDir), + var serviceURL = this._prepareServiceURL(this.directoryServiceURL, destDir), xhr = new XMLHttpRequest(); // xhr.open("PUT", serviceURL, false); @@ -665,7 +752,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(file && file.uri && (typeof lastQueriedTimestamp !== "undefined")) { try { - var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.uri), xhr = new XMLHttpRequest(); xhr.open("GET", serviceURL, false); xhr.setRequestHeader("if-modified-since", lastQueriedTimestamp); @@ -707,7 +794,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(file && file.uri && (typeof lastQueriedTimestamp !== "undefined")) { try { - var serviceURL = this._prepareServiceURL(this._directoryServiceURL, file.uri), + var serviceURL = this._prepareServiceURL(this.directoryServiceURL, file.uri), xhr = new XMLHttpRequest(); xhr.open("GET", serviceURL, false); xhr.setRequestHeader("if-modified-since", lastQueriedTimestamp); @@ -747,7 +834,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(file && file.uri) { try { - var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), + var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.uri), xhr = new XMLHttpRequest(); xhr.open("GET", serviceURL, false); xhr.setRequestHeader("get-attributes", "true"); @@ -790,7 +877,7 @@ exports.CoreIoApi = Montage.create(Component, { var retValue = { success:null, status:null }; if(file && file.uri) { try { - var serviceURL = this._prepareServiceURL(this._directoryServiceURL, file.uri), + var serviceURL = this._prepareServiceURL(this.directoryServiceURL, file.uri), xhr = new XMLHttpRequest(); xhr.open("GET", serviceURL, false); xhr.setRequestHeader("get-attributes", "true"); -- cgit v1.2.3 From 2f61dfca4466661e1ea23888675a86b601b58c63 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Mon, 30 Jan 2012 14:35:11 -0800 Subject: Setting up new file Adding base functionality to creating files. --- js/io/system/coreioapi.js | 64 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 20 deletions(-) (limited to 'js/io/system/coreioapi.js') diff --git a/js/io/system/coreioapi.js b/js/io/system/coreioapi.js index 1585fc33..a10063f5 100755 --- a/js/io/system/coreioapi.js +++ b/js/io/system/coreioapi.js @@ -7,7 +7,6 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot /* ///////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// NOTES: -These methods should only be access through the file and project IO classes. //////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// */ var Montage = require("montage/core/core").Montage, @@ -20,22 +19,29 @@ exports.CoreIoApi = Montage.create(Component, { deserializedFromTemplate: { enumerable: false, value: function () { - //////////////////////////////////////////////////////////// - - //TODO: Add logic for getting rooUrl from local storage - - //////////////////////////////////////////////////////////// - - - - //Checking for status of I/O API - this.ioDetected = this.isActive(); - //TODO: Add welcome screen logic, probably externally + //Checking for local storage of URL for IO + if (window.localStorage['ioRootUrl']) { + //Getting URL from local storage + this.rootUrl = window.localStorage['ioRootUrl']; + //Checks for IO API to be active + this.ioServiceDetected = this.isIoServiceActive(); + // + console.log('FileIO: localStorage URL detected | IO Service Detected: '+ this.ioServiceDetected); + // + } else { + //TODO: Remove, automatically prompt user on welcome + this.rootUrl = 'http://localhost:16380'; + //TODO: Changed to false, welcome screen prompts user + this.ioServiceDetected = this.isIoServiceActive(); + // + console.log('FileIO: localStorage URL NOT detected | IO Service Detected: '+ this.ioServiceDetected); + // + } } }, //////////////////////////////////////////////////////////////////// //Method to check status of I/O API, will return false if not active - isActive: { + isIoServiceActive: { enumerable: false, value: function () { //Doing a directory root check, a 200 status means running @@ -47,27 +53,27 @@ exports.CoreIoApi = Montage.create(Component, { } }, //////////////////////////////////////////////////////////////////// - //Root API URL - _ioDetected: { + // + _ioServiceDetected: { enumerable: false, value: false }, //////////////////////////////////////////////////////////////////// - // - ioDetected: { + //Checking for service availability on boot + ioServiceDetected: { enumerable: false, get: function() { - return this._ioDetected; + return this._ioServiceDetected; }, set: function(value) { - this._ioDetected = value; + this._ioServiceDetected = value; } }, //////////////////////////////////////////////////////////////////// //Root API URL _rootUrl: { enumerable: false, - value: 'http://localhost:16380' + value: null }, //////////////////////////////////////////////////////////////////// // @@ -78,6 +84,24 @@ exports.CoreIoApi = Montage.create(Component, { }, set: function(value) { this._rootUrl = value; + window.localStorage["ioRootUrl"] = value; + } + }, + //////////////////////////////////////////////////////////////////// + //API service URL + _apiServiceURL: { + enumerable: false, + value: '/' + }, + //////////////////////////////////////////////////////////////////// + // + apiServiceURL: { + enumerable: false, + get: function() { + return this.rootUrl+this._apiServiceURL; + }, + set: function(value) { + this._apiServiceURL = value; } }, //////////////////////////////////////////////////////////////////// -- cgit v1.2.3 From 3b4291c783c4b8fb07f111a240049069277f3c49 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Mon, 30 Jan 2012 18:19:00 -0800 Subject: Core API initialization routine Setting up the core API routine to check for cloud API availability. Also cleaned up template files for IO and set up initial string contents. --- js/io/system/coreioapi.js | 58 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 14 deletions(-) (limited to 'js/io/system/coreioapi.js') diff --git a/js/io/system/coreioapi.js b/js/io/system/coreioapi.js index a10063f5..33c7bc28 100755 --- a/js/io/system/coreioapi.js +++ b/js/io/system/coreioapi.js @@ -24,7 +24,7 @@ exports.CoreIoApi = Montage.create(Component, { //Getting URL from local storage this.rootUrl = window.localStorage['ioRootUrl']; //Checks for IO API to be active - this.ioServiceDetected = this.isIoServiceActive(); + this.ioServiceDetected = this.cloudAvailable(); // console.log('FileIO: localStorage URL detected | IO Service Detected: '+ this.ioServiceDetected); // @@ -32,7 +32,7 @@ exports.CoreIoApi = Montage.create(Component, { //TODO: Remove, automatically prompt user on welcome this.rootUrl = 'http://localhost:16380'; //TODO: Changed to false, welcome screen prompts user - this.ioServiceDetected = this.isIoServiceActive(); + this.ioServiceDetected = this.cloudAvailable(); // console.log('FileIO: localStorage URL NOT detected | IO Service Detected: '+ this.ioServiceDetected); // @@ -41,13 +41,16 @@ exports.CoreIoApi = Montage.create(Component, { }, //////////////////////////////////////////////////////////////////// //Method to check status of I/O API, will return false if not active - isIoServiceActive: { + cloudAvailable: { enumerable: false, value: function () { - //Doing a directory root check, a 200 status means running - if (this.getDirectoryContents({uri:'/'}).status === 200) { + // + if (this.getCloudStatus().status === 200) { + //Active return true; } else { + //Inactive + //TODO: Logic to prompt the user for cloud, otherwise return false return false; } } @@ -83,22 +86,21 @@ exports.CoreIoApi = Montage.create(Component, { return this._rootUrl; }, set: function(value) { - this._rootUrl = value; - window.localStorage["ioRootUrl"] = value; + this._rootUrl = window.localStorage["ioRootUrl"] = value; } }, //////////////////////////////////////////////////////////////////// //API service URL _apiServiceURL: { enumerable: false, - value: '/' + value: '/cloudstatus' }, //////////////////////////////////////////////////////////////////// // apiServiceURL: { enumerable: false, get: function() { - return this.rootUrl+this._apiServiceURL; + return String(this.rootUrl+this._apiServiceURL); }, set: function(value) { this._apiServiceURL = value; @@ -115,7 +117,7 @@ exports.CoreIoApi = Montage.create(Component, { fileServiceURL: { enumerable: false, get: function() { - return this.rootUrl+this._fileServiceURL; + return String(this.rootUrl+this._fileServiceURL); }, set: function(value) { this._fileServiceURL = value; @@ -132,7 +134,7 @@ exports.CoreIoApi = Montage.create(Component, { directoryServiceURL: { enumerable: false, get: function() { - return this.rootUrl+this._directoryServiceURL; + return String(this.rootUrl+this._directoryServiceURL); }, set: function(value) { this._directoryServiceURL = value; @@ -921,10 +923,38 @@ exports.CoreIoApi = Montage.create(Component, { } return retValue; } + }, + //////////////////////////////////////////////////////////////////// + // + getCloudStatus: { + enumerable: false, + writable:false, + value: function() { + // + var retValue = {success:null, status:null}; + // + try { + var serviceURL = this._prepareServiceURL(this.apiServiceURL, '/'), + xhr = new XMLHttpRequest(); + // + xhr.open("GET", serviceURL, false); + xhr.send(); + // + if (xhr.readyState === 4) { + retValue.status = xhr.status; + retValue.success = true; + } + } + catch(error) { + xhr = null; + retValue.success = false; + } + // + return retValue; + } } - - - + //////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////// }); //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// \ No newline at end of file -- cgit v1.2.3 From 906776893138257f96a0530674eda456ca3d817b Mon Sep 17 00:00:00 2001 From: Ananya Sen Date: Tue, 31 Jan 2012 06:24:03 -0800 Subject: added the api to check if a file is writable Signed-off-by: Ananya Sen --- js/io/system/coreioapi.js | 47 +---------------------------------------------- 1 file changed, 1 insertion(+), 46 deletions(-) (limited to 'js/io/system/coreioapi.js') diff --git a/js/io/system/coreioapi.js b/js/io/system/coreioapi.js index 1585fc33..c99ebda7 100755 --- a/js/io/system/coreioapi.js +++ b/js/io/system/coreioapi.js @@ -837,50 +837,7 @@ exports.CoreIoApi = Montage.create(Component, { var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.uri), xhr = new XMLHttpRequest(); xhr.open("GET", serviceURL, false); - xhr.setRequestHeader("get-attributes", "true"); - xhr.send(); - if (xhr.readyState === 4) { - retValue.status = xhr.status; - if(xhr.status == 200) { - retValue.content = xhr.responseText; - } - retValue.success = true; - } - } - catch(error) { - xhr = null; - retValue.success = false; - } - } - return retValue; - } - }, - - //////////////////////////////////////////////////////////////////// - // Checks if the directory is writable - // Parameters: - // the file parameter must contain the following properties - // uri: string value containing the full directory path/URI i.e. "c:/foo" - // - // Return values: - // returns an object with two properties - // success: boolean indicating if the call succeeded or failed - // status: int indicating the request HTTP status code - // 204 - The file exists and response body has writable flag - // 404 - the file does not exist - // 500 - unknown server error occurred - //TODO:to be finalized - isDirectoryWritable:{ - enumerable:true, - writable:false, - value:function(file){ - var retValue = { success:null, status:null }; - if(file && file.uri) { - try { - var serviceURL = this._prepareServiceURL(this.directoryServiceURL, file.uri), - xhr = new XMLHttpRequest(); - xhr.open("GET", serviceURL, false); - xhr.setRequestHeader("get-attributes", "true"); + xhr.setRequestHeader("get-file-info", "true"); xhr.send(); if (xhr.readyState === 4) { retValue.status = xhr.status; @@ -899,8 +856,6 @@ exports.CoreIoApi = Montage.create(Component, { } } - - }); //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// \ No newline at end of file -- cgit v1.2.3 From e75223a2c4c1e13d66639841e6418e94fe9b726f Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Tue, 31 Jan 2012 15:59:46 -0800 Subject: Cloud IO Prompt Setting up UI for the file IO prompt on initialization of Ninja and whenever a cloud IO call is made and the server is not detected. --- js/io/system/coreioapi.js | 84 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 73 insertions(+), 11 deletions(-) (limited to 'js/io/system/coreioapi.js') diff --git a/js/io/system/coreioapi.js b/js/io/system/coreioapi.js index 33c7bc28..4407d59a 100755 --- a/js/io/system/coreioapi.js +++ b/js/io/system/coreioapi.js @@ -10,7 +10,9 @@ NOTES: //////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// */ var Montage = require("montage/core/core").Montage, - Component = require("montage/ui/component").Component; + Component = require("montage/ui/component").Component, + Popup = require("js/components/popup.reel").Popup, + CloudPopup = require("js/io/ui/cloudpopup.reel").CloudPopup; //////////////////////////////////////////////////////////////////////// //Exporting as Project I/O exports.CoreIoApi = Montage.create(Component, { @@ -26,16 +28,11 @@ exports.CoreIoApi = Montage.create(Component, { //Checks for IO API to be active this.ioServiceDetected = this.cloudAvailable(); // - console.log('FileIO: localStorage URL detected | IO Service Detected: '+ this.ioServiceDetected); - // + console.log('Cloud Status: URL detected in localStorage as '+this.rootUrl); } else { - //TODO: Remove, automatically prompt user on welcome - this.rootUrl = 'http://localhost:16380'; - //TODO: Changed to false, welcome screen prompts user - this.ioServiceDetected = this.cloudAvailable(); - // - console.log('FileIO: localStorage URL NOT detected | IO Service Detected: '+ this.ioServiceDetected); // + this.ioServiceDetected = false; + console.log('Cloud Status: No URL detected in localStorage'); } } }, @@ -45,18 +42,83 @@ exports.CoreIoApi = Montage.create(Component, { enumerable: false, value: function () { // - if (this.getCloudStatus().status === 200) { + if (this.rootUrl && this.getCloudStatus().status === 200) { //Active return true; } else { //Inactive - //TODO: Logic to prompt the user for cloud, otherwise return false + if (!this._cloudDialogOpen || this.application.ninja) { + this.showCloudDialog(); + } return false; } } }, //////////////////////////////////////////////////////////////////// // + _cloudDialogOpen: { + enumerable: false, + value: false + }, + //////////////////////////////////////////////////////////////////// + // + _cloudDialogComponents: { + enumerable: false, + value: {blackout: null, popup: null, dialog: null} + }, + ///////