[deepamehta-devel] How to use the FilesService aas of 4.7

Jörg Richter jri at deepamehta.de
Wed Nov 11 18:02:05 CET 2015

Hi Malte,

thats great you adapt your plugins to DM 4.7!
Most of the Files API methods expect a path. This is always a "repository path", that is relative to the file repo base directory.

In conjunction with the per-workspace file repo feature as of DM 4.7 the semantics of "repository path" has slightly changed: if the per-workspace file repos are activated (dm4.filerepo.per_workspace=true) the repository path is expected to contain the workspace prefix as the first path segment, e.g. "/workspace-1234" where 1234 is the workspace ID.

However there is one exception to that rule: if and only if "/" is passed as the repository path the workspace prefix is determined automatically with the semantics of "current workspace". That means DM inspects the request's workspace cookie in this case (and only in this case).

You might ask, why DM doesn't determine the workspace prefix ALWAYS automatically? There are applications for which the semantics of "current workspace" is not flexible enough. Consider the standard file browser: when browsing the contents of a public Folder topic while you're in your private workspace, the workspace of the Folder topic is relevant, not the private one.

Regarding the use cases you mention here, however, I see the 4.7 Files API does not support them very well. I understand the API user must not be supposed to construct the workspace prefix manually (based on the per_workspace config and the workspace cookie) as this is (internally) performed by the FilesService already.

In order to proceed with the current 4.7 release you can use the following workaround (which is obviously not very nice). In your plugins define this helper method:

	String prefix() {
		File repo = filesService.getPath("/");
		return ((FilesPlugin) filesService).repoPath(repo)

This method returns the prefix to be used when constructing a repository path (as to be passed to a Files API call). In case of per-workspace file repos are activated the prefix represents the current workspace (e.g. "/workspace-1234", based on the workspace cookie). In case of per-workspace file repos are NOT activated an empty string is returned.

So, in a Files API call you use it like this:

	filesService.createFile(in, prefix() + "/images/img1.jpg")

And to check if a directory exists in the file repo do this:

	filesService.getFile(prefix() + "/images")

If the file/directory does not exist a FileRepositoryException with a 404 status code is thrown.

And to create a file topic manually for an existing file to this:

	filesService.getFileTopic(prefix() + "/images/img2.jpg")

This returns the File topic representing the file at the given repository path. If no such File topic exists it will be created (and returned).

To summarize: consequently use the prefix() method in your Files API calls if the "current workspace" semantics is wanted. Your plugin will work in both cases then: with or without per-workspace file repos.

I hope this helps.

In the next DM release the Files API will be improved.

Thank you for presenting your use cases!


On Nov 11, 2015, at 13:52, Malte Reissig wrote:

> Hi Jörg,
> i have created some Tickets, divided by "Action i wanted to use the
> FilesService for". I had problems implementing these actions with the
> new 4.7, esp. i wanted to avoid to handle/re-implement the per-workspace
> setting in my various plugins.
> 1) Use FilesService to create File in a subdirectory (of the Workspace
> current to the Requests)
> https://trac.deepamehta.de/ticket/879
> (#882 was a duplicate for that and i just closed that with reference)
> 2) Use FilesService to check if a directpory (in the Workspace current
> to the Requests) exists:
> https://trac.deepamehta.de/ticket/881
> https://trac.deepamehta.de/ticket/880
> For the upgrade of the dm4-export plugin
> (https://trac.deepamehta.de/ticket/871) i need to use (1) and for the
> upgrade of dm4-images (https://trac.deepamehta.de/ticket/821) (2). For
> the former it would be great if the filesservice provides some special
> support from either the FilesService through a) creating a file topic
> manually for an existing file or b) from you for getting to know how to
> adapt the XMLStreamWriter(new FileWriter()) impl to be compatible with
> the FilesService.store|createFile method.
> Thanks very much for your support & Cheers!
> -- 
> devel mailing list
> devel at lists.deepamehta.de
> http://lists.deepamehta.de/mailman/listinfo/devel-lists.deepamehta.de

More information about the devel mailing list