From 08f0d0cb7eb224f9b2b08e288e3b5d631ab6940e Mon Sep 17 00:00:00 2001 From: Ling Wang Date: Mon, 7 Dec 2020 20:50:37 -0600 Subject: [PATCH] Add file parameter to storeMediaFile (#208) --- actions/media.md | 26 ++++++++++++++++++++++---- plugin/__init__.py | 7 ++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/actions/media.md b/actions/media.md index 8c534bd..2874a60 100644 --- a/actions/media.md +++ b/actions/media.md @@ -2,10 +2,8 @@ * **storeMediaFile** - 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. + Stores a file with the specified base64-encoded contents inside the media folder. Alternatively you can specify a + absolute file path, or a url from where the file shell be downloaded. If more than one of `data`, `path` and `url` are provided, the `data` field will be used first, then `path`, and finally `url`. 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 @@ -32,6 +30,26 @@ Hello world! ``` + *Sample request*: + ```json + { + "action": "storeMediaFile", + "version": 6, + "params": { + "filename": "_hello.txt", + "file": "/path/to/file" + } + } + ``` + + *Sample result*: + ```json + { + "result": null, + "error": null + } + ``` + *Sample request*: ```json { diff --git a/plugin/__init__.py b/plugin/__init__.py index 5fa9030..8de44af 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -471,10 +471,15 @@ class AnkiConnect: @util.api() - def storeMediaFile(self, filename, data=None, url=None): + def storeMediaFile(self, filename, data=None, path=None, url=None): if data: self.deleteMediaFile(filename) self.media().writeData(filename, base64.b64decode(data)) + elif file: + self.deleteMediaFile(filename) + with open(path, 'rb') as f: + data = f.read() + self.media().writeData(filename, data) elif url: self.deleteMediaFile(filename) downloadedData = util.download(url)