Match columns of storage with filter keyword
[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_debug("Transaction data count : [%d]", data_cnt);
193
194         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
195
196         g_media_svc_insert_item_data_cnt = data_cnt;
197         g_media_svc_insert_item_cur_data_cnt = 0;
198
199         /* Prepare for making noti item list */
200         if (with_noti) {
201                 media_svc_debug("making noti list from pid[%d]", from_pid);
202                 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
203                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
204
205                 _media_svc_set_noti_from_pid(from_pid);
206                 g_insert_with_noti = TRUE;
207         }
208
209         return MS_MEDIA_ERR_NONE;
210 }
211
212 int media_svc_insert_item_end(uid_t uid)
213 {
214         int ret = MS_MEDIA_ERR_NONE;
215
216         media_svc_debug_fenter();
217
218         if (g_media_svc_insert_item_cur_data_cnt > 0) {
219
220                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
221                 if (g_insert_with_noti) {
222                         media_svc_debug("sending noti list");
223                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
224                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
225                         g_insert_with_noti = FALSE;
226                         _media_svc_set_noti_from_pid(-1);
227                 }
228         }
229
230         g_media_svc_insert_item_data_cnt = 1;
231         g_media_svc_insert_item_cur_data_cnt = 0;
232
233         return ret;
234 }
235
236 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)
237 {
238         int ret = MS_MEDIA_ERR_NONE;
239         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
240         media_svc_media_type_e media_type;
241
242         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
243         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
244         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
245         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
246
247         media_svc_content_info_s content_info;
248         memset(&content_info, 0, sizeof(media_svc_content_info_s));
249
250         /*Set media info*/
251         /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
252         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
253         if (ret != MS_MEDIA_ERR_NONE)
254                 return ret;
255
256         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
257         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
258         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
259         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
260                 media_svc_debug("Do nothing[%d]", media_type);
261         else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
262                 ret = _media_svc_extract_image_metadata(&content_info);
263         else
264                 ret = _media_svc_extract_media_metadata(handle, &content_info, uid);
265         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
266
267         /*Set or Get folder id*/
268         ret = _media_svc_get_and_append_folder_id_by_path(handle, storage_id, path, storage_type, folder_uuid, uid);
269         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
270
271         content_info.folder_uuid = g_strdup(folder_uuid);
272         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
273
274         if (g_media_svc_insert_item_data_cnt == 1) {
275
276                 ret = _media_svc_insert_item_with_data(handle, storage_id, &content_info, FALSE, uid);
277                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
278
279                 if (g_insert_with_noti)
280                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
281
282         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
283
284                 ret = _media_svc_insert_item_with_data(handle, storage_id, &content_info, TRUE, uid);
285                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
286
287                 if (g_insert_with_noti)
288                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
289
290                 g_media_svc_insert_item_cur_data_cnt++;
291
292         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
293
294                 ret = _media_svc_insert_item_with_data(handle, storage_id, &content_info, TRUE, uid);
295                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
296
297                 if (g_insert_with_noti)
298                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
299
300                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
301                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
302
303                 if (g_insert_with_noti) {
304                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
305                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
306
307                         /* Recreate noti list */
308                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
309                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
310                 }
311
312                 g_media_svc_insert_item_cur_data_cnt = 0;
313
314         } else {
315                 media_svc_error("Error in media_svc_insert_item_bulk");
316                 _media_svc_destroy_content_info(&content_info);
317                 return MS_MEDIA_ERR_INTERNAL;
318         }
319
320         _media_svc_destroy_content_info(&content_info);
321
322         return MS_MEDIA_ERR_NONE;
323 }
324
325 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)
326 {
327         int ret = MS_MEDIA_ERR_NONE;
328         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
329         media_svc_media_type_e media_type;
330
331         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
332         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
333         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
334         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
335
336         media_svc_content_info_s content_info;
337         memset(&content_info, 0, sizeof(media_svc_content_info_s));
338
339         /*Set media info*/
340         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
341         if (ret != MS_MEDIA_ERR_NONE)
342                 return ret;
343
344         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
345         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
346         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
347         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
348                 /*Do nothing.*/
349         } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
350                 ret = _media_svc_extract_image_metadata(&content_info);
351         } else {
352                 ret = _media_svc_extract_media_metadata(handle, &content_info, uid);
353         }
354
355         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
356
357         /*Set or Get folder id*/
358         ret = _media_svc_get_and_append_folder_id_by_path(handle, storage_id, path, storage_type, folder_uuid, uid);
359         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
360
361         content_info.folder_uuid = g_strdup(folder_uuid);
362         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
363
364         /* Extracting thumbnail */
365         if (content_info.thumbnail_path == NULL) {
366                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
367                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
368
369                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, uid);
370                         if (ret == MS_MEDIA_ERR_NONE)
371                                 content_info.thumbnail_path = g_strdup(thumb_path);
372                 }
373         }
374
375         ret = _media_svc_insert_item_with_data(handle, storage_id, &content_info, FALSE, uid);
376
377         if (ret == MS_MEDIA_ERR_NONE) {
378                 media_svc_debug("Insertion is successful. Sending noti for this");
379                 _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);
380         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
381                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
382         }
383
384         _media_svc_destroy_content_info(&content_info);
385         return ret;
386 }
387
388 int media_svc_move_item(sqlite3 *handle, const char *src_path, const char *dest_path, uid_t uid)
389 {
390         int ret = MS_MEDIA_ERR_NONE;
391         char *file_name = NULL;
392         char *folder_path = NULL;
393         int modified_time = 0;
394         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
395         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
396         char org_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
397         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
398         ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
399         ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
400         int media_type = -1;
401
402         media_svc_debug_fenter();
403
404         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
405         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
406         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
407
408         /* Get storage_id */
409         ret = _media_svc_get_storage_uuid(handle, src_path, org_stg_id, uid);
410         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
411         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
412         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
413         /* Get storage_type */
414         ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
415         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
416         ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
417         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
418
419         /*check and update folder*/
420         ret = _media_svc_get_and_append_folder_id_by_path(handle, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
421         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
422
423         /*get filename*/
424         file_name = g_path_get_basename(dest_path);
425
426         /*get modified_time*/
427         modified_time = _media_svc_get_file_time(dest_path);
428
429         ret = _media_svc_get_media_type_by_path(handle, org_stg_id, src_path, &media_type);
430         if (ret != MS_MEDIA_ERR_NONE) {
431                 media_svc_error("_media_svc_get_media_type_by_path failed");
432                 SAFE_FREE(file_name);
433                 return ret;
434         }
435
436         /*get old thumbnail_path and remove thumbnail */
437         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
438         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
439                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
440                 SAFE_FREE(file_name);
441                 return ret;
442         }
443
444         if (STRING_VALID(old_thumb_path)) {
445                 ret = _media_svc_remove_file(old_thumb_path);
446                 if (ret != MS_MEDIA_ERR_NONE)
447                         media_svc_error("_media_svc_remove_file failed : %d", ret);
448         }
449
450         /*move item*/
451         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);
452         SAFE_FREE(file_name);
453         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
454
455         media_svc_debug("Move is successful. Sending noti for this");
456
457         /* Get notification info */
458         media_svc_noti_item *noti_item = NULL;
459         ret = _media_svc_get_noti_info(handle, dst_stg_id, dest_path, MS_MEDIA_ITEM_FILE, &noti_item);
460         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
461
462         /* Send notification for move */
463         _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
464         _media_svc_destroy_noti_item(noti_item);
465
466         /*update folder modified_time*/
467         folder_path = g_path_get_dirname(dest_path);
468         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
469         SAFE_FREE(folder_path);
470         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
471
472         return MS_MEDIA_ERR_NONE;
473 }
474
475 int media_svc_set_item_validity_begin(int data_cnt)
476 {
477         media_svc_debug("Transaction data count : [%d]", data_cnt);
478
479         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
480
481         g_media_svc_item_validity_data_cnt = data_cnt;
482         g_media_svc_item_validity_cur_data_cnt = 0;
483
484         return MS_MEDIA_ERR_NONE;
485 }
486
487 int media_svc_set_item_validity_end(uid_t uid)
488 {
489         int ret = MS_MEDIA_ERR_NONE;
490
491         media_svc_debug_fenter();
492
493         if (g_media_svc_item_validity_cur_data_cnt > 0)
494                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
495
496         g_media_svc_item_validity_data_cnt = 1;
497         g_media_svc_item_validity_cur_data_cnt = 0;
498
499         return ret;
500 }
501
502 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
503 {
504         int ret = MS_MEDIA_ERR_NONE;
505
506         if (g_media_svc_item_validity_data_cnt == 1) {
507
508                 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
509
510         } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
511
512                 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
513                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
514
515                 g_media_svc_item_validity_cur_data_cnt++;
516
517         } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
518
519                 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
520                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
521
522                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
523                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
524
525                 g_media_svc_item_validity_cur_data_cnt = 0;
526
527         } else {
528
529                 media_svc_error("Error in media_svc_set_item_validity");
530                 return MS_MEDIA_ERR_INTERNAL;
531         }
532
533         return MS_MEDIA_ERR_NONE;
534 }
535
536 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
537 {
538         int ret = MS_MEDIA_ERR_NONE;
539         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
540
541         media_svc_debug_fenter();
542
543         int media_type = -1;
544         ret = _media_svc_get_media_type_by_path(handle, storage_id, path, &media_type);
545         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
546
547         /*Get thumbnail path to delete*/
548         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
549         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
550
551         if (g_media_svc_insert_item_data_cnt == 1) {
552
553                 /* Get notification info */
554                 media_svc_noti_item *noti_item = NULL;
555                 ret = _media_svc_get_noti_info(handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
556                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
557
558                 /*Delete item*/
559                 ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid);
560                 if (ret != MS_MEDIA_ERR_NONE) {
561                         media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
562                         _media_svc_destroy_noti_item(noti_item);
563
564                         return ret;
565                 }
566
567                 /* Send notification */
568                 media_svc_debug("Deletion is successful. Sending noti for this");
569                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
570                 _media_svc_destroy_noti_item(noti_item);
571         } else {
572                 ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid);
573                 if (ret != MS_MEDIA_ERR_NONE) {
574                         media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
575                         return ret;
576                 }
577
578         }
579
580         /*Delete thumbnail*/
581         if (STRING_VALID(thumb_path)) {
582                 ret = _media_svc_remove_file(thumb_path);
583                 if (ret != MS_MEDIA_ERR_NONE)
584                         media_svc_error("fail to remove thumbnail file.");
585         }
586
587         return MS_MEDIA_ERR_NONE;
588 }
589
590 int media_svc_refresh_item(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
591 {
592         int ret = MS_MEDIA_ERR_NONE;
593         media_svc_media_type_e media_type;
594         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
595
596         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
597         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
598         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
599         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
600
601         media_svc_content_info_s content_info;
602         memset(&content_info, 0, sizeof(media_svc_content_info_s));
603
604         /*Set media info*/
605         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
606         if (ret != MS_MEDIA_ERR_NONE)
607                 return ret;
608
609         /* Initialize thumbnail information to remake thumbnail. */
610         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
611         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
612                 _media_svc_destroy_content_info(&content_info);
613                 return ret;
614         }
615
616         if (STRING_VALID(thumb_path)) {
617                 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
618                         ret = _media_svc_remove_file(thumb_path);
619                         if (ret != MS_MEDIA_ERR_NONE)
620                                 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
621                 }
622
623                 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
624                 if (ret != MS_MEDIA_ERR_NONE) {
625                         _media_svc_destroy_content_info(&content_info);
626                         return ret;
627                 }
628         }
629
630         /* Get notification info */
631         media_svc_noti_item *noti_item = NULL;
632         ret = _media_svc_get_noti_info(handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
633         if (ret != MS_MEDIA_ERR_NONE) {
634                 _media_svc_destroy_content_info(&content_info);
635                 return ret;
636         }
637
638         media_type = noti_item->media_type;
639         content_info.media_type = media_type;
640
641         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
642         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
643         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
644         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
645                 media_svc_debug("Do nothing [%d]", media_type);
646         else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
647                 ret = _media_svc_extract_image_metadata(&content_info);
648         else
649                 ret = _media_svc_extract_media_metadata(handle, &content_info, uid);
650
651         if (ret != MS_MEDIA_ERR_NONE) {
652                 _media_svc_destroy_noti_item(noti_item);
653                 _media_svc_destroy_content_info(&content_info);
654                 return ret;
655         }
656
657         /* Extracting thumbnail */
658         if (content_info.thumbnail_path == NULL) {
659                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
660                         memset(thumb_path, 0, sizeof(thumb_path));
661
662                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, uid);
663                         if (ret == MS_MEDIA_ERR_NONE)
664                                 content_info.thumbnail_path = g_strdup(thumb_path);
665                 }
666         }
667
668         ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
669
670         if (ret == MS_MEDIA_ERR_NONE) {
671                 media_svc_debug("Update is successful. Sending noti for this");
672                 _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);
673         } else {
674                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
675         }
676
677         _media_svc_destroy_content_info(&content_info);
678         _media_svc_destroy_noti_item(noti_item);
679
680         return ret;
681 }
682
683 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)
684 {
685         int ret = MS_MEDIA_ERR_NONE;
686         char *uuid = NULL;
687
688         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
689         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
690
691         /* Get notification info */
692         media_svc_noti_item *noti_item = NULL;
693         ret = _media_svc_get_noti_info(handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
694         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
695
696         if (folder_id != NULL) {
697                 uuid = strndup(folder_id, strlen(folder_id));
698         } else {
699                 if (noti_item->media_uuid != NULL) {
700                         uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
701                 } else {
702                         _media_svc_destroy_noti_item(noti_item);
703                         media_svc_error("folder uuid is wrong");
704                         return MS_MEDIA_ERR_DB_INTERNAL;
705                 }
706         }
707
708         ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
709         _media_svc_destroy_noti_item(noti_item);
710         SAFE_FREE(uuid);
711
712         return ret;
713 }
714
715 int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
716 {
717         media_svc_debug_fenter();
718
719         return _media_svc_check_db_upgrade(handle, user_version, uid);
720 }
721
722 int media_svc_update_item_begin(int data_cnt)
723 {
724         media_svc_debug("Transaction data count : [%d]", data_cnt);
725
726         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
727
728         g_media_svc_update_item_data_cnt = data_cnt;
729         g_media_svc_update_item_cur_data_cnt = 0;
730
731         return MS_MEDIA_ERR_NONE;
732 }
733
734 int media_svc_update_item_end(uid_t uid)
735 {
736         int ret = MS_MEDIA_ERR_NONE;
737
738         media_svc_debug_fenter();
739
740         if (g_media_svc_update_item_cur_data_cnt > 0)
741                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
742
743         g_media_svc_update_item_data_cnt = 1;
744         g_media_svc_update_item_cur_data_cnt = 0;
745
746         return ret;
747 }
748
749 int media_svc_update_item_meta(const char *file_path, const char *storage_id, int storage_type, uid_t uid)
750 {
751         int ret = MS_MEDIA_ERR_NONE;
752         media_svc_media_type_e media_type;
753         media_svc_content_info_s content_info;
754         memset(&content_info, 0, sizeof(media_svc_content_info_s));
755
756         /*Set media info*/
757         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
758         if (ret != MS_MEDIA_ERR_NONE)
759                 return ret;
760
761         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
762                 ret = _media_svc_extract_music_metadata_for_update(&content_info, media_type);
763         else {
764                 _media_svc_destroy_content_info(&content_info);
765                 return MS_MEDIA_ERR_NONE;
766         }
767
768         if (ret != MS_MEDIA_ERR_NONE) {
769                 _media_svc_destroy_content_info(&content_info);
770                 return ret;
771         }
772
773         if (g_media_svc_update_item_data_cnt == 1) {
774
775                 ret = _media_svc_update_meta_with_data(&content_info);
776                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
777
778         } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
779
780                 ret = _media_svc_update_meta_with_data(&content_info);
781                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
782
783                 g_media_svc_update_item_cur_data_cnt++;
784
785         } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
786
787                 ret = _media_svc_update_meta_with_data(&content_info);
788                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
789
790                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
791                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
792
793                 g_media_svc_update_item_cur_data_cnt = 0;
794
795         } else {
796                 media_svc_error("Error in media_svc_update_item_meta");
797                 _media_svc_destroy_content_info(&content_info);
798                 return MS_MEDIA_ERR_INTERNAL;
799         }
800
801         _media_svc_destroy_content_info(&content_info);
802
803         return ret;
804 }
805
806 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)
807 {
808         return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
809 }
810
811 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
812 {
813         return _media_svc_get_pinyin_str(src_str, pinyin_str);
814 }
815
816 int media_svc_check_pinyin_support(bool *support)
817 {
818         *support = _media_svc_check_pinyin_support();
819
820         return MS_MEDIA_ERR_NONE;
821 }
822
823 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
824 {
825         int ret = MS_MEDIA_ERR_NONE;
826
827         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
828
829         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
830         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
831
832         ret = _media_svc_update_media_view(handle, uid);
833         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
834
835         return ret;
836 }
837
838 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
839 {
840         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
841 }
842
843 int media_svc_generate_uuid(char **uuid)
844 {
845         char *gen_uuid = NULL;
846         gen_uuid = _media_info_generate_uuid();
847         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
848
849         *uuid = strdup(gen_uuid);
850
851         return MS_MEDIA_ERR_NONE;
852 }
853
854 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
855 {
856         return _media_svc_check_storage(handle, storage_id, storage_path, validity, uid);
857 }
858
859 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
860 {
861         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
862 }
863
864 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)
865 {
866         int ret = MS_MEDIA_ERR_NONE;
867
868         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
869         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
870         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
871         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
872
873         ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
874         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
875
876         ret = _media_svc_create_media_table_with_id(storage_id, uid);
877         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
878
879         ret = _media_svc_update_media_view(handle, uid);
880         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
881
882         /* Remove external storage that validity is 0 */
883         ret = _media_svc_delete_invalid_storage(handle, uid);
884         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
885
886         return ret;
887 }
888
889 int media_svc_insert_folder_begin(int data_cnt)
890 {
891         media_svc_debug("Transaction data count : [%d]", data_cnt);
892
893         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
894
895         g_media_svc_insert_folder_data_cnt = data_cnt;
896         g_media_svc_insert_folder_cur_data_cnt = 0;
897
898         return MS_MEDIA_ERR_NONE;
899 }
900
901 int media_svc_insert_folder_end(uid_t uid)
902 {
903         int ret = MS_MEDIA_ERR_NONE;
904
905         media_svc_debug_fenter();
906
907         if (g_media_svc_insert_folder_cur_data_cnt > 0)
908                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
909
910         g_media_svc_insert_folder_data_cnt = 1;
911         g_media_svc_insert_folder_cur_data_cnt = 0;
912
913         return ret;
914 }
915
916 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
917 {
918         int ret = MS_MEDIA_ERR_NONE;
919         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
920
921         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
922         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
923         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
924         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
925
926         if (g_media_svc_insert_folder_data_cnt == 1) {
927
928                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
929
930         } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
931
932                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
933                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
934
935                 g_media_svc_insert_folder_cur_data_cnt++;
936
937         } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
938
939                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
940                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
941
942                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
943                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
944
945                 g_media_svc_insert_folder_cur_data_cnt = 0;
946
947         } else {
948                 media_svc_error("Error in media_svc_set_insert_folder");
949                 return MS_MEDIA_ERR_INTERNAL;
950         }
951
952         return ret;
953 }
954
955 int media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
956 {
957         return _media_svc_set_folder_validity(handle, storage_id, start_path, validity, is_recursive, uid);
958 }
959
960 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
961 {
962         int ret = MS_MEDIA_ERR_NONE;
963         int count = -1;
964
965         ret = _media_svc_count_folder_with_path(handle, storage_id, folder_path, &count);
966         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
967
968         if (count > 0) {
969                 media_svc_debug("item is exist in database");
970                 return MS_MEDIA_ERR_NONE;
971         } else {
972                 media_svc_debug("item is not exist in database");
973                 return MS_MEDIA_ERR_DB_NO_RECORD;
974         }
975
976         return MS_MEDIA_ERR_NONE;
977 }
978
979 int media_svc_append_query(const char *query, uid_t uid)
980 {
981         return _media_svc_append_query_list(query, uid);
982 }
983
984 int media_svc_send_query(uid_t uid)
985 {
986         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
987 }
988
989 int media_svc_get_media_type(const char *path, int *mediatype)
990 {
991         return _media_svc_get_media_type(path, mediatype);
992 }
993
994 int media_svc_create_thumbnail(const char *storage_id, const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
995 {
996         int ret = MS_MEDIA_ERR_NONE;
997         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
998         char *sql = NULL;
999
1000         // 1. Check media type
1001         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
1002                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1003
1004         // 2. try to create thumbnail
1005         ret = _media_svc_create_thumbnail(file_path, thumb_path, MEDIA_SVC_PATHNAME_SIZE, media_type, uid);
1006         if (ret != MS_MEDIA_ERR_NONE) {
1007                 media_svc_error("Failed to create thumbnail [%d]", ret);
1008                 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
1009                         return ret;
1010         }
1011
1012         // 3. Update creation result to media db
1013         sql = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path='%q' WHERE path='%q';", storage_id, thumb_path, file_path);
1014
1015         ret = _media_svc_sql_query(sql, uid);
1016         SQLITE3_SAFE_FREE(sql);
1017         if (ret != MS_MEDIA_ERR_NONE) {
1018                 media_svc_error("Failed to update media db [%d]", ret);
1019                 *thumbnail_path = g_strdup("");
1020         } else {
1021                 *thumbnail_path = g_strdup(thumb_path);
1022         }
1023
1024         return ret;
1025 }