Commit 1b70752b authored by Hardik Juneja's avatar Hardik Juneja Committed by Hardik Juneja

Add repair function to drivetojiomapping storage

repair function syncs the files between ROOT and Jio documents and add meta data required by officejs
parent a7ee771f
......@@ -247,8 +247,75 @@
return this._sub_storage.removeAttachment(ROOT, id);
};
function cloneToDocumentExtension(context, options) {
return context._sub_storage.getAttachment(ROOT,
options.title)
.push(function (result) {
return jIO.util.readBlobAsText(result);
})
.push(function (text) {
options.text_content = text.target.result;
return context._sub_storage.putAttachment(
DOCUMENT_KEY,
options.title + DOCUMENT_EXTENSION,
new Blob([JSON.stringify(options)],
{type: "application/json"})
);
});
}
FileSystemBridgeStorage.prototype.repair = function () {
return this._sub_storage.repair.apply(this._sub_storage, arguments);
var context = this,
attachment_dict = {},
document_dict = {};
return context._sub_storage.repair.apply(this._sub_storage, arguments)
.push(function () {
return context._sub_storage.allAttachments(ROOT);
})
.push(function (result_dict) {
var key;
for (key in result_dict) {
if (result_dict.hasOwnProperty(key)) {
if (key !== undefined) {
attachment_dict[key] = null;
}
}
}
return context._sub_storage.allAttachments(DOCUMENT_KEY);
})
.push(undefined, function (error) {
if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) {
return context._sub_storage.put(DOCUMENT_KEY, {});
}
throw error;
})
.push(function (result_dict) {
var key,
attachment,
extension,
promise_list = [],
options = {};
for (key in result_dict) {
if (result_dict.hasOwnProperty(key)) {
if (key !== undefined) {
document_dict[key] = null;
}
}
}
for (attachment in attachment_dict) {
if (attachment_dict.hasOwnProperty(attachment)) {
extension = attachment.slice((Math.max(0,
attachment.lastIndexOf(".")) || Infinity) + 1);
options.type = extension;
options.title = attachment;
if (!document_dict.hasOwnProperty(attachment + '.json')) {
promise_list.push(cloneToDocumentExtension(context, options));
}
}
}
return RSVP.all(promise_list);
});
};
jIO.addStorage('drivetojiomapping', FileSystemBridgeStorage);
......
......@@ -1078,9 +1078,13 @@
return "OK";
};
Storage200.prototype.allAttachments = function () {
return {};
};
jio.repair(expected_options)
.then(function (result) {
equal(result, "OK");
deepEqual(result, []);
})
.fail(function (error) {
ok(false, error);
......@@ -1090,4 +1094,57 @@
});
});
test("repair documents correctly", function () {
stop();
expect(7);
var jio = jIO.createJIO({
type: "drivetojiomapping",
sub_storage: {
type: "drivetojiomapping200"
}
});
Storage200.prototype.repair = function (options) {
deepEqual(options, {}, "repair 200 called");
return "OK";
};
Storage200.prototype.getAttachment = function (id, name) {
equal(id, "/", "putAttachment called");
equal(name, "foo.txt", " putAttachment called");
return new Blob(["ok"], {type: "plain/text"});
};
Storage200.prototype.putAttachment = function (id, name, blob) {
equal(id, "/.jio_documents/", "putAttachment called");
equal(name, "foo.txt.json", " putAttachment called");
equal(blob.type, "application/json", " putAttachment called");
return {};
};
Storage200.prototype.allAttachments = function (id) {
if (id === "/") {
return {
"foo.txt": {}
};
}
if (id === "/.jio_documents/") {
return {};
}
};
jio.repair({})
.then(function (result) {
deepEqual(result, [{}]);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment