[Exif] getThumbnail implementation
authorKonrad Zdunczyk <k.zdunczyk@samsung.com>
Wed, 14 Jan 2015 13:46:51 +0000 (14:46 +0100)
committerRafal Galka <r.galka@samsung.com>
Thu, 15 Jan 2015 08:20:59 +0000 (17:20 +0900)
Change-Id: Ie1ba606a5b35854b987bb2efddeb08fc7aaef57d
Signed-off-by: Konrad Zdunczyk <k.zdunczyk@samsung.com>
src/exif/exif_api.js
src/exif/exif_information.cc
src/exif/exif_instance.cc

index e36058be77c5da4c10b59941e75da454125cff23..e890c49b12abb655d86daaf36aad6a4654a6817d 100644 (file)
@@ -180,7 +180,17 @@ ExifManager.prototype.getThumbnail = function() {
     }
   ]);
 
-  throw 'Not implemented';
+  var callback = function(result) {
+    if (native_.isFailure(result)) {
+      native_.callIfPossible(args.errorCallback,
+          native_.getErrorObject(result));
+    } else {
+      var thumb = native_.getResultObject(result);
+      args.successCallback(thumb.src);
+    }
+  };
+
+  native_.call('Exif_getThumbnail', {'uri': args.uri}, callback);
 };
 
 // this function passes ExifInformation_exposureProgram_attribute test:
index 1badf3f6272a960060d0b6e61daad2160a7db9cf..b1aba683b2f8e56abe6b5d1e1233cfe6e11aeac8 100644 (file)
@@ -764,6 +764,8 @@ void ExifInformation::processEntry(ExifEntry* entry, ExifData* exif_data) {
         }
       } else {
         LoggerE("iso speed ratings: format or components count is invalid!");
+        throw common::TypeMismatchException("iso speed ratings: format or"
+            " components count is invalid!");
       }
       break;
     }
@@ -787,6 +789,8 @@ void ExifInformation::processEntry(ExifEntry* entry, ExifData* exif_data) {
         }
       } else {
         LoggerE("exposure time: format or components count is invalid!");
+        throw common::TypeMismatchException("exposure time: format or"
+            " components count is invalid!");
       }
       break;
     }
index 534061d9f68d1d3a8f75904015c2c0d7a6c54454..9d5720caf19450d465f4c5e57758ff5050edb4f7 100644 (file)
@@ -12,6 +12,7 @@
 #include <string>
 #include <sstream>
 
+#include "common/platform_exception.h"
 #include "common/task-queue.h"
 #include "common/logger.h"
 
@@ -140,6 +141,7 @@ void ExifInstance::getExifInfo(const picojson::value& args,
 
 void ExifInstance::saveExifInfo(const picojson::value& args,
                                 picojson::object& out) {
+  LoggerD("Entered");
   const std::string& uri = args.get("uri").get<std::string>();
 
   const double callback_id = args.get("callbackId").get<double>();
@@ -171,7 +173,71 @@ void ExifInstance::saveExifInfo(const picojson::value& args,
 
 void ExifInstance::getThumbnail(const picojson::value& args,
                                 picojson::object& out) {
-  LoggerE("getThumbnail is not implemented (c++)");
+  LoggerD("Entered");
+  const std::string& uri = args.get("uri").get<std::string>();
+
+  const double callback_id = args.get("callbackId").get<double>();
+  auto get = [=](const std::shared_ptr<JsonValue>& response) -> void {
+    try {
+      const std::string file_path = ExifUtil::convertUriToPath(uri);
+      ExifData* exif_data = nullptr;
+      JsonValue result = JsonValue(JsonObject());;
+      JsonObject& result_obj = result.get<JsonObject>();
+
+      std::string ext = file_path.substr(file_path.find_last_of(".") + 1);
+      std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
+
+      if ("jpg" == ext) {
+        ext = "jpeg";
+      }
+
+      if ("jpeg" == ext || "png" == ext || "gif" == ext) {
+        LoggerD("Get thumbnail from Exif in file: [%s]", file_path.c_str());
+
+        exif_data = exif_data_new_from_file(file_path.c_str());
+        if (!exif_data) {
+          throw common::UnknownException("File can not be loaded");
+        }
+
+        if (exif_data->data && exif_data->size) {
+          gchar* ch_uri = g_base64_encode(exif_data->data, exif_data->size);
+          std::string base64 = "data:image/"+ext+";base64," + ch_uri;
+
+          exif_data_unref(exif_data);
+
+          std::pair<std::string, std::string> pair;
+          pair = std::make_pair("src", base64);
+          result_obj.insert(pair);
+        } else {
+          exif_data_unref(exif_data);
+          LoggerE("File [%s] doesn't contain thumbnail", file_path.c_str());
+          throw common::InvalidValuesException("File doesn't contain"
+              " thumbnail");
+        }
+      } else {
+        LoggerE("extension: %s is not valid (jpeg/jpg/png/gif is supported)",
+            ext.c_str());
+        throw common::NotSupportedException("getThumbnail support only"
+            " jpeg/jpg/png/gif");
+      }
+
+      ReportSuccess(result, response->get<picojson::object>());
+    } catch (const common::PlatformException& e) {
+      ReportError(e, response->get<picojson::object>());
+    }
+  };
+
+  auto get_response =
+      [callback_id, this](const std::shared_ptr<JsonValue>& response) -> void {
+        picojson::object& obj = response->get<picojson::object>();
+        obj.insert(std::make_pair("callbackId", callback_id));
+        LoggerD("callback is %s", response->serialize().c_str());
+        PostMessage(response->serialize().c_str());
+      };
+
+  common::TaskQueue::GetInstance().Queue<JsonValue>(
+      get, get_response,
+      std::shared_ptr<JsonValue>(new JsonValue(JsonObject())));
 }
 
 }  // namespace exif