From 591171016336948894b2234fa795ddd64b6316e7 Mon Sep 17 00:00:00 2001 From: Yannick Mau Date: Sat, 14 Mar 2020 00:36:17 +0100 Subject: [PATCH] Add url download option to storeMediaFile --- README.md | 27 ++++++++++++++++++++++++--- plugin/__init__.py | 13 +++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index bee71ef..b69073c 100644 --- a/README.md +++ b/README.md @@ -1494,9 +1494,10 @@ guarantee that your application continues to function properly in the future. * **storeMediaFile** - Stores a file with the specified base64-encoded contents inside the media folder. To prevent Anki from removing - files not used by any cards (e.g. for configuration files), prefix the filename with an underscore. These files are - still synchronized to AnkiWeb. + Stores a file with the specified base64-encoded contents inside the media folder. alternatively you can specify a + url from where the file shell be downloaded. If both field `data` and `url` are provided, the `data` field will be + used. To prevent Anki from removing files not used by any cards (e.g. for configuration files), prefix the filename + with an underscore. These files are still synchronized to AnkiWeb. *Sample request*: ```json @@ -1522,6 +1523,26 @@ guarantee that your application continues to function properly in the future. ``` Hello world! ``` + + *Sample request*: + ```json + { + "action": "storeMediaFile", + "version": 6, + "params": { + "filename": "_hello.txt", + "url": "https://url.to.file" + } + } + ``` + + *Sample result*: + ```json + { + "result": null, + "error": null + } + ``` * **retrieveMediaFile** diff --git a/plugin/__init__.py b/plugin/__init__.py index d6bc9f8..fb51c29 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -421,10 +421,15 @@ class AnkiConnect: @util.api() - def storeMediaFile(self, filename, data): - self.deleteMediaFile(filename) - self.media().writeData(filename, base64.b64decode(data)) - + def storeMediaFile(self, filename, data=None, url=None): + if data: + self.deleteMediaFile(filename) + self.media().writeData(filename, base64.b64decode(data)) + elif url: + downloadedData = util.download(url) + self.media().writeData(filename, downloadedData) + else: + raise Exception('You must either provide a "data" or a "url" field.') @util.api() def retrieveMediaFile(self, filename):