c2pa-rs

A fork of https://github.com/contentauth/c2pa-rs/
git clone git://archive.git.mtrnord.blog/mtrnords-photography-manager/c2pa-rs.git
Log | Files | Refs | README

mime.rs (4061B)


      1 // Copyright 2022 Adobe. All rights reserved.
      2 // This file is licensed to you under the Apache License,
      3 // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
      4 // or the MIT license (http://opensource.org/licenses/MIT),
      5 // at your option.
      6 
      7 // Unless required by applicable law or agreed to in writing,
      8 // this software is distributed on an "AS IS" BASIS, WITHOUT
      9 // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
     10 // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
     11 // specific language governing permissions and limitations under
     12 // each license.
     13 
     14 /// Converts a file extension to a MIME type
     15 pub fn extension_to_mime(extension: &str) -> Option<&'static str> {
     16     Some(match extension {
     17         "jpg" | "jpeg" => "image/jpeg",
     18         "png" => "image/png",
     19         "gif" => "image/gif",
     20         "psd" => "image/vnd.adobe.photoshop",
     21         "tiff" | "tif" => "image/tiff",
     22         "svg" => "image/svg+xml",
     23         "ico" => "image/x-icon",
     24         "bmp" => "image/bmp",
     25         "webp" => "image/webp",
     26         "dng" => "image/x-adobe-dng",
     27         "heic" => "image/heic",
     28         "heif" => "image/heif",
     29         "mp2" | "mpa" | "mpe" | "mpeg" | "mpg" | "mpv2" => "video/mpeg",
     30         "mp4" => "video/mp4",
     31         "avi" => "video/avi",
     32         "avif" => "image/avif",
     33         "mov" | "qt" => "video/quicktime",
     34         "m4a" => "audio/mp4",
     35         "mid" | "rmi" => "audio/mid",
     36         "mp3" => "audio/mpeg",
     37         "wav" => "audio/wav",
     38         "aif" | "aifc" | "aiff" => "audio/aiff",
     39         "ogg" => "audio/ogg",
     40         "pdf" => "application/pdf",
     41         "ai" => "application/postscript",
     42         "arw" => "image/x-sony-arw",
     43         "nef" => "image/x-nikon-nef",
     44         "c2pa" | "application/x-c2pa-manifest-store" | "application/c2pa" => "application/c2pa",
     45         "cr2" => "image/x-canon-cr2",
     46         "cr3" => "image/x-canon-cr3",
     47         _ => return None,
     48     })
     49 }
     50 
     51 /// Convert a format to a MIME type
     52 /// formats can be passed in as extensions, e.g. "jpg" or "jpeg"
     53 /// or as MIME types, e.g. "image/jpeg"
     54 pub fn format_to_mime(format: &str) -> String {
     55     match extension_to_mime(format) {
     56         Some(mime) => mime,
     57         None => format,
     58     }
     59     .to_string()
     60 }
     61 
     62 /// Converts a format to a file extension
     63 #[cfg(feature = "file_io")]
     64 pub fn format_to_extension(format: &str) -> Option<&'static str> {
     65     Some(match format {
     66         "jpg" | "jpeg" | "image/jpeg" => "jpg",
     67         "png" | "image/png" => "png",
     68         "gif" | "image/gif" => "gif",
     69         "psd" | "image/vnd.adobe.photoshop" => "psd",
     70         "tiff" | "tif" | "image/tiff" => "tiff",
     71         "svg" | "image/svg+xml" => "svg",
     72         "ico" | "image/x-icon" => "ico",
     73         "bmp" | "image/bmp" => "bmp",
     74         "webp" | "image/webp" => "webp",
     75         "dng" | "image/dng" => "dng",
     76         "heic" | "image/heic" => "heic",
     77         "heif" | "image/heif" => "heif",
     78         "mp2" | "mpa" | "mpe" | "mpeg" | "mpg" | "mpv2" | "video/mpeg" => "mp2",
     79         "mp4" | "video/mp4" => "mp4",
     80         "avif" | "image/avif" => "avif",
     81         "avi" | "video/avi" => "avi",
     82         "mov" | "qt" | "video/quicktime" => "mov",
     83         "m4a" | "audio/mp4" => "m4a",
     84         "mid" | "rmi" | "audio/mid" => "mid",
     85         "mp3" | "audio/mpeg" => "mp3",
     86         "wav" | "audio/wav" | "audio/wave" | "audio.vnd.wave" => "wav",
     87         "aif" | "aifc" | "aiff" | "audio/aiff" => "aif",
     88         "ogg" | "audio/ogg" => "ogg",
     89         "pdf" | "application/pdf" => "pdf",
     90         "ai" | "application/postscript" => "ai",
     91         "arw" | "image/x-sony-arw" => "arw",
     92         "nef" | "image/x-nikon-nef" => "nef",
     93         "c2pa" | "application/x-c2pa-manifest-store" | "application/c2pa" => "c2pa",
     94         _ => return None,
     95     })
     96 }
     97 
     98 /// Return a MIME type given a file path.
     99 ///
    100 /// This function will use the file extension to determine the MIME type.
    101 pub fn format_from_path<P: AsRef<std::path::Path>>(path: P) -> Option<String> {
    102     path.as_ref()
    103         .extension()
    104         .map(|ext| crate::utils::mime::format_to_mime(ext.to_string_lossy().as_ref()))
    105 }