diff --git a/actions/media.md b/actions/media.md index 6262126..2412290 100644 --- a/actions/media.md +++ b/actions/media.md @@ -93,6 +93,29 @@ } ``` +* **getMediaFilesNames** + + Gets the names of media files matched the pattern. Returning all names by default. + + *Sample request*: + ```json + { + "action": "getMediaFilesNames", + "version": 6, + "params": { + "pattern": "_hell*.txt" + } + } + ``` + + *Sample result*: + ```json + { + "result": ["_hello.txt"], + "error": null + } + ``` + * **deleteMediaFile** Deletes the specified file inside the media folder. diff --git a/plugin/__init__.py b/plugin/__init__.py index 49a667d..fabf5d8 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -14,6 +14,7 @@ # along with this program. If not, see . import base64 +import glob import hashlib import inspect import json @@ -549,6 +550,12 @@ class AnkiConnect: return False + @util.api() + def getMediaFilesNames(self, pattern='*'): + path = os.path.join(self.media().dir(), pattern) + return [os.path.basename(p) for p in glob.glob(path)] + + @util.api() def deleteMediaFile(self, filename): try: diff --git a/tests/test_media.py b/tests/test_media.py index 7870fbc..1de0ea5 100755 --- a/tests/test_media.py +++ b/tests/test_media.py @@ -16,6 +16,9 @@ class TestMedia(unittest.TestCase): media = util.invoke('retrieveMediaFile', filename=filename) self.assertEqual(media, data) + names = util.invoke('getMediaFilesNames', pattern='_tes*.txt') + self.assertEqual(names, [filename]) + # deleteMediaFile util.invoke('deleteMediaFile', filename=filename)