forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemoteFile.js
More file actions
1 lines (1 loc) · 3.73 KB
/
RemoteFile.js
File metadata and controls
1 lines (1 loc) · 3.73 KB
1
define(function(require,exports,module){const FileSystemError=require("filesystem/FileSystemError"),FileSystemStats=require("filesystem/FileSystemStats"),NodeUtils=require("utils/NodeUtils"),SESSION_START_TIME=new Date;function _getStats(uri){return new FileSystemStats({isFile:!0,mtime:SESSION_START_TIME.toISOString(),size:0,realPath:uri,hash:uri})}function _getFileName(filePath){var fileName=filePath.split("/").pop();return fileName.trim()||(fileName=(fileName=filePath.trim().slice(0,-1)).split("/").pop()),fileName}function RemoteFile(protocol,fullPath,fileSystem){this._isFile=!0,this._isDirectory=!1,this.readOnly=!0,this._path=fullPath,this._stat=_getStats(fullPath),this._id=fullPath,this._name=_getFileName(fullPath),this._fileSystem=fileSystem,this.donotWatch=!0,this.protocol=protocol,this.encodedPath=fullPath}function _nodeConnectorRead(url,encoding,successCB,errorCB){NodeUtils.fetchURLText(url,encoding).then(successCB).catch(err=>{console.error("failed fetch url: ",url,err),errorCB(err)})}function isTauriResource(url){const startingURLs=["phtauri://","https://phtauri.localhost","asset://","https://asset.localhost","tauri://","https://tauri.localhost"];for(let start of startingURLs)if(url.startsWith(start))return!0;return!1}function _remoteRead(url,encoding,successCB,errorCB){if(Phoenix.isNativeApp&&!isTauriResource(url))return void _nodeConnectorRead(url,encoding,successCB,errorCB);let xmlhttp=new XMLHttpRequest;xmlhttp.open("GET",url,!0),xmlhttp.responseType="arraybuffer",xmlhttp.onload=function(oEvent){const arrayBuffer=xmlhttp.response;try{successCB(iconv.decode(Buffer.from(arrayBuffer),encoding))}catch(err){errorCB(err)}},xmlhttp.onerror=function(err){errorCB(err)},xmlhttp.send()}Object.defineProperties(RemoteFile.prototype,{fullPath:{get:function(){return this._path},set:function(){throw new Error("Cannot set fullPath")}},name:{get:function(){return this._name},set:function(){throw new Error("Cannot set name")}},parentPath:{get:function(){return this._parentPath},set:function(){throw new Error("Cannot set parentPath")}},id:{get:function(){return this._id},set:function(){throw new Error("Cannot set id")}},isFile:{get:function(){return this._isFile},set:function(){throw new Error("Cannot set isFile")}},isDirectory:{get:function(){return this._isDirectory},set:function(){throw new Error("Cannot set isDirectory")}},_impl:{get:function(){return this._fileSystem._impl},set:function(){throw new Error("Cannot set _impl")}}}),RemoteFile.prototype.toString=function(){return"[RemoteFile "+this._path+"]"},RemoteFile.prototype.stat=function(callback){this._stat?callback(null,this._stat):callback(FileSystemError.NOT_FOUND)},RemoteFile.prototype.constructor=RemoteFile,RemoteFile.prototype._contents=null,RemoteFile.prototype._encoding="utf8",RemoteFile.prototype._preserveBOM=!1,RemoteFile.prototype._clearCachedData=function(){},RemoteFile.prototype.read=function(options,callback){if("function"==typeof options&&(callback=options,options={}),this._encoding=options.encoding||"utf8",null!==this._contents&&this._stat)callback(null,this._contents,this._encoding,this._stat);else{var self=this;_remoteRead(this.fullPath,this._encoding,function(data){self._contents=data,callback(null,data,self._encoding,self._stat)},function(e){callback(FileSystemError.NOT_FOUND)})}},RemoteFile.prototype.write=function(data,encoding,callback){"function"==typeof encoding&&(callback=encoding),callback(FileSystemError.NOT_FOUND)},RemoteFile.prototype.exists=function(callback){callback(null,!0)},RemoteFile.prototype.unlink=function(callback){callback(FileSystemError.NOT_FOUND)},RemoteFile.prototype.rename=function(newName,callback){callback(FileSystemError.NOT_FOUND)},RemoteFile.prototype.moveToTrash=function(callback){callback(FileSystemError.NOT_FOUND)},module.exports=RemoteFile});