storeMediaFile: Add option to keep file with the same name (#257)

This commit is contained in:
abdo 2021-05-22 22:21:12 +03:00 committed by GitHub
parent 9fec86f7fe
commit f8ff7c0c38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 8 deletions

View File

@ -4,6 +4,8 @@
Stores a file with the specified base64-encoded contents inside the media folder. Alternatively you can specify a 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. 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.
Any existing file with the same name is deleted by default. Set `deleteExisting` to false to prevent that
by [letting Anki give the new file a non-conflicting name](https://github.com/ankitects/anki/blob/aeba725d3ea9628c73300648f748140db3fdd5ed/rslib/src/media/files.rs#L194).
*Sample request*: *Sample request*:
```json ```json

View File

@ -561,19 +561,18 @@ class AnkiConnect:
@util.api() @util.api()
def storeMediaFile(self, filename, data=None, path=None, url=None, skipHash=None): def storeMediaFile(self, filename, data=None, path=None, url=None, skipHash=None, deleteExisting=True):
if data: if not (data or path or url):
raise Exception('You must provide a "data", "path", or "url" field.')
if deleteExisting:
self.deleteMediaFile(filename) self.deleteMediaFile(filename)
if data:
mediaData = base64.b64decode(data) mediaData = base64.b64decode(data)
elif path: elif path:
self.deleteMediaFile(filename)
with open(path, 'rb') as f: with open(path, 'rb') as f:
mediaData = f.read() mediaData = f.read()
elif url: elif url:
self.deleteMediaFile(filename)
mediaData = util.download(url) mediaData = util.download(url)
else:
raise Exception('You must either provide a "data" or a "url" field.')
if skipHash is None: if skipHash is None:
skip = False skip = False

View File

@ -10,14 +10,16 @@ class TestMedia(unittest.TestCase):
data = 'test' data = 'test'
# storeMediaFile # storeMediaFile
util.invoke('storeMediaFile', filename='_test.txt', data=data) util.invoke('storeMediaFile', filename=filename, data=data)
filename2 = util.invoke('storeMediaFile', filename=filename, data='testtest', deleteExisting=False)
self.assertNotEqual(filename2, filename)
# retrieveMediaFile (part 1) # retrieveMediaFile (part 1)
media = util.invoke('retrieveMediaFile', filename=filename) media = util.invoke('retrieveMediaFile', filename=filename)
self.assertEqual(media, data) self.assertEqual(media, data)
names = util.invoke('getMediaFilesNames', pattern='_tes*.txt') names = util.invoke('getMediaFilesNames', pattern='_tes*.txt')
self.assertEqual(names, [filename]) self.assertEqual(set(names), set([filename, filename2]))
# deleteMediaFile # deleteMediaFile
util.invoke('deleteMediaFile', filename=filename) util.invoke('deleteMediaFile', filename=filename)