From: Tomasz Marciniak Date: Tue, 3 Nov 2015 13:26:29 +0000 (+0100) Subject: [File] Added FileWriter implementation. X-Git-Tag: submit/tizen/20151221.111205^2~58^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=556e8d0ac276284966aed266cd5a1aac895e4b0f;p=platform%2Fcore%2Fapi%2Fcordova-plugins.git [File] Added FileWriter implementation. [Verification] Code compiles without errors. Change-Id: I9e5f11b4e0a32ff045e2aa0e7384ae80a83d1e55 Signed-off-by: Tomasz Marciniak --- diff --git a/src/file/cordova_file.gyp b/src/file/cordova_file.gyp index fd9b7d7..709906b 100644 --- a/src/file/cordova_file.gyp +++ b/src/file/cordova_file.gyp @@ -10,6 +10,8 @@ 'cordova_file_api.js', 'cordova_file_extension.cc', 'cordova_file_extension.h', + 'cordova_file_instance.cc', + 'cordova_file_instance.h', ], 'include_dirs': [ '../', diff --git a/src/file/cordova_file_extension.cc b/src/file/cordova_file_extension.cc index 69eb837..02236b8 100755 --- a/src/file/cordova_file_extension.cc +++ b/src/file/cordova_file_extension.cc @@ -15,6 +15,7 @@ */ #include "file/cordova_file_extension.h" +#include "file/cordova_file_instance.h" // This will be generated from cordova_file_api.js extern const char kSource_cordova_file_api[]; @@ -34,6 +35,11 @@ CordovaFileExtension::CordovaFileExtension() { CordovaFileExtension::~CordovaFileExtension() {} +common::Instance* CordovaFileExtension::CreateInstance() { + LoggerD("Entered"); + return new CordovaFileInstance(); +} + } // file } // cordova } // extension diff --git a/src/file/cordova_file_extension.h b/src/file/cordova_file_extension.h index 0c3bc12..bbf4707 100755 --- a/src/file/cordova_file_extension.h +++ b/src/file/cordova_file_extension.h @@ -27,6 +27,10 @@ class CordovaFileExtension : public common::Extension { public: CordovaFileExtension(); virtual ~CordovaFileExtension(); + + private: + // common::Extension implementation. + virtual common::Instance* CreateInstance(); }; } // file diff --git a/src/file/cordova_file_instance.cc b/src/file/cordova_file_instance.cc new file mode 100644 index 0000000..53ec8a5 --- /dev/null +++ b/src/file/cordova_file_instance.cc @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2015 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 "file/cordova_file_instance.h" + +#include + +#include +#include +#include + +namespace extension { +namespace cordova { +namespace file { + +using common::ErrorCode; +using common::PlatformResult; + +CordovaFileInstance::CordovaFileInstance() { + using std::placeholders::_1; + using std::placeholders::_2; + + LoggerD("Entered"); + +#define REGISTER_SYNC(c, x) \ + RegisterSyncHandler(c, std::bind(&CordovaFileInstance::x, this, _1, _2)); +#define REGISTER_ASYNC(c, x) \ + RegisterSyncHandler(c, std::bind(&CordovaFileInstance::x, this, _1, _2)); + + REGISTER_SYNC("File_truncate", Truncate); + +#undef REGISTER_SYNC +#undef REGISTER_ASYNC +} + +CordovaFileInstance::~CordovaFileInstance() { + LoggerD("Entered"); +} + +void CordovaFileInstance::Truncate(const picojson::value& args, picojson::object& out) { + LoggerD("Entered"); + + if (!args.contains("uri") || !args.contains("length")) { + LoggerE("Invalid parameter passed."); + ReportError(PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid parameter passed."), &out); + return; + } + + const double length = args.get("length").get(); + const std::string& path = args.get("uri").get(); + + int ret = truncate(path.c_str(), length); + LoggerD("Truncate returned value [%d]", ret); + + if (!ret) { + ReportSuccess(out); + } else { + ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "File truncate failed."), &out); + } +} + +} // file +} // cordova +} // extension diff --git a/src/file/cordova_file_instance.h b/src/file/cordova_file_instance.h new file mode 100644 index 0000000..e85b6a1 --- /dev/null +++ b/src/file/cordova_file_instance.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 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 FILE_CORDOVA_FILE_INSTANCE_H_ +#define FILE_CORDOVA_FILE_INSTANCE_H_ + +#include +#include + +namespace extension { +namespace cordova { +namespace file { + +class CordovaFileInstance : public common::ParsedInstance { + public: + CordovaFileInstance(); + virtual ~CordovaFileInstance(); + + private: + void Truncate(const picojson::value& args, picojson::object& out); +}; +} // file +} // cordova +} // extension + +#endif // FILE_CORDOVA_FILE_INSTANCE_H_ diff --git a/src/file/js/FileWriter.js b/src/file/js/FileWriter.js index 7782fef..91b4222 100644 --- a/src/file/js/FileWriter.js +++ b/src/file/js/FileWriter.js @@ -18,9 +18,76 @@ cordova.define('cordova-plugin-file.tizen.FileWriter', function(require, exports, module) { // TODO: remove -> end +var utils_ = xwalk.utils; +var native_ = new utils_.NativeManager(extension); + module.exports = { - write: function(successCallback, errorCallback, args) {}, - truncate: function(successCallback, errorCallback, args) {} + write: function(successCallback, errorCallback, args) { + var uri = args[0]; + var data = args[1]; + var position = args[2]; + + var onSuccess = function (file) { + if (file.isDirectory) { + errorCallback && errorCallback(FileError.INVALID_MODIFICATION_ERR); + return; + } + + var openStreamSuccess = function (stream) { + try { + stream.position = position; + stream.write(data); + var length = stream.position - position; + stream.close(); + successCallback && successCallback(length); + } catch (error) { + errorCallback && errorCallback(ConvErrorCode(error.code)); + } + } + + var openStreamError = function (error) { + errorCallback && errorCallback(ConvErrorCode(error.code)); + } + + try { + file.openStream('rw', openStreamSuccess, openStreamError); + } catch (error) { + errorCallback && errorCallback(ConvErrorCode(error.code)); + } + } + + var onError = function (error) { + errorCallback && errorCallback(ConvErrorCode(error.code)); + } + + try { + tizen.filesystem.resolve(uri, onSuccess, onError, 'rw'); + } catch (error) { + errorCallback && errorCallback(ConvErrorCode(error.code)); + } + }, + + truncate: function(successCallback, errorCallback, args) { + var uriPrefix = 'file://'; + var uri = args[0]; + var length = args[1]; + + if (uri.indexOf(uriPrefix) === 0) { + uri = uri.substr(uriPrefix.length); + } + + var callArgs = { + 'uri': uri, + 'length': length + }; + + var result = native_.callSync('File_truncate', callArgs); + if (native_.isFailure(result)) { + errorCallback && errorCallback(ConvErrorCode(native_.getErrorObject(result).code)); + } else { + successCallback && successCallback(length); + } + } }; //TODO: remove when added to public cordova repository -> begin