Use emacs base64 sha1 (#38)

Use built-in functions for SHA1 hasing and base64 encoding
This commit is contained in:
Kevin Foley 2018-12-31 02:53:14 -05:00 committed by Lei Tan
parent 7af16c6447
commit 115ce2e2e6

View file

@ -194,13 +194,7 @@ See https://apps.ankiweb.net/docs/manual.html#latex-conflicts.")
(defun anki-editor--anki-connect-store-media-file (path) (defun anki-editor--anki-connect-store-media-file (path)
"Store media file for PATH, which is an absolute file name. "Store media file for PATH, which is an absolute file name.
The result is the path to the newly stored media file." The result is the path to the newly stored media file."
(unless (-all? #'executable-find '("base64" "sha1sum")) (let* ((hash (secure-hash 'sha1 path))
(error "Please make sure `base64' and `sha1sum' are available from your shell, which are required for storing media files"))
(let* ((hash (string-trim
(shell-command-to-string
(format "sha1sum %s | awk '{print $1}'"
(shell-quote-argument path)))))
(media-file-name (format "%s-%s%s" (media-file-name (format "%s-%s%s"
(file-name-base path) (file-name-base path)
hash hash
@ -210,10 +204,10 @@ The result is the path to the newly stored media file."
"retrieveMediaFile" "retrieveMediaFile"
`((filename . ,media-file-name)))) `((filename . ,media-file-name))))
(message "Storing media file to Anki for %s..." path) (message "Storing media file to Anki for %s..." path)
(setq content (string-trim (setq content (base64-encode-string
(shell-command-to-string (with-temp-buffer
(format "base64 --wrap=0 %s" (insert-file-contents path)
(shell-quote-argument path))))) (buffer-string))))
(anki-editor--anki-connect-invoke-result (anki-editor--anki-connect-invoke-result
"storeMediaFile" "storeMediaFile"
`((filename . ,media-file-name) `((filename . ,media-file-name)