How to access filemanager view from proxy

Hello,
I’m developing a filemanager which makes use of a proxy to access the server.
I can’t find a way to access the filemanager data from the load method of the proxy. In the params I get the file id, while I need the filename, so I need to make a lookup in the data of the filemanager. But how to access it ?
If someone can help me I wojuld be very gratefull
Thanks you

Antonio

Ok, I came up with the following code, where I try to call getItem(id) to get the filename from the id. The problem is that the load method is called with view = null, so the code view.getItem(fileId).value goes into error.
I tried with const $$(files).getItem(fieldId) but I get the same error.
What am I doing wrong ?

Thanks

Antonio


var myProxy  = {
	$proxy:true,
	
	load: function(view, params){
		
		if (!params) {
			// Retrieve the content of the home directory
			return getHomeDirectory();
		}
		else {
			if (params.action == "create") {
				
				// Retrieve the filename
				const fileId = params.source;
				const filename = view.getItem(fileId).value;
				
				// Create a new file or directory
				return createFile(params.target + "/" + params.source);
			}
			else if (params.action == "rename") {
				// Rename the specified file or directory
				return renameFile(params.source, params.target)
			}
			else if (params.action == "remove") {
				// Delete the specified file or directory
				return deleteFile(params.source);
			}
		}
		return;
	}
}

Hello @antlomb, I’ve partly covered this question in your related thread, please check it out first.

I tried with const $$(files).getItem(fieldId) but I get the same error. What am I doing wrong ?

This depends on the specific handler this method was called from. For example, if you were to call the getItem() method from within the “delete” handler you won’t be able to get the item, since it would already be removed from the DataStore, hence why the method returns undefined in this case (the full path to the deleted item is still stored in the params argument by default - params.source).

Depending on your use case, certain workarounds might be possible. For instance, it is important to know whether you are planning to make calls to the API that will trigger these handlers or if you are going to trigger them via user action (context menu). If you are simply using a context menu, then you could get your data based on the currently selected item, although you would still need to use a global variable (by default, when you right click an item to call the context menu it does get selected). As a rough example - https://snippet.webix.com/dqbg723v.

Thanks again @dzmitry, with this and your other answer in the other thread I think I found the solution.

By the way, @dzmitry, when you say:

“the “delete” handler you won’t be able to get the item, since it would already be removed from the DataStore, hence why the method returns undefined in this case (the full path to the deleted item is still stored in the params argument by default - params.source).”

well, in my case, the params.source in the delete handler contains the id of item to be deleted, not the full path.

In this case, how can I get the full path which has been deleted ?

and another problem, the rename handler seems to get in the source the id of the new file and in the target the filename of the new file. In other words, in the params there are no references to the old file. How can I call a server to rename the file without knowing the old and the new filenames ?
Can you help me here also ?

well, in my case, the params.source in the delete handler contains the id of item to be deleted, not the full path.

Apologies, I’ve made it sound more confusing than it is. Our backend is configured in such a way that every item’s id is equal to its full path, which is why this works for our sample. Incidentally, this could also work as your potential solution (reconfiguring your backend to act in a similar way).

In this case, how can I get the full path which has been delete

Could you please specify if you are calling the following commands via a context menu or are you also making the calls to the API directly?

If you are using the context menu exclusively you could get the correct id with the help of the onItemSelect event handler (the item gets selected as you call the context menu over it), as previously mentioned, here is an approximate solution - https://snippet.webix.com/dqbg723v (alternative in the case our backend is down - https://snippet.webix.com/pmqs7duv).

An alternative solution agnostic to this condition would be to get the item reference just before it gets deleted via the onBeforeDelete event handler, here is an example - https://snippet.webix.com/vgu480kf (I am using this sample instead of the one with a backend since our backend is down at the moment, please also note that in this case we are working with item ids that do not consist of its path).

  • I used getPathNames and not getPath, which returns ids and not values

The getPath() method does actually return ids as well, so I’m not quite sure what the issue in your case might have been. The getPathNames() method returns an array of id-value pairs while the getPath() method returns an array of ids exclusively.

the rename handler seems to get in the source the id of the new file and in the target the filename of the new file

Is that actually the case? Judging from my limited testing, the “source” parameter points to the id of the original item that was renamed (old file). This can actually be seen from our server-side sample, since the item’s ids depend on the actual name of the file (and its path), so if you were to rename it - its id would change as well. Here is the sample - https://snippet.webix.com/w9tt2qvb. Please note that the “target” parameter points to the old “name” (id) of the file.

Hello, @dzmitry, many many thanks for all the help.
Yes, getPath() returns only ids, that’s why I used getPathNames which returns id-value pairs. In my case the server side does not store the ids and I needed the values.
Following your input I used the onBeforeDelete event handler to get the info about the deleted file/folder. I preferred not using the onItemSelect, even if, up to now, I’m just deleting files from the context menus.
About the rename handler that was my fault, sorry, you’re correct.
I’m quite done … now I’m working with downloading and uploading, thanks again for all the help.

Hi, is there difference between the proxy sources or the place you are using the internet? I could run the code in the US but now I’m in my portuguese property Property for sale in Portugal — Portuguese real estate and the code just doesn’t work.

Hey @phil_p,

Hi, is there difference between the proxy sources or the place you are using the internet?

There should be no difference based off the location alone (given that you are trying to run the exact same code). As I see it, given your circumstances, the issue may arise when the target website is unreachable via your network and you are sending server requests to that target.

Could you please share the code you are trying to run to investigate your issue further? Also, if possible, could you please provide more details about the setup you are trying to run your code in?