getAttachment method
Get an attachment. If the parameters are invalid null is returned.
Implementation
Future<dynamic> getAttachment(String id, String attachmentName) {
final opCompleter = Completer<dynamic>();
if (id.isEmpty) {
opCompleter.complete(null);
return opCompleter.future;
}
if (attachmentName.isEmpty) {
opCompleter.complete(null);
return opCompleter.future;
}
final key = '$id-$attachmentName-${_SporranDatabase.attachmentMarkerc}';
/* Check for offline, if so try the get from local storage */
if (!online) {
_database.getLocalStorageObject(key).then((dynamic document) {
final dynamic res = JsonObjectLite<dynamic>();
res.localResponse = true;
res.id = id;
res.rev = null;
res.operation = getAttachmentc;
if (document == null) {
res.ok = false;
res.payload = null;
} else {
res.ok = true;
res.payload = document;
}
opCompleter.complete(res);
if (_clientCompleter != null) {
_completionResponse = _createCompletionResponse(res);
_clientCompleter();
}
return opCompleter.future;
});
} else {
void completer(dynamic res) {
/* If Ok update local storage with the attachment */
res.operation = getAttachmentc;
res.id = id;
res.localResponse = false;
res.rev = '';
if (!res.error) {
final dynamic successResponse = res.jsonCouchResponse;
res.ok = true;
final dynamic attachment = JsonObjectLite<dynamic>();
attachment.attachmentName = attachmentName;
attachment.contentType = successResponse.contentType;
attachment.payload = res.responseText;
attachment.rev = res.rev;
res.payload = attachment;
_database.updateLocalStorageObject(
key, attachment, res.rev, _SporranDatabase.updatedc);
} else {
res.ok = false;
res.payload = null;
}
opCompleter.complete(res);
if (_clientCompleter != null) {
_completionResponse = _createCompletionResponse(res);
_clientCompleter();
}
}
/* Get the attachment from CouchDb */
_database.wilt.getAttachment(id, attachmentName).then(completer);
}
return opCompleter.future;
}