[MediaContent] Add exception and change p/invoke function (#782)
authorhsgwon <haesu.gwon@samsung.com>
Mon, 15 Apr 2019 06:32:40 +0000 (15:32 +0900)
committerGitHub <noreply@github.com>
Mon, 15 Apr 2019 06:32:40 +0000 (15:32 +0900)
* [MediaController] Add exception and change p/invoke function

src/Tizen.Content.MediaContent/Tizen.Content.MediaContent/MediaInfoCommand.cs

index 3a820bf..b474646 100644 (file)
@@ -476,8 +476,16 @@ namespace Tizen.Content.MediaContent
         /// <privilege>http://tizen.org/privilege/content.write</privilege>
         /// <param name="mediaId">The media ID to delete.</param>
         /// <returns>true if the matched record was found and deleted, otherwise false.</returns>
-        /// <remarks>The <see cref="MediaDatabase.ScanFile(string)"/> or the <see cref="MediaDatabase.ScanFolderAsync(string)"/> can be used instead.</remarks>
-        /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
+        /// <remarks>
+        /// The <see cref="MediaDatabase.ScanFile(string)"/> or the <see cref="MediaDatabase.ScanFolderAsync(string)"/> can be used instead.<br/>
+        /// Since API level 6, If file still exists in file system before calling this method,
+        /// <see cref="InvalidOperationException"/> will be thrown to keep db consistency.
+        /// </remarks>
+        /// <exception cref="InvalidOperationException">
+        ///     The <see cref="MediaDatabase"/> is disconnected.<br/>
+        ///     -or-<br/>
+        ///     File still exists in file system. (Since API level 6)
+        /// </exception>
         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
         /// <exception cref="ArgumentNullException"><paramref name="mediaId"/> is null.</exception>
@@ -496,7 +504,21 @@ namespace Tizen.Content.MediaContent
                 return false;
             }
 
-            CommandHelper.Delete(Interop.MediaInfo.Delete, mediaId);
+            Interop.MediaInfo.GetMediaFromDB(mediaId, out var handle).
+                ThrowIfError("Failed to delete MediaInfo.");
+
+            var path = InteropHelper.GetString(handle, Interop.MediaInfo.GetFilePath);
+
+            // If we don't check file existence before calling `ScanFile` method,
+            // The inconsistency between DB and file system could be occurred.
+            if (File.Exists(path))
+            {
+                throw new InvalidOperationException("File still exists in file system. Remove it first.");
+            }
+
+            // Native 'delete' function was deprecated, so we need to use 'scan file' function instead of it.
+            Database.ScanFile(path);
+
             return true;
         }