Add file parameter to storeMediaFile (#208)

This commit is contained in:
Ling Wang 2020-12-07 20:50:37 -06:00 committed by GitHub
parent 06dff21107
commit 08f0d0cb7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 5 deletions

View File

@ -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
{

View File

@ -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)