+ //File Manager methods
+ getFilelistSuccess(data) {
+ let self = this;
+ let filesState = null;
+ if (self.fileMonitoringSocketID) {
+ let newState = {};
+ if(data.hasOwnProperty('contents')) {
+ filesState = updateFileState( _cloneDeep(this.filesState),data);
+ let normalizedData = normalizeTree(data);
+ newState = {
+ files: {
+ data: _mergeWith(normalizedData.data, self.files.data, function(obj, src) {
+ return _uniqBy(obj? obj.concat(src) : src, 'name');
+ }),
+ id: normalizedData.id
+ },
+ filesState: filesState
+ }
+ } else {
+ newState = {
+ files: false
+ }
+ }
+ if(!_isEqual(newState.files, this.files) || ! _isEqual(newState.fileState, this.fileState)) {
+ this.setState(newState);
+ }
+
+ }
+ function normalizeTree(data) {
+ let f = {
+ id:[],
+ data:{}
+ };
+ function getContents(d) {
+ if(d.hasOwnProperty('contents')) {
+ let contents = [];
+ d.contents.map(function(c,i) {
+ if (!c.hasOwnProperty('contents')) {
+ contents.push(c);
+ } else {
+ getContents(c);
+ }
+ })
+ f.id.push(d.name);
+ f.data[d.name] = contents;
+ }
+ }
+ getContents(data);
+ return f;
+ }
+ function updateFileState(obj, d) {
+ d.newFile = '';
+ if(d.hasOwnProperty('contents')) {
+ d.contents.map(updateFileState.bind(null, obj))
+ }
+ // override any "pending" state we may have initialized
+ obj[d.name] = '';
+ return obj;
+ }
+ }
+ sendDownloadFileRequest(data) {
+ let id = data.id || this.item.id;
+ let type = data.type || this.item.uiState.type;
+ let assetType = data.assetType;
+ let path = data.path;
+ let url = data.url;
+ this.getInstance().addFile(id, type, assetType, path, url, data.refresh);
+ }
+ updateFileLocationInput = (data) => {
+ let name = data.name;
+ let value = data.value;
+ var filesState = _cloneDeep(this.filesState);
+ filesState[name] = value;
+ this.setState({
+ filesState: filesState
+ });
+ }
+ addFileSuccess = (data) => {
+ if(!data.refresh) {
+ let path = data.path;
+ if (path.startsWith('readme')) {
+ // this asset type stuff should be in a more common location
+ // this is a wee bit of a hack till it is
+ path = '.' + path.slice(6);
+ }
+ let fileName = data.fileName;
+ let files = _cloneDeep(this.files);
+ let assetGroup = files.data[path] || [];
+ if (fileName) {
+ let name = path + '/' + fileName;
+ if (assetGroup.findIndex(f => f.name === name) == -1){
+ assetGroup.push({name});
+ }
+ }
+ files.data[path] = assetGroup;
+ if (files.id.indexOf(path) == -1){
+ files.id.push(path);
+ }
+ let filesState = _cloneDeep(this.filesState);
+ filesState[name] = "DOWNLOADING";
+ this.setState({files, filesState});
+ }
+
+ }
+ startWatchingJob = () => {
+ let ws = window.multiplexer.channel(this.jobSocketId);
+ this.setState({
+ jobSocket:null
+ })
+ }
+ openDownloadMonitoringSocketSuccess = (id) => {
+ let self = this;
+ let ws = window.multiplexer.channel(id);
+ let downloadJobs = _cloneDeep(self.downloadJobs);
+ let newFiles = false;
+ ws.onmessage = (socket) => {
+ if (self.files && self.files.length > 0) {
+ let jobs = [];
+ try {
+ jobs = JSON.parse(socket.data);
+ } catch(e) {}
+ newFiles = _cloneDeep(self.files);
+ jobs.map(function(j) {
+ //check if not in completed state
+ let fullPath = j['package-path'];
+ let path = fullPath.split('/');
+ let fileName = path.pop();
+ path = path.join('/');
+ let index = _findIndex(self.files.data[path], function(o){
+ return fullPath == o.name
+ });
+ if((index > -1) && newFiles.data[path][index]) {
+ newFiles.data[path][index].status = j.status
+ } else {
+ if(j.status.toUpperCase() == 'LOADING...' || j.status.toUpperCase() == 'IN_PROGRESS') {
+ newFiles.data[path].push({
+ status: j.status,
+ name: fullPath
+ })
+ } else {
+ // if ()
+ }
+ }
+ })
+ self.setState({
+ files: newFiles
+ })
+ // console.log(JSON.parse(socket.data));
+ }
+ }
+ this.setState({
+ jobSocketId: id,
+ jobSocket: ws
+ })
+
+ }
+ getFilelistSocketSuccess = (id) => {
+ let self = this;
+ let ws = window.multiplexer.channel(id);
+ ws.onmessage = (socket) => {
+ if (self.fileMonitoringSocketID) {
+ let data = [];
+ try {
+ data = JSON.parse(socket.data);
+ } catch(e) {}
+ self.getFilelistSuccess(data)
+ }
+ }
+
+ this.setState({
+ filesState: [],
+ files: {
+ id:[],
+ data:{}
+ },
+ fileMonitoringSocketID: id,
+ fileMonitoringSocket: ws
+ })
+
+ }
+ closeFileManagerSockets() {
+ this.fileMonitoringSocketID = null;
+ this.setState({
+ jobSocketId : null,
+ fileMonitoringSocketID : null
+ // jobSocket : null,
+ // fileMonitoringSocket : null,
+ });
+ this.jobSocket && this.jobSocket.close();
+ this.fileMonitoringSocket && this.fileMonitoringSocket.close();
+ console.log('closing');
+ }
+ openFileManagerSockets(i) {
+ let self = this;
+ let item = i || self.item;
+ // this.closeFileManagerSockets();
+ this.getInstance().openFileMonitoringSocket(item.id, item.uiState.type).then(function() {
+ // // self.getInstance().openDownloadMonitoringSocket(item.id);
+ });
+ this.getInstance().openDownloadMonitoringSocket(item.id);
+ }
+ endWatchingJob(id) {
+
+ }
+ deletePackageFile(asset) {
+ let {assetType, path} = asset;
+ let id = this.item.id;
+ let type = this.item.uiState.type;
+ this.getInstance().deleteFile(id, type, assetType, path);
+ }
+ deleteFileSuccess = (data) => {
+ let name = null;
+ let path = null;
+ if (data.assetFolder === 'readme'){
+ // freak'n root folder is special
+ name = data.path;
+ path = ['.'];
+ } else {
+ name = data.assetFolder + '/' + data.path;
+ path = name.split('/');
+ path.pop();
+ }
+ let files = _cloneDeep(this.files);
+ let filesForPath = files.data[path.join('/')]
+ _remove(filesForPath, function(c) {
+ return c.name == name;
+ });
+
+ this.setState({
+ files: files
+ })
+ }
+ deleteFileError = (error) => {
+ const filepath = error.path;
+ const message = error.data && error.data.output ? ' (' + error.data.output['error-trace'] + ')' : ' (server error)';
+ console.log('Unable to delete', filepath, 'Error:', message);
+ ComposerAppActions.showError.defer({
+ errorMessage: 'Unable to delete ' + filepath + message + '. '
+ });
+ }
+
+ newPathNameUpdated = (event) => {
+ const value = event.target.value;
+ this.setState({
+ newPathName: value
+ })
+ }
+ createDirectory = (assetType) => {
+ console.log(this.newPathName);
+ this.sendDownloadFileRequest({
+ id: this.item.id,
+ type: this.item.uiState.type,
+ assetType: assetType,
+ path: this.newPathName,
+ url: utils.getSearchParams(window.location).dev_download_server || window.location.protocol + '//' + window.location.host,
+ refresh: true
+ });
+ this.setState({
+ newPathName: ''
+ })
+ }