Skip to content

Commit

Permalink
camera upload worksgit add .
Browse files Browse the repository at this point in the history
  • Loading branch information
joetime committed Aug 25, 2016
1 parent 7485b88 commit 398fc9e
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 32 deletions.
31 changes: 16 additions & 15 deletions www/js/backand.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,32 @@ angular.module('starter').config(function(BackandProvider) {
}
})

.service('$fileUploadService', function() {
.service('$fileUploadService', function($logService, $errorService, Backand) {

var actionUrl = "https://api.backand.com/1/objects/action/Test/?name=file"
var log = $logService.log;
log('$fileUploadService init');
var actionUrl = "https://api.backand.com/1/objects/action/Test/1"

function upload(filename, filedata) {
log('$fileUploadService upload filename:', filename, true);
log('$fileUploadService upload filedata:', filedata, true);

// By calling the files action with POST method in will perform
// an upload of the file into Backand Storage

log('upload filename', filename)
log('upload filedata', filedata);

return $http({
method: 'POST',
url: actionUrl, //Backand.getApiUrl() + baseActionUrl + objectName,
url: actionUrl, //Backand.getApiUrl() + '/1/objects/action/Test/1',
params: {
"name": filesActionName
name: 'files',
parameters: {
filename: filename,
filedata: 'x'
}
},
headers: {
'Content-Type': 'application/json'
config: {
ignoreError: false
},
// you need to provide the file name and the file data
data: {
"filename": filename,
"filedata": filedata.substr(filedata.indexOf(',') + 1, filedata.length) //need to remove the file prefix type
}
data: {}
});
};

Expand Down
107 changes: 97 additions & 10 deletions www/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ angular.module('starter.controllers', [])
})

.controller("SystemTestsCtrl",
function($scope, $acgoSettings, $cordovaCamera, $cordovaGeolocation, $dataService, $logService, $fileUploadService, $errorService, $http) {
function($scope, $acgoSettings, $cordovaCamera, $cordovaGeolocation, $dataService, $logService, $fileUploadService, $errorService, $http, Backand) {

var log = $logService.log;
log('SystemTestsCtrl init');
Expand All @@ -54,28 +54,113 @@ angular.module('starter.controllers', [])
$scope.takePicture = function() {

var options = $acgoSettings.camera();
log('camera options', options, true);

$cordovaCamera.getPicture(options).then(function(imageData) {
$scope.imgURI = "data:image/jpeg;base64," + imageData;
$cordovaCamera.getPicture(options).then(function(imageDataURL) {

if (confirm('test upload?')) {
// show picture in UI
$scope.imgURI = "data:image/jpeg;base64," + imageDataURL;

$fileUploadService.doUpload(
'testimage.jpg', "data:image/jpeg;base64," + imageData
).then(function(d) {
log('upload success', d)
if (imageDataURL && confirm('test upload?')) {

log('attempting upload', imageDataURL, true)

$http({
method: 'POST',
url: Backand.getApiUrl() + '/1/objects/action/Test/1',
params: {
name: 'files',
parameters: {
filename: 'test.jpg',
}
},
config: {
ignoreError: true
},
data: {
filedata: imageDataURL
}
}).then(function(d) {
log('camera upload success', d, true);
$scope.uploadResult = d;
$scope.uploading = false;
},
function(err) {
log('upload err', d)
log('camera upload error', err, true);
$scope.uploadResult = err;
$scope.uploading = false;
});

}

}, function(err) {
log('error getting picture', err, true);
// An error occured. Show a message to the user
});
}


// Test upload files
$scope.uploadTest = function() {
log('uploading test...');
$scope.uploading = true;

$http({
method: 'POST',
url: Backand.getApiUrl() + '/1/objects/action/Test/1',
params: {
name: 'files',
parameters: {
filename: 'test.txt',
filedata: 'abcd'
}
},
config: {
ignoreError: true
},
data: {}
}).then(function(d) {
log('upload success', d, true);
$scope.uploadResult = d;
$scope.uploading = false;
},
function(err) {
log('upload error', err, true);
$scope.uploadResult = err;
$scope.uploading = false;
});
}

$scope.downloadTest = function() {

log('uploading test...');
$scope.downloading = true;

$http({
method: 'GET',
url: Backand.getApiUrl() + '/1/objects/action/Test/1',
params: {
name: 'files',
parameters: {
filename: 'test.txt',
//filedata: 'abcd'
}
},
config: {
ignoreError: true
},
data: {}
}).then(function(d) {
log('download success', d, true);
$scope.uploadResult = d;
$scope.downloading = false;
},
function(err) {
log('download error', err, true);
$scope.uploadResult = err;
$scope.downloading = false;
});
};

// Access Geolocation
$scope.getLocation = function() {

Expand All @@ -95,6 +180,7 @@ angular.module('starter.controllers', [])
});
}

// Test connection to Backand
$scope.getData = function() {

$scope.gettingData = true;
Expand All @@ -110,6 +196,7 @@ angular.module('starter.controllers', [])
});
}

// Test logging errors
$scope.logError = function() {
$scope.loggingError = true;
$errorService.test().then(
Expand Down
16 changes: 12 additions & 4 deletions www/js/svc-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ angular.module('starter')

isTest = isTest || false;

var timeStamp = new Date();
var exString = 'JSON:' + JSON.stringify(ex);

var record = {
//"timeStamp": timeStamp,
"exception": ex,
"exception": exString,
"url": url,
"cause": cause,
"isTest": isTest
}
};

//log('logging error: ', record);
return $http.post(endpointUrl, record);
}

logOther = function(msg, obj) {
obj = JSON.stringify(obj);
var record = {
"exception": obj,
"url": msg
};
return $http.post(endpointUrl, record);
}

Expand All @@ -28,6 +35,7 @@ angular.module('starter')

return {
log: logError,
logOther: logOther,
test: test
}
});
9 changes: 7 additions & 2 deletions www/js/svc-log.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
angular.module('starter').service('$logService', function() {
angular.module('starter').service('$logService', function($errorService) {

var logs = [];
var listeners = [];
Expand All @@ -20,7 +20,7 @@ angular.module('starter').service('$logService', function() {
};

// add log to the list, then call all the listeners
function log(msg, obj) {
function log(msg, obj, server) {
if (obj)
console.log(msg, obj);
else
Expand All @@ -35,6 +35,11 @@ angular.module('starter').service('$logService', function() {
angular.forEach(listeners, function(listener) {
listener(logs);
});

// try logging to server
if (server) {
$errorService.logOther(msg, obj);
}
}

// add a function to the listeners list
Expand Down
2 changes: 2 additions & 0 deletions www/js/svc-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ angular.module('starter').service('$acgoSettings', ['$cordovaCamera', '$logServi
try {
var c = Camera; // will fail if no Camera is defined

log('setting additional camera defaults');
defaults.camera.destinationType = c.DestinationType.DATA_URL;
//defaults.camera.destinationType = c.DestinationType.FILE_URI;
defaults.camera.sourceType = c.PictureSourceType.CAMERA;
defaults.camera.encodingType = c.EncodingType.JPEG;
defaults.camera.popoverOptions = CameraPopoverOptions;
Expand Down
2 changes: 1 addition & 1 deletion www/templates/tab-logs.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<i ng-show="log.object!=undefined && expand" class="ion-arrow-down-b"></i>
<b>{{log.msg}}</b>
<br />
<span ng-show="expand">{{log.object}}</span>
<span ng-show="expand" style="font-size:7px">{{log.object}}</span>
</ion-item>
</ion-list>

Expand Down
15 changes: 15 additions & 0 deletions www/templates/tab-systemtests.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
<img ng-show="imgURI !== undefined" ng-src="{{imgURI}}" />
<img ng-show="imgURI === undefined" ng-src="http://placehold.it/400x300" />
</ion-item>
<!-- Upload test -->
<ion-item class="item-divider">Upload file</ion-item>
<ion-item>

<button type="button" class="button button-positive" ng-click="uploadTest()">Upload</button>
<br />
<span ng-show="uploading">one moment...</span> {{uploadResult}}
</ion-item>
<!-- Download test -->
<ion-item class="item-divider">Download file</ion-item>
<ion-item>
<button type="button" class="button button-positive" ng-click="downloadTest()">Download</button>
<br />
<span ng-show="downloading">one moment...</span> {{downloadResult}}
</ion-item>
<!-- Geolocation -->
<ion-item class="item-divider">Geolocation</ion-item>
<ion-item>
Expand Down

0 comments on commit 398fc9e

Please sign in to comment.