'metadata_extension.h',
'metadata_instance.cc',
'metadata_instance.h',
+ 'metadata_file_handle.cc',
+ 'metadata_file_handle.h',
],
'includes': [
'../common/pkg-config.gypi',
function MetadataManager() {}
MetadataManager.prototype.createFileHandle = function() {
- // TODO
- throw new WebAPIException(WebAPIException.NOT_SUPPORTED_ERR, 'Not implemented yet');
+ var args = validator_.validateArgs(arguments, [
+ {
+ name: 'path',
+ type: validator_.Types.STRING,
+ optional: false,
+ nullable: false
+ }
+ ]);
+ try {
+ // get normalized URI of a file
+ args.path = tizen.filesystem.toURI(args.path);
+ } catch (e) {
+ throw new WebAPIException(WebAPIException.NOT_FOUND_ERR, 'Path is invalid');
+ }
+
+ var result = native_.callSync('MetadataManagerCreateFileHandle', { uri: args.path });
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+
+ return new MetadataFileHandle(native_.getResultObject(result));
};
-function MetadataFileHandle() {
- // TODO
+function MetadataFileHandle(data) {
+ Object.defineProperties(this, {
+ _id: { value: data.id, writable: false, enumerable: false },
+ path: { value: data.path, writable: false, enumerable: true }
+ });
}
// Exports
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "metadata/metadata_file_handle.h"
+
+#include "common/logger.h"
+#include "common/platform_exception.h"
+
+namespace extension {
+namespace metadata {
+
+using namespace common;
+
+PlatformResult convertErrorCode(int err) {
+ switch (err) {
+ case METADATA_EXTRACTOR_ERROR_FILE_EXISTS:
+ return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "File does not exist.");
+ case METADATA_EXTRACTOR_ERROR_PERMISSION_DENIED:
+ return LogAndCreateResult(ErrorCode::SECURITY_ERR, "Permission denied.");
+ case METADATA_EXTRACTOR_ERROR_INVALID_PARAMETER:
+ case METADATA_EXTRACTOR_ERROR_OUT_OF_MEMORY:
+ case METADATA_EXTRACTOR_ERROR_OPERATION_FAILED:
+ default:
+ return LogAndCreateResult(ErrorCode::ABORT_ERR, "Unknown error occurred.");
+ }
+}
+
+MetadataFileHandle::MetadataFileHandle(int id) : handle_(nullptr), id_(id) {
+ ScopeLogger();
+}
+
+// 'this' owns a handle_, and path_ and invalidates 'o'
+MetadataFileHandle::MetadataFileHandle(MetadataFileHandle&& o)
+ : handle_(nullptr), id_(o.id_), path_(std::move(o.path_)) {
+ ScopeLogger();
+ handle_ = o.handle_;
+ o.handle_ = nullptr;
+}
+
+MetadataFileHandle& MetadataFileHandle::operator=(MetadataFileHandle&& o) {
+ ScopeLogger();
+ // 'this' owns a handle_, and path_ and invalidates 'o'
+ handle_ = o.handle_;
+ o.handle_ = nullptr;
+ id_ = o.id_;
+ path_ = std::move(o.path_);
+ return *this;
+}
+
+MetadataFileHandle::~MetadataFileHandle() {
+ ScopeLogger();
+ if (handle_) {
+ int result = metadata_extractor_destroy(handle_);
+ if (METADATA_EXTRACTOR_ERROR_NONE != result) {
+ LoggerW("Error during destruction of handle: %d", result);
+ return;
+ }
+ }
+}
+
+PlatformResult MetadataFileHandle::Initialize(const std::string& path) {
+ ScopeLogger();
+ int result = metadata_extractor_create(&handle_);
+ if (METADATA_EXTRACTOR_ERROR_NONE != result) {
+ LoggerE("Error during creation of metadata extractor handle, object is not valid: %d", result);
+ return convertErrorCode(result);
+ }
+
+ result = metadata_extractor_set_path(handle_, path.c_str());
+ if (METADATA_EXTRACTOR_ERROR_NONE != result) {
+ LoggerE("Error during path set, object is not valid: %d", result);
+ return convertErrorCode(result);
+ }
+ this->path_ = path;
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+picojson::value MetadataFileHandle::ToJSON() {
+ ScopeLogger();
+ picojson::value result = picojson::value(picojson::object());
+ picojson::object& result_obj = result.get<picojson::object>();
+ result_obj.insert(std::make_pair("id", picojson::value((double)id_)));
+ result_obj.insert(std::make_pair("path", picojson::value(path_)));
+
+ return result;
+}
+
+} // namespace metadata
+} // namespace extension
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef METADATA_METADATA_FILE_HANDLE_H_
+#define METADATA_METADATA_FILE_HANDLE_H_
+
+#include <metadata_extractor.h>
+#include <string>
+
+#include "common/picojson.h"
+#include "common/platform_result.h"
+
+namespace extension {
+namespace metadata {
+
+class MetadataFileHandle {
+ public:
+ MetadataFileHandle(int id);
+ MetadataFileHandle(MetadataFileHandle&& o);
+ MetadataFileHandle& operator=(MetadataFileHandle&& o);
+ virtual ~MetadataFileHandle();
+
+ picojson::value ToJSON();
+
+ common::PlatformResult Initialize(const std::string& path);
+
+ private:
+ metadata_extractor_h handle_;
+ int id_;
+ std::string path_;
+};
+
+} // namespace metadata
+} // namespace extension
+
+#endif // METADATA_METADATA_FILE_HANDLE_H_
#include "common/logger.h"
#include "common/picojson.h"
#include "common/platform_exception.h"
-#include "common/tools.h"
namespace extension {
namespace metadata {
using namespace common;
-MetadataInstance::MetadataInstance() {
+const std::string URI_PREFIX = "file://";
+
+std::string ltrim(const std::string& s) {
+ ScopeLogger();
+ std::string str = s;
+ std::string::iterator i;
+ for (i = str.begin(); i != str.end(); ++i) {
+ if (!isspace(*i)) {
+ break;
+ }
+ }
+ str.erase(str.begin(), i);
+ return str;
+}
+
+std::string convertUriToPath(const std::string& str) {
+ ScopeLogger();
+ std::string path = ltrim(str);
+ std::string prefix = path.substr(0, URI_PREFIX.size());
+
+ if (prefix == URI_PREFIX) {
+ return path.substr(URI_PREFIX.size());
+ } else {
+ return path;
+ }
+}
+
+MetadataInstance::MetadataInstance() : next_handle_id_(1) {
ScopeLogger();
using std::placeholders::_1;
using std::placeholders::_2;
ScopeLogger();
}
-void MetadataInstance::MetadataManagerCreateFileHandle(const picojson::value& args, picojson::object& out) {
+void MetadataInstance::MetadataManagerCreateFileHandle(const picojson::value& args,
+ picojson::object& out) {
ScopeLogger();
+ const std::string& uri = args.get("uri").get<std::string>();
+ auto path = convertUriToPath(uri);
- LogAndReportError(PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "Not implemented yet"), &out);
+ auto m = MetadataFileHandle{next_handle_id_};
+ auto res = m.Initialize(path);
+ if (!res) {
+ LogAndReportError(res, &out);
+ return;
+ }
+
+ ReportSuccess(m.ToJSON(), out);
+ handles_map_.insert(std::pair<int, MetadataFileHandle>(next_handle_id_++, std::move(m)));
}
void MetadataInstance::MetadataFileHandleGet(const picojson::value& args, picojson::object& out) {
LogAndReportError(PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "Not implemented yet"), &out);
}
-void MetadataInstance::MetadataFileHandleRelease(const picojson::value& args, picojson::object& out) {
+void MetadataInstance::MetadataFileHandleRelease(const picojson::value& args,
+ picojson::object& out) {
ScopeLogger();
LogAndReportError(PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "Not implemented yet"), &out);
#define METADATA_METADATA_INSTANCE_H_
#include "common/extension.h"
+#include "metadata/metadata_file_handle.h"
namespace extension {
namespace metadata {
void MetadataFileHandleGet(const picojson::value& args, picojson::object& out);
void MetadataFileHandleRelease(const picojson::value& args, picojson::object& out);
+ std::map<int, MetadataFileHandle> handles_map_;
+ int next_handle_id_;
};
} // namespace metadata