refactored cloudstorage dao

This commit is contained in:
Tankred Hase 2013-06-10 20:10:39 +02:00
parent 019c23b511
commit 07325b8ed0
1 changed files with 34 additions and 28 deletions

View File

@ -2,9 +2,11 @@
* High level storage api for handling syncing of data to * High level storage api for handling syncing of data to
* and from the cloud. * and from the cloud.
*/ */
app.dao.CloudStorage = function(window, $) { define(['jquery'], function($) {
'use strict'; 'use strict';
var self = {};
// //
// Generic Ajax helper functions // Generic Ajax helper functions
// //
@ -12,7 +14,7 @@ app.dao.CloudStorage = function(window, $) {
/** /**
* GET (read) request * GET (read) request
*/ */
this.get = function(uri, callback) { self.get = function(uri, callback) {
$.ajax({ $.ajax({
url: uri, url: uri,
type: 'GET', type: 'GET',
@ -22,7 +24,8 @@ app.dao.CloudStorage = function(window, $) {
}, },
error: function(xhr, textStatus, err) { error: function(xhr, textStatus, err) {
callback({ callback({
errMsg: xhr.status + ': ' + xhr.statusText errMsg: xhr.status + ': ' + xhr.statusText,
err: err
}); });
} }
}); });
@ -31,7 +34,7 @@ app.dao.CloudStorage = function(window, $) {
/** /**
* PUT (update) request * PUT (update) request
*/ */
this.put = function(item, uri, callback) { self.put = function(item, uri, callback) {
$.ajax({ $.ajax({
url: uri, url: uri,
type: 'PUT', type: 'PUT',
@ -42,7 +45,8 @@ app.dao.CloudStorage = function(window, $) {
}, },
error: function(xhr, textStatus, err) { error: function(xhr, textStatus, err) {
callback({ callback({
errMsg: xhr.status + ': ' + xhr.statusText errMsg: xhr.status + ': ' + xhr.statusText,
err: err
}); });
} }
}); });
@ -51,7 +55,7 @@ app.dao.CloudStorage = function(window, $) {
/** /**
* DELETE (remove) request * DELETE (remove) request
*/ */
this.remove = function(uri, callback) { self.remove = function(uri, callback) {
$.ajax({ $.ajax({
url: uri, url: uri,
type: 'DELETE', type: 'DELETE',
@ -60,7 +64,8 @@ app.dao.CloudStorage = function(window, $) {
}, },
error: function(xhr, textStatus, err) { error: function(xhr, textStatus, err) {
callback({ callback({
errMsg: xhr.status + ': ' + xhr.statusText errMsg: xhr.status + ': ' + xhr.statusText,
err: err
}); });
} }
}); });
@ -74,18 +79,18 @@ app.dao.CloudStorage = function(window, $) {
* Pushes an encrypted item to the user's cloud storage * Pushes an encrypted item to the user's cloud storage
* @param type [String] The type of item e.g. 'email' * @param type [String] The type of item e.g. 'email'
*/ */
this.putEncryptedItem = function(item, type, emailAddress, folderName, callback) { self.putEncryptedItem = function(item, type, emailAddress, folderName, callback) {
var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName + '/' + item.id; var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName + '/' + item.id;
this.put(item, uri, callback); self.put(item, uri, callback);
}; };
/** /**
* Delete an encrypted item from the cloud * Delete an encrypted item from the cloud
* @param type [String] The type of item e.g. 'email' * @param type [String] The type of item e.g. 'email'
*/ */
this.deleteEncryptedItem = function(id, type, emailAddress, folderName, callback) { self.deleteEncryptedItem = function(id, type, emailAddress, folderName, callback) {
var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName + '/' + id; var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName + '/' + id;
this.remove(uri, callback); self.remove(uri, callback);
}; };
/** /**
@ -94,9 +99,9 @@ app.dao.CloudStorage = function(window, $) {
* @param offset [Number] The offset of items to fetch (0 is the last stored item) * @param offset [Number] The offset of items to fetch (0 is the last stored item)
* @param num [Number] The number of items to fetch (null means fetch all) * @param num [Number] The number of items to fetch (null means fetch all)
*/ */
this.listEncryptedItems = function(type, emailAddress, folderName, callback) { self.listEncryptedItems = function(type, emailAddress, folderName, callback) {
var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName; var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName;
this.get(uri, callback); self.get(uri, callback);
}; };
// //
@ -106,10 +111,10 @@ app.dao.CloudStorage = function(window, $) {
/** /**
* Find the user's corresponding public key * Find the user's corresponding public key
*/ */
this.getPublicKey = function(keyId, callback) { self.getPublicKey = function(keyId, callback) {
var uri = app.config.cloudUrl + '/publickey/key/' + keyId; var uri = app.config.cloudUrl + '/publickey/key/' + keyId;
this.get(uri, function(err, key) { self.get(uri, function(err, key) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
@ -129,10 +134,10 @@ app.dao.CloudStorage = function(window, $) {
/** /**
* Find the user's corresponding public key by email * Find the user's corresponding public key by email
*/ */
this.getPublicKeyByUserId = function(userId, callback) { self.getPublicKeyByUserId = function(userId, callback) {
var uri = app.config.cloudUrl + '/publickey/user/' + userId; var uri = app.config.cloudUrl + '/publickey/user/' + userId;
this.get(uri, function(err, keys) { self.get(uri, function(err, keys) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
@ -159,17 +164,17 @@ app.dao.CloudStorage = function(window, $) {
/** /**
* Persist the user's publc key * Persist the user's publc key
*/ */
this.putPublicKey = function(pubkey, callback) { self.putPublicKey = function(pubkey, callback) {
var uri = app.config.cloudUrl + '/publickey/user/' + pubkey.userId + '/key/' + pubkey._id; var uri = app.config.cloudUrl + '/publickey/user/' + pubkey.userId + '/key/' + pubkey._id;
this.put(pubkey, uri, callback); self.put(pubkey, uri, callback);
}; };
/** /**
* Delete the public key from the cloud storage service * Delete the public key from the cloud storage service
*/ */
this.removePublicKey = function(keyId, callback) { self.removePublicKey = function(keyId, callback) {
var uri = app.config.cloudUrl + '/publickey/key/' + keyId; var uri = app.config.cloudUrl + '/publickey/key/' + keyId;
this.remove(uri, callback); self.remove(uri, callback);
}; };
// //
@ -179,9 +184,9 @@ app.dao.CloudStorage = function(window, $) {
/** /**
* Fetch private key by id * Fetch private key by id
*/ */
this.getPrivateKey = function(keyId, callback) { self.getPrivateKey = function(keyId, callback) {
var uri = app.config.cloudUrl + '/privatekey/key/' + keyId; var uri = app.config.cloudUrl + '/privatekey/key/' + keyId;
this.get(uri, function(err, key) { self.get(uri, function(err, key) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
@ -201,17 +206,18 @@ app.dao.CloudStorage = function(window, $) {
/** /**
* Persist encrypted private key to cloud service * Persist encrypted private key to cloud service
*/ */
this.putPrivateKey = function(privkey, callback) { self.putPrivateKey = function(privkey, callback) {
var uri = app.config.cloudUrl + '/privatekey/user/' + privkey.userId + '/key/' + privkey._id; var uri = app.config.cloudUrl + '/privatekey/user/' + privkey.userId + '/key/' + privkey._id;
this.put(privkey, uri, callback); self.put(privkey, uri, callback);
}; };
/** /**
* Delete the private key from the cloud storage service * Delete the private key from the cloud storage service
*/ */
this.removePrivateKey = function(keyId, callback) { self.removePrivateKey = function(keyId, callback) {
var uri = app.config.cloudUrl + '/privatekey/key/' + keyId; var uri = app.config.cloudUrl + '/privatekey/key/' + keyId;
this.remove(uri, callback); self.remove(uri, callback);
}; };
}; return self;
});