[gnome-documents] utils: factor out function to replace file
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-documents] utils: factor out function to replace file
- Date: Mon, 2 Sep 2019 18:58:39 +0000 (UTC)
commit 36b1208ed8a794250e3c43848f2fb4c5ad41229e
Author: Cosimo Cecchi <cosimoc gnome org>
Date: Mon Aug 5 16:00:57 2019 +0200
utils: factor out function to replace file
This is non-trivial code repeated many times; factor it out.
src/documents.js | 130 +++++--------------------------------------------------
src/utils.js | 31 +++++++++++++
2 files changed, 43 insertions(+), 118 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index 05be854f..752b5714 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -984,34 +984,9 @@ const GoogleDocument = class GoogleDocument extends DocCommon {
return;
}
- localFile.replace_async(
- null, false, Gio.FileCreateFlags.PRIVATE,
- GLib.PRIORITY_DEFAULT, cancellable,
- (object, res) => {
- let outputStream;
-
- try {
- outputStream = object.replace_finish(res);
- } catch (e) {
- callback(false, e);
- return;
- }
-
- outputStream.splice_async(
- inputStream,
- Gio.OutputStreamSpliceFlags.CLOSE_SOURCE | Gio.OutputStreamSpliceFlags.CLOSE_TARGET,
- GLib.PRIORITY_DEFAULT, cancellable,
- (object, res) => {
- try {
- object.splice_finish(res);
- } catch (e) {
- callback(false, e);
- return;
- }
-
- callback(false, null);
- });
- });
+ Utils.replaceFile(localFile, inputStream, cancellable, (error) => {
+ callback(false, error);
+ });
});
}
@@ -1039,32 +1014,9 @@ const GoogleDocument = class GoogleDocument extends DocCommon {
GLib.mkdir_with_parents(dirPath, 448);
let downloadFile = Gio.File.new_for_path(path);
- downloadFile.replace_async(
- null, false, Gio.FileCreateFlags.PRIVATE, GLib.PRIORITY_DEFAULT, null, (source, res) => {
- let outputStream;
-
- try {
- outputStream = downloadFile.replace_finish(res);
- } catch (e) {
- callback(false);
- return;
- }
-
- outputStream.splice_async(
- inputStream,
- Gio.OutputStreamSpliceFlags.CLOSE_SOURCE | Gio.OutputStreamSpliceFlags.CLOSE_TARGET,
- GLib.PRIORITY_DEFAULT, null,
- (source, res) => {
- try {
- outputStream.splice_finish(res);
- } catch (e) {
- callback(false);
- return;
- }
-
- callback(true);
- });
- });
+ Utils.replaceFile(downloadFile, inputStream, null, (error) => {
+ callback(!!error);
+ });
});
}
@@ -1188,38 +1140,9 @@ const OwncloudDocument = class OwncloudDocument extends DocCommon {
return;
}
- localFile.replace_async(
- null,
- false,
- Gio.FileCreateFlags.PRIVATE,
- GLib.PRIORITY_DEFAULT,
- cancellable,
- (object, res) => {
- let outputStream;
-
- try {
- outputStream = object.replace_finish(res);
- } catch (e) {
- callback(false, e);
- return;
- }
-
- outputStream.splice_async(
- inputStream,
- Gio.OutputStreamSpliceFlags.CLOSE_SOURCE | Gio.OutputStreamSpliceFlags.CLOSE_TARGET,
- GLib.PRIORITY_DEFAULT,
- cancellable,
- (object, res) => {
- try {
- object.splice_finish(res);
- } catch (e) {
- callback(false, e);
- return;
- }
-
- callback(false, null);
- });
- });
+ Utils.replaceFile(localFile, inputStream, cancellable, (error) => {
+ callback(false, error);
+ });
});
}
@@ -1313,38 +1236,9 @@ const SkydriveDocument = class SkydriveDocument extends DocCommon {
return;
}
- localFile.replace_async(
- null,
- false,
- Gio.FileCreateFlags.PRIVATE,
- GLib.PRIORITY_DEFAULT,
- cancellable,
- (object, res) => {
- let outputStream;
-
- try {
- outputStream = object.replace_finish(res);
- } catch (e) {
- callback(false, e);
- return;
- }
-
- outputStream.splice_async(
- inputStream,
- Gio.OutputStreamSpliceFlags.CLOSE_SOURCE |
Gio.OutputStreamSpliceFlags.CLOSE_TARGET,
- GLib.PRIORITY_DEFAULT,
- cancellable,
- (object, res) => {
- try {
- object.splice_finish(res);
- } catch (e) {
- callback(false, e);
- return;
- }
-
- callback(false, null);
- });
- });
+ Utils.replaceFile(localFile, inputStream, cancellable, (error) => {
+ callback(false, error);
+ });
});
});
}
diff --git a/src/utils.js b/src/utils.js
index aeb3f372..db8262ed 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -149,3 +149,34 @@ function populateActionGroup(actionGroup, actionEntries, prefix) {
actionGroup.add_action(action);
});
}
+
+function replaceFile(file, inputStream, cancellable, callback) {
+ file.replace_async(
+ null, false, Gio.FileCreateFlags.PRIVATE,
+ GLib.PRIORITY_DEFAULT, cancellable,
+ (object, res) => {
+ let outputStream;
+
+ try {
+ outputStream = object.replace_finish(res);
+ } catch (e) {
+ callback(e);
+ return;
+ }
+
+ outputStream.splice_async(
+ inputStream,
+ Gio.OutputStreamSpliceFlags.CLOSE_SOURCE | Gio.OutputStreamSpliceFlags.CLOSE_TARGET,
+ GLib.PRIORITY_DEFAULT, cancellable,
+ (object, res) => {
+ try {
+ object.splice_finish(res);
+ } catch (e) {
+ callback(e);
+ return;
+ }
+
+ callback(null);
+ });
+ });
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]