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 the MIME types to file extensions and vice versa.
29 /// Conversions are provided from the file extensions to MIME types and from the MIME types to file extensions.
31 /// <since_tizen> 3 </since_tizen>
32 public static class MimeUtil
35 /// Gets the MIME type for the given file extension.
36 /// The MIME type is 'application/octet-stream' if the given file extension is not associated with specific file formats.
38 /// <param name="fileExtension"> The file extension.</param>
41 /// string mimeType = MimeUtil.GetMimeType("png");
44 /// <since_tizen> 3 </since_tizen>
45 public static string GetMimeType(string fileExtension)
48 int res = Interop.Mime.GetMime(fileExtension, out mime);
49 if (res != (int)MimeError.None)
51 throw MimeExceptionFactory.CreateException((MimeError)res);
57 /// Gets the file extensions for the given MIME type.
60 /// If successful, returns the list of file extension strings for the given MIME type.
61 /// The array of file extension is without the leading dot ('.').
63 /// <param name="mime"> The MIME type.</param>
66 /// var extColl = MimeUtil.GetFileExtension("video/mpeg");
67 /// foreach ( string obj in extColl )
69 /// Console.WriteLine(obj);
73 /// <since_tizen> 3 </since_tizen>
74 public static IEnumerable<string> GetFileExtension(string mime)
76 IntPtr extensionArray = IntPtr.Zero;
78 int res = Interop.Mime.GetFile(mime, out extensionArray, out length);
79 if (res != (int)MimeError.None)
81 throw MimeExceptionFactory.CreateException((MimeError)res);
83 IntPtr[] extensionList = new IntPtr[length];
84 Marshal.Copy(extensionArray, extensionList, 0, length);
85 Collection<string> coll = new Collection<string>();
86 foreach (IntPtr extension in extensionList)
88 coll.Add(Marshal.PtrToStringAnsi(extension));
89 Interop.Libc.Free(extension);
91 Interop.Libc.Free(extensionArray);
95 internal enum MimeError : int
97 None = Tizen.Internals.Errors.ErrorCode.None,
98 InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter,
99 OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory,
100 IoError = Tizen.Internals.Errors.ErrorCode.IoError,