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