savePhoto : function(_args) { Cloud.Photos.create({ photo : _args.photo, acl_id : ACL_ID, 'photo_sync_sizes[]' : 'square_75', 'photo_sync_sizes[]' : 'small_240', 'photo_sync_sizes[]' : 'medium_500' }, function(e) { Cloud.onsendstream = Cloud.ondatastream = null; if (e.success) { var photo = e.photos[0]; Cloud.Objects.update({ classname : 'radiolistener', id : Ti.App.Properties.getString('RADIOLISTENERproduction'), fields : { photo_id : photo.id, photo_urls : (photo.urls) ? photo.urls : undefined } }, function(_e) { if (_e.success) { _args.done(); } else { console.log('Error:\n' + ((_e.error && _e.message) || JSON.stringify(_e))); } }); } }); },
create : function(_photo, _cb, _onSendStream, _onDataStream) { var params = { photo:_photo }; params['photo_sync_sizes[]'] = 'square_75'; Cloud.Photos.create(params, _cb ? _cb : Model.eventDefaultCallback); },
$.init = function() { Cloud.Photos.query({ page: 1, per_page: 1000, order: "-updated_at", where: "{\"user_id\":\"" + Alloy.Globals.user.id + "\"}" }, function(e) { if (e.success) { var data = []; for (var i = 0, l = e.photos.length; i < l; i++) { var photo = e.photos[i]; if (photo && !photo.processed) continue; var image = Ti.UI.createImageView({ image: photo.urls.medium_500, backgroundColor: "#484850", width: "75dp", height: "75dp", top: "2dp", bottom: "2dp", left: "2dp", right: "2dp", borderColor: "#fff", borderWidth: "1dp", accessibilityValue: i }); $.photosList.add(image); image.addEventListener("singletap", imageView); } } else alert("Error:\\n" + (e.error && e.message || JSON.stringify(e))); }); };
function findPhotos(userID) { Cloud.Photos.search({ user_id: userID }, function(e) { if (e.success) { if (e.photos.length == 0) { table.setData([{ title: 'No Results!' }]); } else { var data = []; for (var i = 0, l = e.photos.length; i < l; i++) { data.push(Ti.UI.createTableViewRow({ title: e.photos[i].filename, id: e.photos[i].id, color: 'black' })); } table.setData(data); } } else { Utils.error(e); } }); }
success: function(e){ // Ti.API.info(e.mediaType); if(e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO){ image = e.media; Ti.API.info(image); Cloud.Photos.create({ photo: image, name: 'profile' + uid, user_id: uid }, function(e){ if(e.success){ var photo = e.photos[0]; Ti.API.info('Success:\n' + 'id: ' + photo.id + '\n' + 'filename: ' + photo.filename + '\n' + 'size: ' + photo.size, 'updated_at: ' + photo.updated_at); }else{ Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); Ti.API.info("Code: "+e.code); } }); } },
button.addEventListener('click', function(evt) { if (!photo) { alert('Please provide a photo!'); return; } if (Ti.UI.createProgressBar) { Cloud.onsendstream = function(evt) { uploadProgress.value = evt.progress * 0.5; }; Cloud.ondatastream = function(evt) { uploadProgress.value = (evt.progress * 0.5) + 0.5; }; } Cloud.Photos.create({ photo: photo, collection_id: collectionID, 'photo_sync_sizes[]': 'small_240' }, function(e) { Cloud.onsendstream = Cloud.ondatastream = null; if (e.success) { photo = null; collectionID = null; alert('Uploaded!'); } else { Utils.error(e); } }); });
// searches for your profile picture and sets it function searchPhoto() { var uid; Cloud.Users.showMe(function(e){ if (e.success) { var user = e.users[0]; uid = user.id; } else { Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); } }); Cloud.Photos.search({ user_id: uid }, function (e) { if (e.success) { for (var i = 0; i < e.photos.length; i++) { var photo = e.photos[i]; /*Ti.API.info('id: ' + photo.id + '\n' + 'name: ' + photo.name + '\n' + 'filename: ' + photo.filename + '\n' + 'updated_at: ' + photo.updated_at + '\n' + 'url: ' + photo.urls.original + '\n' + 'userID: ' + photo.user_id);*/ filename = photo.urls.original; //filename = '/images/avatar.jpeg'; } Ti.API.info(filename); // $.picture.image = filename; var pPicture = $.UI.create('ImageView', { height: '100%', id: 'pPicture', image:filename, layout: 'vertical' }); $.profilePicture.add(pPicture); } else { Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); filename = '/images/avatar.jpeg'; var pImage = Ti.UI.createImageView({ width:"200px", height:"200px", image:filename }); $.profilePicture.add(pImage); } }); }
Cloud.Users.login(login_params, function(e){ var photo_params = { photo_id: photo_id }; Cloud.Photos.show(photo_params, function(elem){ if(elem.success){ self.add(Ti.UI.createImageView({image:elem.photos[0].urls.original})); }else{ alert("あれ?見つからないよ"); } }); });
$.submitBtn.on("click", function() { var previewBlob = Alloy.Globals.previewBlob; if (!previewBlob) return !1; var data = { photo: previewBlob }; data["photo_sync_sizes[]"] = "medium_500"; Cloud.Photos.create(data, function(e) { if (e.success) { $.imagePreview.animate({ opacity: 0, duration: 250 }, function() { $.imagePreview.visible = !1; }); Alloy.Globals.previewBlob = null; var photo = e.photos[0], newImage = Ti.UI.createImageView({ image: photo.urls.medium_500, backgroundColor: "#484850", width: "75dp", height: "75dp", top: "2dp", bottom: "2dp", left: "2dp", right: "2dp", borderColor: "#fff", borderWidth: "1dp", accessibilityValue: 0 }), photoList = $.photosList.children.slice(0); for (var i = 0; i < photoList.length; ++i) $.photosList.remove(photoList[i]); $.photosList.add(newImage); newImage.addEventListener("singletap", imageView); for (var j = 0; j < photoList.length; ++j) { var image = Ti.UI.createImageView({ image: photoList[j].image, backgroundColor: "#484850", width: "75dp", height: "75dp", top: "2dp", bottom: "2dp", left: "2dp", right: "2dp", borderColor: "#fff", borderWidth: "1dp", accessibilityValue: j + 1 }); $.photosList.add(image); image.addEventListener("singletap", imageView); } } else alert("Error:\\n" + (e.error && e.message || JSON.stringify(e))); }); });
Cloud.Users.login(login_params, function(e){ var params = {photo: image}; if(e.success){ Cloud.Photos.create(params, function(e){ if(e.success){ photo_id = e.photos[0].id; alert("アップロード成功"); }else{ alert("アップロードできませんでした"); } }); }else{alert('login failed');} });
button.addEventListener('click', function() { button.hide(); status.text = 'Removing, please wait...'; Cloud.Photos.remove({ photo_id: evt.id }, function(e) { if (e.success) { status.text = 'Removed!'; } else { status.text = '' + (e.error && e.message) || e; } }); });
function setBackground(tour) { Cloud.Photos.query({ where: { tags_array: tour.id } }, function (e) { if (e.success) { tour.background = e.photos[0].urls.original; tourStarter.addTour(tour); } else { alert('Error: ' + ((e.error && e.message) || JSON.stringify(e))); } }); }
exports.UploadImage = function(imagepath) { var Cloud = require('ti.cloud'); Cloud.Photos.create({ photo : Titanium.Filesystem.getFile(imagepath) }, function(e) { if (e.success) { var photo = e.photos[0]; alert('画像を投稿しました:\\n' + 'id: ' + photo.id + '\\n' + 'filename: ' + photo.filename + '\\n' + 'size: ' + photo.size, 'updated_at: ' + photo.updated_at); }else{ alert('画像の投稿に失敗しました') } }); }
var onSuccess = function(e){ if(e.media){ Cloud.Photos.create({ photo: e.media, collection_name: Ti.App.Properties.getString('collectionName') }, function (e) { if (e.success) { var photo = e.photo[0]; } else { alert('Error:\\n' + ((e.error && e.message) || JSON.stringify(e))); } _callback && _callback(e); }); } };
var cameraWork = function(image){ Cloud.Photos.create({ photo: image }, function (e) { if (e.success) { var photo = e.photos[0]; alert('Success:\n' + 'id: ' + photo.id + '\n' + 'filename: ' + photo.filename + '\n' + 'size: ' + photo.size, 'updated_at: ' + photo.updated_at); } else { alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); } }); };
exports.uploadPhoto = function(photo, locLat, locLong) { Cloud.Photos.create({ photo : photo, 'photo_sync_sizes[]' : 'medium_640' }, function(e) { if (e.success) { // null out our photo objects to clean up memory photo = null; collectionID = null; // Add an entry to the var savedPhotoId = e.photos[0].id; Cloud.Photos.show({ photo_id : savedPhotoId }, function(e1) { if (e1.success) { var uploadedPhoto = e1.photos[0]; Cloud.Objects.create({ classname : 'perspectives', fields : { photoId : savedPhotoId, url : uploadedPhoto.urls.medium_640, thumbsUp : 0, thumbsDown : 0, locLat : locLat, locLong : locLong } }, function(e2) { if (e2.success) { db.addLocation(e2.perspectives[0].id, savedPhotoId, uploadedPhoto.urls.medium_640, 0, 0, locLat, locLong, true); } else { alert('Error:\\n' + ((e2.error && e.message) || JSON.stringify(e2))); } }); } else { alert('Error:\\n' + ((e1.error && e1.message) || JSON.stringify(e1))); } }); } else { // oops, something went wrong } }); };
function getPhoto(_item, _callback) { if (!_item.photo || !_item.photo.id) return; if (_item.photo_url) _callback(_item) else {// proccessed ? Cloud.Photos.show({ photo_id : _item.photo.id }, function(_e) { if (_e.success && _e.photos) { _item.photo_url = _e.photos[0].urls; _callback(_item) } else { console.log('ERROR: '); } }); } }
}, function (e) { if (e.success) { var user = e.users[0]; Cloud.Photos.create({ photo: photo, 'photo_sync_sizes[]': 'small_240' }, function(e) { Cloud.onsendstream = Cloud.ondatastream = null; if (e.success) { // null out our photo objects to clean up memory photo = null; collectionID = null; alert('Photo successfully uploaded!'); } else { alert('An error occurred during upload.'); } }); } else { alert('There was an error during upload.'); } });
success : function(e) { alert(e); var photo; if (e.mediaType === Ti.Media.MEDIA_TYPE_PHOTO) { photo = e.media; Cloud.Photos.create({ custom_fields : { "filepatch" : path }, photo : photo }, function(e) { if (e.success) { alert("Data saved to the cloud"); } else { alert("Error:\n" + ((e.error && e.message ) || JSON.stringify(e))); } }); } else { alert("thought this was a photo, but it's" + e.mediaType); } },
function postPhoto(_args) { if (!_args.post.photo && _args.onsuccess && typeof (_args.onsuccess) == 'function') { _args.onsuccess(null); return; } console.log(_args.post.photo); console.log( typeof _args.post.photo); Cloud.Photos.create({ photo : _args.post.photo, acl_id : mensa_aclid }, function(e) { Cloud.onsendstream = Cloud.ondatastream = null; console.log('===create Photo======'); console.log(e); if (e.success) { if (_args.onsuccess && typeof (_args.onsuccess) == 'function') _args.onsuccess(e.photos[0]); } else { if (_args.onerror && typeof (_args.onerror) == 'function') _args.onerror(null); } }); };
module.exports = function(contentClass,isDownloaded) { logger.info('**** FETCH IMAGES '+JSON.stringify(contentClass)+' isDownloaded:'+isDownloaded); var deferred = jQ.Deferred(); var className = contentClass.className; var that = this; var dbName = 'presto'; var tableName = 'photos'; var fields = ['filename','size','processed','created_at','updated_at','content_type','md5']; var errorCount = 0; // which sizes to download var imageSizes = contentClass.imageSizes != null ? contentClass.imageSizes : ['medium_640']; var new_node = that.getContentNode(className); // Collect photo id for all collections that have a photo function getExternalPhotos(className) { var result = []; var posts = new Posts(); posts.fetch({ limit: null, // all where: 'tag = ?', // just this tag params: className }); posts.each(function(post) { result.push(post.get('id_photo')); }); var events = new Events(); events.fetch({ limit: 1000, // all where: 'tag = ?', params: className }); events.each(function(event) { result.push(event.get('id_photo')); }); // do not duplicate result = _.uniq(result); return result; } // init notify object var notify = { current: 0, steps: 1 }; //logger.info('Loading from online ....'); var externalIds = getExternalPhotos(className); logger.info('Also have external photos: '+JSON.stringify(externalIds)); // build the query for downloading the photo, also download pictures for other content class like posts var whereClause = null; if (externalIds.length !== 0) { whereClause = {'$or': [{tags_array: {'$in':[className]}},{id: {'$in':externalIds}}]}; } else { whereClause = {tags_array: {'$in':[className]}}; } //logger.info('Searching photos with the clause -> '+JSON.stringify(whereClause)); var md5_list = []; // if the content is downloaded and not builtin, then check also for images // already downloaded with md5 signature if (isDownloaded) { var photos = new Photos(); photos.fetch({ where: 'tag = ?', params: className, limit: 1000 }); photos.each(function (item) { md5_list.push(item.get('md5')); }); } Ti.API.info('MD5 HASH LIST: '+md5_list); // query for pictures Cloud.Photos.query({ where: whereClause, limit: 1000 },function(result) { // if any if (_.isArray(result.photos) != null && result.photos.length !== 0) { logger.info('Found photos: '+result.photos.length); // notify the deferred, number of steps now is the number of images to download n notify.current = 1; notify.steps = result.photos.length*imageSizes.length; deferred.notify(notify); // create the directory that._createDirectory(className+'/photos'); // delete all previous content //logger.info('** Deleting photos for '+className); db = Ti.Database.open(dbName); db.execute("BEGIN;"); db.execute('DELETE FROM '+tableName+' WHERE tag = ?',className); db.execute("COMMIT;"); db.close(); // store in memory new_node.photos = result.photos; // create a download stack var downloader = new Stackable(); downloader.context(that); //logger.info('photossss'+JSON.stringify(result.photos)) // enqueue all photos _(result.photos).each(function(photo) { var sqlite_photo = new Photo(); // standard fields sqlite_photo.guid = photo.id; sqlite_photo.set('id_user',photo.user_id); sqlite_photo.set('tag',className); if (photo.custom_fields != null) { // store custom fields as title sqlite_photo.set('title',photo.custom_fields.title); sqlite_photo.set('description',photo.custom_fields.description); // store the language sqlite_photo.set('language',photo.custom_fields.language); // store whole custom fields sqlite_photo.set('custom_fields',JSON.stringify(photo.custom_fields)); } // specific fields _(fields).each(function(field) { sqlite_photo.set(field,photo[field]); }); // cycle the formats and add a downloader _(imageSizes).each(function(size) { // some prep on file names var fileExtension = that.getFileExtension(photo.filename); var fileWithoutExtension = photo.filename.replace(fileExtension,''); fileWithoutExtension = fileWithoutExtension.replace(/[. ]/g,'_'); var relativeFilename = 'photos/'+fileWithoutExtension+'_'+size+fileExtension; // save on backbone sqlite_photo.set(size,relativeFilename); // add downloader only if md5 not changed if (_.contains(md5_list,photo.md5)) { Ti.API.info('Image: '+photo.id+' skipped, is not changed'); } else { // push the downloader downloader.push(function() { //logger.info('- downloading '+photo.urls[size]); var download = that.downloadPhotos( photo.urls[size], new_node.path+relativeFilename ); download.done(function() { notify.current += 1; deferred.notify(notify); }); return download; }); } }); // save try { sqlite_photo.save(); } catch(e) { Ti.API.error('Failed fetching image: '+photo.filename+' ('+photo.id+') -> duplicated'); logger.info('Sql error: '+JSON.stringify(e)); errorCount++; } }); // start download downloader.serial() .done(function() { //logger.info('OK, done downloading.'); if (errorCount === 0) { deferred.resolve(); } else { deferred.reject(); } }) .fail(function() { // sistemare errore con rollback deferred.reject(); }); } else { deferred.resolve(); } }); return deferred.promise(); };
Cloud.Friends.requests(function(e) { if (e.success) { var photo_url = null; if (e.friend_requests.length == 0) { var row = Ti.UI.createTableViewRow({ height : 60, title : "No new notifications" }); friendRequest.push(row); approvalTable.setData(friendRequest); Ti.App.fireEvent("set_badge"); } else { if (Ti.App.Properties.getString("total_friends") == 0 || Ti.App.Properties.getString("total_friends") == null) { var row = Ti.UI.createTableViewRow({ height : 60, title : "No new notifications" }); friendRequest=[]; friendRequest.push(row); approvalTable.setData(friendRequest); } else { total_friends = e.friend_requests.length; for (var i = 0; i < e.friend_requests.length; i++) { Ti.API.info("e.friend_requestse=" + JSON.stringify(e.friend_requests)); var user = e.friend_requests[i].user; // var touchEnabled = false; var touchEnabled = true; if (friend_status(user.id) == true) { obj = campus_taps_friends[global_index].friendship; already_friend_index = (obj.friend_id == user.id) ? obj.id : 0; var row = Ti.UI.createTableViewRow({ height : 60, id : (i + 1), user_id : user.id, selectedBackgroundColor:"white" }); friendRequest.push(row); var user_image_view = Titanium.UI.createImageView({ defaultImage : '/images/profilePlaceholder.png', image : photo_url, width : 40, height : 40, left : 10, layout : "horizontal" }); Cloud.Photos.search({ user_id : user.id }, function(e) { if (e.success) { if (e.photos.length > 0) { var photo = e.photos[0]; photo_url = photo.urls.square_75; } else { photo_url = "/images/profilePlaceholder.png"; } user_image_view.image = photo_url; row.add(user_image_view); } else { user_image_view.image = "/images/profilePlaceholder.png"; row.add(user_image_view); } }); var name = Ti.UI.createLabel({ text : user.first_name + " " + user.last_name, width : 175, height : 20, font : { fontSize : 15 }, left : 60 }); row.add(name); var reject = Ti.UI.createButton({ backgroundImage : '/images/notification/friendNotificationReject.png', height : 30, width : 30, right : 50, name : "reject", zIndex : 10, friend_id : user.id, already_friend_index : already_friend_index, touchEnabled : touchEnabled }); row.add(reject); var approve = Ti.UI.createButton({ backgroundImage : '/images/notification/friendNotificationAccept.png', height : 30, width : 30, right : 10, name : "approve", zIndex : 10, friend_id : user.id, already_friend_index : already_friend_index }); row.add(approve); } else { touchEnabled = false; } } approvalTable.addEventListener("click", function(e) { var index = e.index; var badgeCount = Ti.UI.iPhone.getAppBadge(); badgeCount = badgeCount - 1; Ti.UI.iPhone.setAppBadge(badgeCount); Ti.API.info("e.source.name=",e.source.name); switch(e.source.name) { case "approve": var friend_id = e.source.friend_id; current_row_index = index; var id = e.source.already_friend_index; if (friend_id) { Cloud.Friends.approve({ user_ids : friend_id }, function(e) { if (e.success) { if (total_friends > 0) { Ti.App.fireEvent("set_badge"); } var user_id = Ti.App.Properties.getString('currentUser_id'); var url = null; url = "http://campustaps.com/friendships/update?user_id=" + user_id + "&friend_id=" + friend_id + "&status=Friend"; Ti.API.info("IRLLL=" + url); xhr_operations(url, 'PUT', approve_remote_friend_callback, approve_remote_friend_errorback); Ti.API.info("url=" + url); } else { Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); } }); } break; /// case "reject": var friend_id = e.source.friend_id; var id = e.source.already_friend_index; var user_id = Ti.App.Properties.getString('currentUser_id'); current_row_index = index; if (friend_id) { Cloud.Friends.remove({ user_ids : friend_id }, function(e) { if (e.success) { // Ti.App.fireEvent("set_badge"); var url = null; url = "http://campustaps.com/friendships/update?user_id=" + user_id + "&friend_id=" + friend_id + "&status=Rejected"; Ti.API.info("update url=" + url); xhr_operations(url, 'PUT', delete_remote_friend_callback, delete_remote_friend_errorback); } else { alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); } }); } break; // }; }); approvalTable.setData(friendRequest); } } } else { Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); } });
exports.loadPhotoId = function(savedPhotoId, photoLoadCallback) { Cloud.Photos.show({ photo_id : savedPhotoId }, photoLoadCallback); };
remove : function(_photo_id, _cb) { Cloud.Photos.remove({ photo_id : _photo_id }, _cb ? _cb : Model.eventDefaultCallback); }