Update sqlite, taglib, and wand for new 'magic:*' defuns.
authorSteve Youngs <steve@sxemacs.org>
Mon, 24 Feb 2020 20:50:08 +0000 (06:50 +1000)
committerSteve Youngs <steve@sxemacs.org>
Mon, 24 Feb 2020 20:50:08 +0000 (06:50 +1000)
This updates a few things to use the new and improved ffi-magic.

* lisp/ffi/ffi-sqlite.el (sqlite-file-p): Use #'magic:file to get
the job done.

* lisp/ffi/ffi-wand.el (Wand-file-supported-for-read-p):
Use #'magic:file-image-p to test file type.

* lisp/ffi/ffi-taglib.el (taglib:mimetypes): New, replaces
`taglib:extensions'
(taglib:extensions): Removed.
(taglib:get-tag): Use #'magic:file to test for supported file
formats.
(taglib:list-all-tags): Ditto.

Signed-off-by: Steve Youngs <steve@sxemacs.org>
lisp/ffi/ffi-sqlite.el
lisp/ffi/ffi-taglib.el
lisp/ffi/ffi-wand.el

index aaebf12..c648021 100644 (file)
@@ -251,11 +251,8 @@ ERROR is message to be signaled."
 ;;;###autoload
 (defun sqlite-file-p (filename)
   "Return non-nil if FILENAME is actually SQLite format 3 file."
-  ;; Unfortunately `magic:file-type' does not recognizes SQLite files,
-  ;; so do it by hand
-  (with-temp-buffer
-    (insert-file-contents-literally filename nil 0 15)
-    (string= (buffer-substring) "SQLite format 3")))
+  (string= (magic:file filename :mime-type)
+          "application/x-sqlite3"))
 
 ;;;###autoload
 (defun sqlite-open (file)
index 5d74f85..bed7bc6 100644 (file)
 
       (dllist-to-list result))))
 
-;;; FIXME: this isn't failsafe, use #'magic:file-type instead.
-(defvar taglib:extensions
-  '("mp3" "mpc" "ogg" "flac" "spx" "wv" "tta")
-  "List of file types that taglib supports.")
+
+(defvar taglib:mimetypes
+'("audio/aiff"
+  "audio/flac"
+  "audio/mp4"
+  "audio/mpeg"
+  "audio/ogg"
+  "audio/speex"
+  "audio/tta"
+  "audio/x-aiff"
+  "audio/x-m4a"
+  "audio/x-mp4a-latm"
+  "audio/x-musepack"
+  "audio/x-tta"
+  ;; Taglib supports some video types as well, who knew?!  ATM
+  ;; ffi-taglib.el is single-mindedly audio focused.  We should
+  ;; broaden its horizons. --SY.
+  ;; "video/mp4"
+  ;; "video/mpeg"
+  ;; "video/ogg"
+  ;; "video/x-ms-asf"
+)
+"List of MIME audio subtypes that taglib supports.")
 
 (defvar taglib:editable-tagnames
   '("album" "artist" "comment" "genre" "title" "track" "year")
@@ -461,11 +480,9 @@ otherwise just display it in the echo area."
                          (mapfam #'list taglib:tagnames) nil t)))
   (when (string= tag "")
     (error 'invalid-argument tag))
-  ;; better done with #'magic:file-type
-  (unless (member (file-name-extension (file-basename file))
-                 taglib:extensions)
-    (error "Unsupported file type: %s" (file-name-extension
-                                       (file-basename file))))
+  (let ((ftype (magic:file file :mime-type)))
+    (unless (member ftype taglib:mimetypes)
+      (error "Unsupported file type: %s" ftype)))
   (let* ((fo (taglib:file-new (expand-file-name file)))
         (to (taglib:file-tag fo))
         tfun res)
@@ -512,10 +529,15 @@ otherwise just display it in the echo area."
 
 ;;;###autoload
 (defun taglib:list-all-tags (file)
-  "Display a buffer showing all the tags of FILE."
+  "Display a buffer showing all the tags of FILE.
+
+This function is for interactive use only."
   (interactive "fFilename: ")
   (unless (interactive-p)
     (error 'invalid-operation "Interactive only function"))
+  (let ((ftype (magic:file file :mime-type)))
+    (unless (member ftype taglib:mimetypes)
+      (error "Unsupported file type: %s" ftype)))
   (let ((buf (get-buffer-create "*taglib:tags*"))
        (tags (taglib:properties file)))
     (with-current-buffer buf
index 8369f29..c309eb4 100644 (file)
@@ -2978,16 +2978,12 @@ Bindings are:
 ;;;###autoload
 (defun Wand-file-supported-for-read-p (file)
   "Return non-nil if Wand can decode FILE."
-  ;; Try by extension first, then try heuristic method using
-  ;; `magic:file-type'
-  (let ((ext (file-name-extension file)))
-    (or (and ext (Wand-format-supported-for-read-p ext))
-
-       (multiple-value-bind (itype imagetext)
-           (split-string (or (magic:file-type file) " ") " ")
-         (and imagetext
-              (string= (downcase imagetext) "image")
-              (Wand-format-supported-for-read-p itype))))))
+  ;; Use `magic:file-image-p' first, fallback to file extension check
+  ;; if that fails.
+  (let ((itype (magic:file-image-p file))
+       (ext (file-name-extension file)))
+    (or (and itype (Wand-format-supported-for-read-p itype))
+       (and ext (Wand-format-supported-for-read-p ext)))))
 
 (defun Wand-formats-list (fmt-regexp &optional mode)
   "Return names of supported formats that matches FMT-REGEXP.