Modify DB write operation
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <string.h>
23 #include <errno.h>
24 #include "media-svc.h"
25 #include "media-svc-media.h"
26 #include "media-svc-debug.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
29 #include "media-svc-env.h"
30 #include "media-svc-media-folder.h"
31 #include "media-svc-album.h"
32 #include "media-svc-noti.h"
33 #include "media-svc-storage.h"
34
35 static __thread int g_media_svc_item_validity_data_cnt = 1;
36 static __thread int g_media_svc_item_validity_cur_data_cnt = 0;
37
38 static __thread int g_media_svc_insert_item_data_cnt = 1;
39 static __thread int g_media_svc_insert_item_cur_data_cnt = 0;
40
41 static __thread int g_media_svc_update_item_data_cnt = 1;
42 static __thread int g_media_svc_update_item_cur_data_cnt = 0;
43
44 static __thread int g_media_svc_insert_folder_data_cnt = 1;
45 static __thread int g_media_svc_insert_folder_cur_data_cnt = 0;
46
47 /* Flag for items to be published by notification */
48 static __thread int g_insert_with_noti = FALSE;
49
50 #define BATCH_REQUEST_MAX 300
51
52 int media_svc_get_user_version(sqlite3 *handle, int *user_version)
53 {
54         return _media_svc_get_user_version(handle, user_version);
55 }
56
57 int media_svc_create_table(uid_t uid)
58 {
59         int ret = MS_MEDIA_ERR_NONE;
60         char *sql = NULL;
61
62         media_svc_debug_fenter();
63
64         ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
65         if (ret != MS_MEDIA_ERR_NONE) {
66                 media_svc_error("_media_svc_init_table_query fail.");
67                 goto ERROR;
68         }
69
70         /*create media table*/
71         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
72         if (ret != MS_MEDIA_ERR_NONE) {
73                 media_svc_error("_media_svc_make_table_query fail.");
74                 goto ERROR;
75         }
76
77         /*create folder table*/
78         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
79         if (ret != MS_MEDIA_ERR_NONE) {
80                 media_svc_error("_media_svc_make_table_query fail.");
81                 goto ERROR;
82         }
83
84         /*create playlist_map table*/
85         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
86         if (ret != MS_MEDIA_ERR_NONE) {
87                 media_svc_error("_media_svc_make_table_query fail.");
88                 goto ERROR;
89         }
90
91         /*create playlist table*/
92         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
93         if (ret != MS_MEDIA_ERR_NONE) {
94                 media_svc_error("_media_svc_make_table_query fail.");
95                 goto ERROR;
96         }
97
98         /* create album table*/
99         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
100         if (ret != MS_MEDIA_ERR_NONE) {
101                 media_svc_error("_media_svc_make_table_query fail.");
102                 goto ERROR;
103         }
104
105         /*create tag_map table*/
106         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
107         if (ret != MS_MEDIA_ERR_NONE) {
108                 media_svc_error("_media_svc_make_table_query fail.");
109                 goto ERROR;
110         }
111
112         /*create tag table*/
113         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
114         if (ret != MS_MEDIA_ERR_NONE) {
115                 media_svc_error("_media_svc_make_table_query fail.");
116                 goto ERROR;
117         }
118
119         /*create bookmark table*/
120         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
121         if (ret != MS_MEDIA_ERR_NONE) {
122                 media_svc_error("_media_svc_make_table_query fail.");
123                 goto ERROR;
124         }
125
126         /*create storage table from tizen 2.4 */
127         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
128         if (ret != MS_MEDIA_ERR_NONE) {
129                 media_svc_error("_media_svc_make_table_query fail.");
130                 goto ERROR;
131         }
132
133         /*create face table. from tizen 3.0*/
134         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
135         if (ret != MS_MEDIA_ERR_NONE) {
136                 media_svc_error("_media_svc_make_table_query fail.");
137                 goto ERROR;
138         }
139
140         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
141         if (ret != MS_MEDIA_ERR_NONE) {
142                 media_svc_error("_media_svc_make_table_query fail.");
143                 goto ERROR;
144         }
145
146         sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
147         ret = _media_svc_sql_query(sql, uid);
148         if (ret != MS_MEDIA_ERR_NONE) {
149                 media_svc_error("user_version update fail.");
150                 goto ERROR;
151         }
152
153         _media_svc_destroy_table_query();
154
155         media_svc_debug_fleave();
156
157         return MS_MEDIA_ERR_NONE;
158 ERROR:
159         _media_svc_destroy_table_query();
160
161         media_svc_debug_fleave();
162
163         return ret;
164 }
165
166 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
167 {
168         int ret = MS_MEDIA_ERR_NONE;
169         int count = -1;
170
171         ret = _media_svc_count_record_with_path(handle, storage_id, path, &count);
172         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
173
174         if (count > 0) {
175                 media_svc_debug("item is exist in database");
176                 return MS_MEDIA_ERR_NONE;
177         } else {
178                 media_svc_debug("item is not exist in database");
179                 return MS_MEDIA_ERR_DB_NO_RECORD;
180         }
181
182         return MS_MEDIA_ERR_NONE;
183 }
184
185 int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
186 {
187         return _media_svc_get_modified_time(handle, storage_id, path, modified_time);
188 }
189
190 int media_svc_insert_item_begin(int data_cnt, int with_noti, int from_pid)
191 {
192         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
193
194         g_media_svc_insert_item_data_cnt = data_cnt;
195         g_media_svc_insert_item_cur_data_cnt = 0;
196
197         /* Prepare for making noti item list */
198         if (with_noti) {
199                 media_svc_debug("making noti list from pid[%d]", from_pid);
200                 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
201                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
202
203                 _media_svc_set_noti_from_pid(from_pid);
204                 g_insert_with_noti = TRUE;
205         }
206
207         return MS_MEDIA_ERR_NONE;
208 }
209
210 int media_svc_insert_item_end(uid_t uid)
211 {
212         int ret = MS_MEDIA_ERR_NONE;
213
214         media_svc_debug_fenter();
215
216         if (g_media_svc_insert_item_cur_data_cnt > 0) {
217
218                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
219                 if (g_insert_with_noti) {
220                         media_svc_debug("sending noti list");
221                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
222                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
223                         g_insert_with_noti = FALSE;
224                         _media_svc_set_noti_from_pid(-1);
225                 }
226         }
227
228         g_media_svc_insert_item_data_cnt = 1;
229         g_media_svc_insert_item_cur_data_cnt = 0;
230
231         return ret;
232 }
233
234 int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
235 {
236         int ret = MS_MEDIA_ERR_NONE;
237         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
238         media_svc_media_type_e media_type;
239
240         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
241         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
242         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
243         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
244
245         media_svc_content_info_s content_info;
246         memset(&content_info, 0, sizeof(media_svc_content_info_s));
247
248         /*Set media info*/
249         /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
250         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
251         if (ret != MS_MEDIA_ERR_NONE)
252                 return ret;
253
254         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
255         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
256         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
257         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
258                 media_svc_debug("Do nothing[%d]", media_type);
259         else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
260                 ret = _media_svc_extract_image_metadata(&content_info);
261         else
262                 ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
263         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
264
265         /*Set or Get folder id*/
266         ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, folder_uuid, uid);
267         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
268
269         content_info.folder_uuid = g_strdup(folder_uuid);
270         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
271
272         if (g_media_svc_insert_item_data_cnt == 1) {
273
274                 ret = _media_svc_insert_item_with_data(handle, true, storage_id, &content_info, FALSE, uid);
275                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
276
277                 if (g_insert_with_noti)
278                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
279
280         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
281
282                 ret = _media_svc_insert_item_with_data(handle, true, storage_id, &content_info, TRUE, uid);
283                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
284
285                 if (g_insert_with_noti)
286                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
287
288                 g_media_svc_insert_item_cur_data_cnt++;
289
290         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
291
292                 ret = _media_svc_insert_item_with_data(handle, true, storage_id, &content_info, TRUE, uid);
293                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
294
295                 if (g_insert_with_noti)
296                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
297
298                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
299                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
300
301                 if (g_insert_with_noti) {
302                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
303                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
304
305                         /* Recreate noti list */
306                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
307                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
308                 }
309
310                 g_media_svc_insert_item_cur_data_cnt = 0;
311
312         } else {
313                 media_svc_error("Error in media_svc_insert_item_bulk");
314                 _media_svc_destroy_content_info(&content_info);
315                 return MS_MEDIA_ERR_INTERNAL;
316         }
317
318         _media_svc_destroy_content_info(&content_info);
319
320         return MS_MEDIA_ERR_NONE;
321 }
322
323 int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
324 {
325         int ret = MS_MEDIA_ERR_NONE;
326         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
327         media_svc_media_type_e media_type;
328
329         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
330         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
331         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
332         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
333
334         media_svc_content_info_s content_info;
335         memset(&content_info, 0, sizeof(media_svc_content_info_s));
336
337         /*Set media info*/
338         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
339         if (ret != MS_MEDIA_ERR_NONE)
340                 return ret;
341
342         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
343         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
344         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
345         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
346                 /*Do nothing.*/
347         } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
348                 ret = _media_svc_extract_image_metadata(&content_info);
349         } else {
350                 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
351         }
352
353         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
354
355         /*Set or Get folder id*/
356         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, folder_uuid, uid);
357         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
358
359         content_info.folder_uuid = g_strdup(folder_uuid);
360         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
361
362         /* Extracting thumbnail */
363         if (content_info.thumbnail_path == NULL) {
364                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
365                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
366
367                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, media_type, uid);
368                         if (ret == MS_MEDIA_ERR_NONE)
369                                 content_info.thumbnail_path = g_strdup(thumb_path);
370                 }
371         }
372
373         ret = _media_svc_insert_item_with_data(handle, false, storage_id, &content_info, FALSE, uid);
374
375         if (ret == MS_MEDIA_ERR_NONE) {
376                 media_svc_debug("Insertion is successful. Sending noti for this");
377                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
378         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
379                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
380         }
381
382         _media_svc_destroy_content_info(&content_info);
383         return ret;
384 }
385
386 int media_svc_move_item(sqlite3 *handle, const char *src_path, const char *dest_path, uid_t uid)
387 {
388         int ret = MS_MEDIA_ERR_NONE;
389         char *file_name = NULL;
390         char *folder_path = NULL;
391         int modified_time = 0;
392         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
393         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
394         char org_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
395         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
396         ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
397         ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
398         int media_type = -1;
399
400         media_svc_debug_fenter();
401
402         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
403         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
404         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
405
406         /* Get storage_id */
407         ret = _media_svc_get_storage_uuid(handle, src_path, org_stg_id, uid);
408         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
409         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
410         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
411         /* Get storage_type */
412         ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
413         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
414         ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
415         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
416
417         /*check and update folder*/
418         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
419         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
420
421         /*get filename*/
422         file_name = g_path_get_basename(dest_path);
423
424         /*get modified_time*/
425         modified_time = _media_svc_get_file_time(dest_path);
426
427         ret = _media_svc_get_media_type_by_path(handle, org_stg_id, src_path, &media_type);
428         if (ret != MS_MEDIA_ERR_NONE) {
429                 media_svc_error("_media_svc_get_media_type_by_path failed");
430                 SAFE_FREE(file_name);
431                 return ret;
432         }
433
434         /*get old thumbnail_path and remove thumbnail */
435         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
436         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
437                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
438                 SAFE_FREE(file_name);
439                 return ret;
440         }
441
442         if (STRING_VALID(old_thumb_path)) {
443                 ret = _media_svc_remove_file(old_thumb_path);
444                 if (ret != MS_MEDIA_ERR_NONE)
445                         media_svc_error("_media_svc_remove_file failed : %d", ret);
446         }
447
448         /*move item*/
449         ret = _media_svc_update_item_by_path(org_stg_id, src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_uuid, uid);
450         SAFE_FREE(file_name);
451         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
452
453         media_svc_debug("Move is successful. Sending noti for this");
454
455         /* Get notification info */
456         media_svc_noti_item *noti_item = NULL;
457         ret = _media_svc_get_noti_info(handle, dst_stg_id, dest_path, MS_MEDIA_ITEM_FILE, &noti_item);
458         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
459
460         /* Send notification for move */
461         _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
462         _media_svc_destroy_noti_item(noti_item);
463
464         /*update folder modified_time*/
465         folder_path = g_path_get_dirname(dest_path);
466         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
467         SAFE_FREE(folder_path);
468         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
469
470         return MS_MEDIA_ERR_NONE;
471 }
472
473 int media_svc_set_item_validity_begin(int data_cnt)
474 {
475         media_svc_debug("Transaction data count : [%d]", data_cnt);
476
477         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
478
479         g_media_svc_item_validity_data_cnt = data_cnt;
480         g_media_svc_item_validity_cur_data_cnt = 0;
481
482         return MS_MEDIA_ERR_NONE;
483 }
484
485 int media_svc_set_item_validity_end(uid_t uid)
486 {
487         int ret = MS_MEDIA_ERR_NONE;
488
489         media_svc_debug_fenter();
490
491         if (g_media_svc_item_validity_cur_data_cnt > 0)
492                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
493
494         g_media_svc_item_validity_data_cnt = 1;
495         g_media_svc_item_validity_cur_data_cnt = 0;
496
497         return ret;
498 }
499
500 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
501 {
502         int ret = MS_MEDIA_ERR_NONE;
503
504         if (g_media_svc_item_validity_data_cnt == 1) {
505
506                 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
507
508         } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
509
510                 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
511                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
512
513                 g_media_svc_item_validity_cur_data_cnt++;
514
515         } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
516
517                 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
518                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
519
520                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
521                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
522
523                 g_media_svc_item_validity_cur_data_cnt = 0;
524
525         } else {
526
527                 media_svc_error("Error in media_svc_set_item_validity");
528                 return MS_MEDIA_ERR_INTERNAL;
529         }
530
531         return MS_MEDIA_ERR_NONE;
532 }
533
534 int media_svc_delete_item_by_path(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, uid_t uid)
535 {
536         int ret = MS_MEDIA_ERR_NONE;
537         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
538
539         media_svc_debug_fenter();
540
541         int media_type = -1;
542         ret = _media_svc_get_media_type_by_path(handle, storage_id, path, &media_type);
543         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
544
545         /*Get thumbnail path to delete*/
546         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
547         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
548
549         if (g_media_svc_insert_item_data_cnt == 1) {
550
551                 /* Get notification info */
552                 media_svc_noti_item *noti_item = NULL;
553                 ret = _media_svc_get_noti_info(handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
554                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
555
556                 /*Delete item*/
557                 ret = _media_svc_delete_item_by_path(is_direct, storage_id, path, FALSE, uid);
558                 if (ret != MS_MEDIA_ERR_NONE) {
559                         media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
560                         _media_svc_destroy_noti_item(noti_item);
561
562                         return ret;
563                 }
564
565                 /* Send notification */
566                 media_svc_debug("Deletion is successful. Sending noti for this");
567                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
568                 _media_svc_destroy_noti_item(noti_item);
569         } else {
570                 ret = _media_svc_delete_item_by_path(is_direct, storage_id, path, TRUE, uid);
571                 if (ret != MS_MEDIA_ERR_NONE) {
572                         media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
573                         return ret;
574                 }
575
576         }
577
578         /*Delete thumbnail*/
579         if (STRING_VALID(thumb_path)) {
580                 ret = _media_svc_remove_file(thumb_path);
581                 if (ret != MS_MEDIA_ERR_NONE)
582                         media_svc_error("fail to remove thumbnail file.");
583         }
584
585         return MS_MEDIA_ERR_NONE;
586 }
587
588 int media_svc_refresh_item(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
589 {
590         int ret = MS_MEDIA_ERR_NONE;
591         media_svc_media_type_e media_type;
592         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
593
594         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
595         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
596         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
597         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
598
599         media_svc_content_info_s content_info;
600         memset(&content_info, 0, sizeof(media_svc_content_info_s));
601
602         /*Set media info*/
603         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
604         if (ret != MS_MEDIA_ERR_NONE)
605                 return ret;
606
607         /* Initialize thumbnail information to remake thumbnail. */
608         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
609         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
610                 _media_svc_destroy_content_info(&content_info);
611                 return ret;
612         }
613
614         if (STRING_VALID(thumb_path)) {
615                 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
616                         ret = _media_svc_remove_file(thumb_path);
617                         if (ret != MS_MEDIA_ERR_NONE)
618                                 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
619                 }
620
621                 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
622                 if (ret != MS_MEDIA_ERR_NONE) {
623                         _media_svc_destroy_content_info(&content_info);
624                         return ret;
625                 }
626         }
627
628         /* Get notification info */
629         media_svc_noti_item *noti_item = NULL;
630         ret = _media_svc_get_noti_info(handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
631         if (ret != MS_MEDIA_ERR_NONE) {
632                 _media_svc_destroy_content_info(&content_info);
633                 return ret;
634         }
635
636         media_type = noti_item->media_type;
637         content_info.media_type = media_type;
638
639         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
640         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
641         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
642         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
643                 media_svc_debug("Do nothing [%d]", media_type);
644         else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
645                 ret = _media_svc_extract_image_metadata(&content_info);
646         else
647                 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
648
649         if (ret != MS_MEDIA_ERR_NONE) {
650                 _media_svc_destroy_noti_item(noti_item);
651                 _media_svc_destroy_content_info(&content_info);
652                 return ret;
653         }
654
655         /* Extracting thumbnail */
656         if (content_info.thumbnail_path == NULL) {
657                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
658                         memset(thumb_path, 0, sizeof(thumb_path));
659
660                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, media_type, uid);
661                         if (ret == MS_MEDIA_ERR_NONE)
662                                 content_info.thumbnail_path = g_strdup(thumb_path);
663                 }
664         }
665
666         ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
667
668         if (ret == MS_MEDIA_ERR_NONE) {
669                 media_svc_debug("Update is successful. Sending noti for this");
670                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
671         } else {
672                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
673         }
674
675         _media_svc_destroy_content_info(&content_info);
676         _media_svc_destroy_noti_item(noti_item);
677
678         return ret;
679 }
680
681 int media_svc_send_dir_update_noti(sqlite3 *handle, const char *storage_id, const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
682 {
683         int ret = MS_MEDIA_ERR_NONE;
684         char *uuid = NULL;
685
686         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
687         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
688
689         /* Get notification info */
690         media_svc_noti_item *noti_item = NULL;
691         ret = _media_svc_get_noti_info(handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
692         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
693
694         if (folder_id != NULL) {
695                 uuid = strndup(folder_id, strlen(folder_id));
696         } else {
697                 if (noti_item->media_uuid != NULL) {
698                         uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
699                 } else {
700                         _media_svc_destroy_noti_item(noti_item);
701                         media_svc_error("folder uuid is wrong");
702                         return MS_MEDIA_ERR_DB_INTERNAL;
703                 }
704         }
705
706         ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
707         _media_svc_destroy_noti_item(noti_item);
708         SAFE_FREE(uuid);
709
710         return ret;
711 }
712
713 int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
714 {
715         media_svc_debug_fenter();
716
717         return _media_svc_check_db_upgrade(handle, user_version, uid);
718 }
719
720 int media_svc_update_item_begin(int data_cnt)
721 {
722         media_svc_debug("Transaction data count : [%d]", data_cnt);
723
724         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
725
726         g_media_svc_update_item_data_cnt = data_cnt;
727         g_media_svc_update_item_cur_data_cnt = 0;
728
729         return MS_MEDIA_ERR_NONE;
730 }
731
732 int media_svc_update_item_end(uid_t uid)
733 {
734         int ret = MS_MEDIA_ERR_NONE;
735
736         media_svc_debug_fenter();
737
738         if (g_media_svc_update_item_cur_data_cnt > 0)
739                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
740
741         g_media_svc_update_item_data_cnt = 1;
742         g_media_svc_update_item_cur_data_cnt = 0;
743
744         return ret;
745 }
746
747 int media_svc_update_item_meta(const char *file_path, const char *storage_id, int storage_type, uid_t uid)
748 {
749         int ret = MS_MEDIA_ERR_NONE;
750         media_svc_media_type_e media_type;
751         media_svc_content_info_s content_info;
752         memset(&content_info, 0, sizeof(media_svc_content_info_s));
753
754         /*Set media info*/
755         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
756         if (ret != MS_MEDIA_ERR_NONE)
757                 return ret;
758
759         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
760                 ret = _media_svc_extract_music_metadata_for_update(&content_info, media_type);
761         else {
762                 _media_svc_destroy_content_info(&content_info);
763                 return MS_MEDIA_ERR_NONE;
764         }
765
766         if (ret != MS_MEDIA_ERR_NONE) {
767                 _media_svc_destroy_content_info(&content_info);
768                 return ret;
769         }
770
771         if (g_media_svc_update_item_data_cnt == 1) {
772
773                 ret = _media_svc_update_meta_with_data(&content_info);
774                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
775
776         } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
777
778                 ret = _media_svc_update_meta_with_data(&content_info);
779                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
780
781                 g_media_svc_update_item_cur_data_cnt++;
782
783         } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
784
785                 ret = _media_svc_update_meta_with_data(&content_info);
786                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
787
788                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
789                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
790
791                 g_media_svc_update_item_cur_data_cnt = 0;
792
793         } else {
794                 media_svc_error("Error in media_svc_update_item_meta");
795                 _media_svc_destroy_content_info(&content_info);
796                 return MS_MEDIA_ERR_INTERNAL;
797         }
798
799         _media_svc_destroy_content_info(&content_info);
800
801         return ret;
802 }
803
804 int media_svc_publish_noti(media_item_type_e update_item, media_item_update_type_e update_type, const char *path, media_type_e media_type, const char *uuid, const char *mime_type)
805 {
806         return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
807 }
808
809 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
810 {
811         return _media_svc_get_pinyin_str(src_str, pinyin_str);
812 }
813
814 int media_svc_check_pinyin_support(bool *support)
815 {
816         *support = _media_svc_check_pinyin_support();
817
818         return MS_MEDIA_ERR_NONE;
819 }
820
821 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
822 {
823         int ret = MS_MEDIA_ERR_NONE;
824
825         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
826
827         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
828         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
829
830         ret = _media_svc_update_media_view(handle, uid);
831         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
832
833         return ret;
834 }
835
836 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
837 {
838         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
839 }
840
841 int media_svc_generate_uuid(char **uuid)
842 {
843         char *gen_uuid = NULL;
844         gen_uuid = _media_info_generate_uuid();
845         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
846
847         *uuid = strdup(gen_uuid);
848
849         return MS_MEDIA_ERR_NONE;
850 }
851
852 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
853 {
854         return _media_svc_check_storage(handle, storage_id, storage_path, validity, uid);
855 }
856
857 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
858 {
859         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
860 }
861
862 int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid)
863 {
864         int ret = MS_MEDIA_ERR_NONE;
865
866         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
867         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
868         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
869         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
870
871         ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
872         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
873
874         ret = _media_svc_create_media_table_with_id(storage_id, uid);
875         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
876
877         ret = _media_svc_update_media_view(handle, uid);
878         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
879
880         /* Remove external storage that validity is 0 */
881         ret = _media_svc_delete_invalid_storage(handle, uid);
882         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
883
884         return ret;
885 }
886
887 int media_svc_insert_folder_begin(int data_cnt)
888 {
889         media_svc_debug("Transaction data count : [%d]", data_cnt);
890
891         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
892
893         g_media_svc_insert_folder_data_cnt = data_cnt;
894         g_media_svc_insert_folder_cur_data_cnt = 0;
895
896         return MS_MEDIA_ERR_NONE;
897 }
898
899 int media_svc_insert_folder_end(uid_t uid)
900 {
901         int ret = MS_MEDIA_ERR_NONE;
902
903         media_svc_debug_fenter();
904
905         if (g_media_svc_insert_folder_cur_data_cnt > 0)
906                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
907
908         g_media_svc_insert_folder_data_cnt = 1;
909         g_media_svc_insert_folder_cur_data_cnt = 0;
910
911         return ret;
912 }
913
914 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
915 {
916         int ret = MS_MEDIA_ERR_NONE;
917         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
918
919         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
920         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
921         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
922         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
923
924         if (g_media_svc_insert_folder_data_cnt == 1) {
925
926                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
927
928         } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
929
930                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
931                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
932
933                 g_media_svc_insert_folder_cur_data_cnt++;
934
935         } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
936
937                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
938                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
939
940                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
941                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
942
943                 g_media_svc_insert_folder_cur_data_cnt = 0;
944
945         } else {
946                 media_svc_error("Error in media_svc_set_insert_folder");
947                 return MS_MEDIA_ERR_INTERNAL;
948         }
949
950         return ret;
951 }
952
953 int media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
954 {
955         return _media_svc_set_folder_validity(handle, true, storage_id, start_path, validity, is_recursive, uid);
956 }
957
958 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
959 {
960         int ret = MS_MEDIA_ERR_NONE;
961         int count = -1;
962
963         ret = _media_svc_count_folder_with_path(handle, storage_id, folder_path, &count);
964         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
965
966         if (count > 0) {
967                 media_svc_debug("item is exist in database");
968                 return MS_MEDIA_ERR_NONE;
969         } else {
970                 media_svc_debug("item is not exist in database");
971                 return MS_MEDIA_ERR_DB_NO_RECORD;
972         }
973
974         return MS_MEDIA_ERR_NONE;
975 }
976
977 int media_svc_append_query(const char *query, uid_t uid)
978 {
979         return _media_svc_append_query_list(query, uid);
980 }
981
982 int media_svc_send_query(uid_t uid)
983 {
984         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
985 }
986
987 int media_svc_get_media_type(const char *path, int *mediatype)
988 {
989         return _media_svc_get_media_type(path, mediatype);
990 }
991
992 int media_svc_create_thumbnail(const char *storage_id, const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
993 {
994         int ret = MS_MEDIA_ERR_NONE;
995         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
996         char *sql = NULL;
997
998         // 1. Check media type
999         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
1000                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1001
1002         // 2. try to create thumbnail
1003         ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
1004         if (ret != MS_MEDIA_ERR_NONE) {
1005                 media_svc_error("Failed to create thumbnail [%d]", ret);
1006                 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
1007                         return ret;
1008         }
1009
1010         // 3. Update creation result to media db
1011         sql = sqlite3_mprintf("UPDATE '%q' SET media_thumbnail_path='%q' WHERE path='%q';", storage_id, thumb_path, file_path);
1012
1013         ret = _media_svc_sql_query(sql, uid);
1014         SQLITE3_SAFE_FREE(sql);
1015         if (ret != MS_MEDIA_ERR_NONE) {
1016                 media_svc_error("Failed to update media db [%d]", ret);
1017                 *thumbnail_path = g_strdup("");
1018         } else {
1019                 *thumbnail_path = g_strdup(thumb_path);
1020         }
1021
1022         return ret;
1023 }