[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Using mimetype and file to guess content-type
From: |
Jean Louis |
Subject: |
Using mimetype and file to guess content-type |
Date: |
Mon, 07 Jun 2021 23:25:53 +0300 |
In order to guess the content-type, I am using these functions. It
appears that Perl's `mimetype' works so much better than `file', for
example it will give tell for .gpg file that it is pgp encrypted, but
`file' will tell it is application octet stream.
Overall I am replacing some `mutt' based functions with `mail' and
that works well so far. But `mutt' on the command line in batch mode
does recognize the attached file's content type while `mail' does not.
I understand that `mail' is asking for the content-type to be provided
by user, though question is if some mechanism exists in C libraries
that the content-type gets automatically recognized if not supplied on
the command line?
That would or could liberate the `mail' from external dependencies
such as `file' or `mimetype' where latter works better.
(defun rcd-mime-type (file &optional prefer-mime)
"Returns mime type of the file"
(if (rcd-which-list '("file" "mimetype"))
(let ((file-command (executable-find "file"))
(mimetype (executable-find "mimetype")))
;; TODO:
;; There is much work to do here to work on various systems for
;; example there may not be same output on BSD and GNU systems.
;; Additional problem is file quoting.
;; file and mimetype may give different results
(if (and (not prefer-mime) file-command)
(string-trim
(call-process-to-string file-command nil nil "-b" "--mime-type"
file))
(if mimetype
(string-trim
(call-process-to-string mimetype nil nil "-b" file)))))
(rcd-warning-message "RCD ERROR: `file' or/and `mimetype' not found in
$PATH")))
(defun rcd-mailutils-mail-files (files)
"Return list with \"mail\" program arguments for file attachments."
(cond ((stringp files) (list "--content-type" (rcd-mime-type files t) "-A"
files))
((listp files) (let (list)
(when files
(let ((file (pop files)))
(push file list)
(push "-A" list)
(push (rcd-mime-type files t) list)
(push "--content-type" list)))
list))))
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/
- Using mimetype and file to guess content-type,
Jean Louis <=