Setting since_tizen 3/4 on Tizen.NET API
[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 the MIME types to file extensions and vice versa.
27     /// </summary>
28     /// <remarks>
29     /// Conversions are provided from the file extensions to MIME types and from the MIME types to file extensions.
30     /// </remarks>
31     /// <since_tizen> 3 </since_tizen>
32     public static class MimeUtil
33     {
34         /// <summary>
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.
37         /// </summary>
38         /// <param name="fileExtension"> The file extension.</param>
39         /// <example>
40         /// <code>
41         ///     string mimeType = MimeUtil.GetMimeType("png");
42         /// </code>
43         /// </example>
44         /// <since_tizen> 3 </since_tizen>
45         public static string GetMimeType(string fileExtension)
46         {
47             string mime;
48             int res = Interop.Mime.GetMime(fileExtension, out mime);
49             if (res != (int)MimeError.None)
50             {
51                 throw MimeExceptionFactory.CreateException((MimeError)res);
52             }
53             return mime;
54         }
55
56         /// <summary>
57         /// Gets the file extensions for the given MIME type.
58         /// </summary>
59         /// <returns>
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 ('.').
62         /// </returns>
63         /// <param name="mime"> The MIME type.</param>
64         /// <example>
65         /// <code>
66         ///     var extColl = MimeUtil.GetFileExtension("video/mpeg");
67         ///     foreach ( string obj in extColl )
68         ///     {
69         ///         Console.WriteLine(obj);
70         ///     }
71         /// </code>
72         /// </example>
73         /// <since_tizen> 3 </since_tizen>
74         public static IEnumerable<string> GetFileExtension(string mime)
75         {
76             IntPtr extensionArray = IntPtr.Zero;
77             int length = -1;
78             int res = Interop.Mime.GetFile(mime, out extensionArray, out length);
79             if (res != (int)MimeError.None)
80             {
81                 throw MimeExceptionFactory.CreateException((MimeError)res);
82             }
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)
87             {
88                 coll.Add(Marshal.PtrToStringAnsi(extension));
89                 Interop.Libc.Free(extension);
90             }
91             Interop.Libc.Free(extensionArray);
92             return coll;
93         }
94
95         internal enum MimeError : int
96         {
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,
101         }
102     }
103 }