Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MimeType / Tizen.Content.MimeType / MimeUtil.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
20 using System.Collections.Specialized;
21 using System.Runtime.InteropServices;
22
23 namespace Tizen.Content.MimeType
24 {
25     /// <summary>
26     /// The MimeUtil API provides functions to map MIME types to file extensions and vice versa.</summary>
27     /// <remarks>
28     /// Conversions are provided from file extensions to MIME types and from MIME types to file extensions.</remarks>
29     public static class MimeUtil
30     {
31         /// <summary>
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
34         /// </summary>
35         /// <param name="fileExtension"> The file Extension</param>
36         /// <example>
37         /// <code>
38         /// string mimeType = MimeUtil.GetMimeType("png");
39         /// </code>
40         /// </example>
41         public static string GetMimeType(string fileExtension)
42         {
43             string mime;
44             int res = Interop.Mime.GetMime(fileExtension, out mime);
45             if (res != (int)MimeError.None)
46             {
47                 throw MimeExceptionFactory.CreateException((MimeError)res);
48             }
49             return mime;
50         }
51
52         /// <summary>
53         /// Gets file extensions for the given MIME type. </summary>
54         /// <returns>
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>
58         /// <example>
59         /// <code>
60         /// IEnumerable<string> extColl = MimeUtil.GetFileExtension("video/mpeg");
61         /// foreach ( string obj in extColl )
62         /// {
63         ///     Console.WriteLine(obj);
64         /// }
65         /// </code>
66         /// </example>
67         public static IEnumerable<string> GetFileExtension(string mime)
68         {
69             IntPtr extensionArray = IntPtr.Zero;
70             int length = -1;
71             int res = Interop.Mime.GetFile(mime, out extensionArray, out length);
72             if (res != (int)MimeError.None)
73             {
74                 throw MimeExceptionFactory.CreateException((MimeError)res);
75             }
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)
80             {
81                 coll.Add(Marshal.PtrToStringAnsi(extension));
82                 Interop.Libc.Free(extension);
83             }
84             Interop.Libc.Free(extensionArray);
85             return coll;
86         }
87
88         internal enum MimeError : int
89         {
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 */
94         }
95     }
96 }