Override SingleFileDownloadHandler

Hi all,

I’m trying to replace SingleFileDownloadHandler from here with my own implementaton:
packages/filesystem/src/node/download/file-download-handler.ts

in my extension’s package.json, i added these:

{
      "frontend": "lib/browser/download/file-download-frontend-module",
      "backend": "lib/node/download/file-download-backend-module"
}

Added a file under my-extension/src/node/download/file-download-backend-module.ts with the contents:

import { ContainerModule } from 'inversify';
import { FileDownloadHandler, SingleFileDownloadHandler } from '@theia/filesystem/lib/node/download/file-download-handler';
import { MyTestDownloadHandler } from './my-test-download-handler';

export default new ContainerModule((bind, unbind, isBound, rebind) => {
    // My Custom Download handler
    unbind(SingleFileDownloadHandler);
    bind(SingleFileDownloadHandler).to(MyTestDownloadHandler).inSingletonScope().whenTargetNamed(FileDownloadHandler.SINGLE);
});

Build succeeded.
But, when I tried to run, I get the following error and the app terminates:

Failed to start the backend application.
Error: Could not unbind serviceIdentifier: SingleFileDownloadHandler

Also, if I remove the line unbind(SingleFileDownloadHandler); then I get the following error when I run the app:

$ theia start --plugins=local-dir:../plugins/
Error: Ambiguous match found for serviceIdentifier: FileDownloadHandler

Can anyone help me how I can replace or override SingleFileDownloadHandler ?
Thank you very much in advance.

That won’t work without modifying the Theia code. Why do you want to replace and customize the default handler? Maybe it is an XY Problem; are you still working on this?

Hi @kittaakos,

I’m trying to override SingleFileDownloadHandler in order to support downloading folders as zip files.
(Currently, I believe that theia only supports downloading as .tar file only).

The filenames computed in SingleFileDownloadHandler uses hardcoded “.tar”. So, i think i need to override the function async handle(request: Request, response: Response): Promise<void>

Could you please propose another way if any ?

Here is the example. Check the console after downloading a folder. I downloaded the scripts folder from Theia. It still downloads as tar, but from the console output, you can see, you have full control over the implementation. It’s up to you what you want to use:

root INFO Use whatever lib to ZIP /Users/akos.kitta/git/theia/scripts into /var/folders/k3/d2fkvv1j16v3_rz93k7f74180000gn/T/b9ecc142-1095-4e42-a4a6-1af312e74f78/scripts.tar with the following entries:

I hope this helps.

Thank you, @kittaakos.

As I’m using very old version of theia (0.8.0), I would like to compress a folder as zip without modifying the file-system code in theia. Thats why I’m trying to override/replace the original implementations.

you can see, you have full control over the implementation. It’s up to you what you want to use
root INFO Use whatever lib to ZIP /Users/akos.kitta/git/theia/scripts into /var/folders/k3/d2fkvv1j16v3_rz93k7f74180000gn/T/b9ecc142-1095-4e42-a4a6-1af312e74f78/scripts.tar with the following entries:

I apologize, I couldnt understand (sorry, not an expert in this area). Could you provide more information on how I can achieve this?
Thank you very much for timely help.