From 22a58bb7073d0b0916b7b809de22880c91d5237a Mon Sep 17 00:00:00 2001 From: Conor O'Kelly Date: Tue, 7 Apr 2020 21:18:48 -0700 Subject: [PATCH] Use new hook for delete In reference to issue #146 Also added the `deleteMediaFile` when dealing with urls. The new functionality of `writeData` means that is there is a name conflict it will rename the file. This way the behavior of your api does not change. Only potential issues I see with this is that the file is `trashed` but not deleted. So is only cleared up on the usual clear up. Test locally using my own Anki install at `2.1.22` and POST man to create new media --- plugin/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugin/__init__.py b/plugin/__init__.py index 77dd3cd..f5a96d7 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -429,6 +429,7 @@ class AnkiConnect: self.deleteMediaFile(filename) self.media().writeData(filename, base64.b64decode(data)) elif url: + self.deleteMediaFile(filename) downloadedData = util.download(url) self.media().writeData(filename, downloadedData) else: @@ -450,7 +451,10 @@ class AnkiConnect: @util.api() def deleteMediaFile(self, filename): - self.media().syncDelete(filename) + try: + self.media().syncDelete(filename) + except AttributeError: + self.media().trash_files([filename]) @util.api()