2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
20 using System.Collections.Specialized;
21 using System.Runtime.InteropServices;
23 namespace Tizen.Content.MimeType
26 /// The MimeUtil API provides functions to map MIME types to file extensions and vice versa.</summary>
28 /// Conversions are provided from file extensions to MIME types and from MIME types to file extensions.</remarks>
29 public static class MimeUtil
32 /// Gets the MIME type for the given file extension.
33 /// The MIME type is 'application/octet-stream' if the given file extension is not associated with specific file formats
35 /// <param name="fileExtension"> The file Extension</param>
38 /// string mimeType = MimeUtil.GetMimeType("png");
41 public static string GetMimeType(string fileExtension)
44 int res = Interop.Mime.GetMime(fileExtension, out mime);
45 if (res != (int)MimeError.None)
47 throw MimeExceptionFactory.CreateException((MimeError)res);
53 /// Gets file extensions for the given MIME type. </summary>
55 /// If Successfull, return's the list of file extension strings for the given MIME type.
56 /// The array of file extension are without the leading dot ('.')</returns>
57 /// <param name="mime"> The mime type</param>
60 /// IEnumerable<string> extColl = MimeUtil.GetFileExtension("video/mpeg");
61 /// foreach ( string obj in extColl )
63 /// Console.WriteLine(obj);
67 public static IEnumerable<string> GetFileExtension(string mime)
69 IntPtr extensionArray = IntPtr.Zero;
71 int res = Interop.Mime.GetFile(mime, out extensionArray, out length);
72 if (res != (int)MimeError.None)
74 throw MimeExceptionFactory.CreateException((MimeError)res);
76 IntPtr[] extensionList = new IntPtr[length];
77 Marshal.Copy(extensionArray, extensionList, 0, length);
78 Collection<string> coll = new Collection<string>();
79 foreach (IntPtr extension in extensionList)
81 coll.Add(Marshal.PtrToStringAnsi(extension));
82 Interop.Libc.Free(extension);
84 Interop.Libc.Free(extensionArray);
88 internal enum MimeError : int
90 None = Tizen.Internals.Errors.ErrorCode.None, /**< Successful */
91 InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter, /**< Invalid parameter */
92 OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory, /**< Out of memory */
93 IoError = Tizen.Internals.Errors.ErrorCode.IoError, /**< Internal I/O error */