diff --git a/package.json b/package.json index e0db945..9273e52 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-meteor", - "version": "1.0.0-beta14", + "version": "1.0.0-beta15", "description": "DDP React-native Client", "main": "src/Meteor.js", "scripts": { diff --git a/src/Data.js b/src/Data.js index e106bfa..a46fe2e 100644 --- a/src/Data.js +++ b/src/Data.js @@ -2,14 +2,18 @@ import minimongo from 'minimongo-cache'; process.nextTick = setImmediate; export default { - endpoint: null, - options: null, + _endpoint: null, + _options: null, ddp: null, subscriptions: {}, db: new minimongo(), calls: [], hasBeenConnected: false, + getUrl() { + return this._endpoint.substring(0, this._endpoint.indexOf('/websocket')); + }, + _cbs: [], onChange(cb) { this.db.on('change', cb); diff --git a/src/FSCollection.js b/src/FSCollection.js new file mode 100644 index 0000000..208cb81 --- /dev/null +++ b/src/FSCollection.js @@ -0,0 +1,74 @@ +import Collection from './Collection'; +import Data from './Data'; + +export default function(name) { + + const Meteor = this; + + const collectionName = 'cfs.'+name+'.filerecord'; + const setProperties = file => { + const getStoreName = (params = {store: name}) => { + return params.store; + }; + const getImageInfos = params => { + if(!params || !params.store) return file.original || {}; + return file.copies[params.store] || {}; + }; + const getType = params => { + return getImageInfos(params).type; + }; + return { + ...file, + url: params => { + return Data.getUrl()+'/cfs/files/'+name+'/'+file._id+'?store='+getStoreName(params)+(Meteor._tokenIdSaved ? '&token='+btoa(JSON.stringify({authToken: Meteor._tokenIdSaved})) : ""); + }, + isImage: params => { + const type = getType(params); + return type && type.indexOf('image/')===0; + }, + isAudio: params => { + const type = getType(params); + return type && type.indexOf('audio/')===0; + }, + isVideo: params => { + const type = getType(params); + return type && type.indexOf('video/')===0; + }, + isUploaded: params => { + return !!(getImageInfos(params).updatedAt); + }, + name: params => { + return getImageInfos(params).name; + }, + extension: params => { + const imageName = getImageInfos(params).name; + if(!imageName) return; + return imageName.substring(imageName.lastIndexOf('.')+1); + }, + size: params => { + return getImageInfos(params).size; + }, + type: getType, + updatedAt: params=>{ + return getImageInfos(params).updatedAt; + } + } + } + + return { + find(selector, options) { + const elems = Collection(collectionName).find(selector, options); + return elems.map(elem=>{ + return setProperties(elem); + }); + }, + findOne(selector, options) { + const elem = Collection(collectionName).findOne(selector, options); + return elem && setProperties(elem); + }, + insert: Collection(collectionName).insert, + update: Collection(collectionName).update, + upsert: Collection(collectionName).upsert, + remove: Collection(collectionName).remove + }; +} \ No newline at end of file diff --git a/src/Meteor.js b/src/Meteor.js index 0446ffe..64e9e4e 100644 --- a/src/Meteor.js +++ b/src/Meteor.js @@ -9,6 +9,7 @@ import Random from '../lib/Random'; import Data from './Data'; import collection from './Collection'; +import FSCollection from './FSCollection'; import call from './Call'; import Mixin from './components/Mixin'; @@ -21,6 +22,7 @@ module.exports = { Accounts: Accounts, MeteorListView: ListView, collection: collection, + FSCollection: FSCollection, getData() { return Data }, diff --git a/src/user/User.js b/src/user/User.js index c2796e5..60858de 100644 --- a/src/user/User.js +++ b/src/user/User.js @@ -18,8 +18,10 @@ module.exports = { return this._isLoggingIn; }, logout(callback) { - this.call("logout", function(err) { + this.call("logout", err => { AsyncStorage.removeItem(TOKEN_KEY); + this._tokenIdSaved = null; + this._userIdSaved = null; typeof callback == 'function' && callback(err); }); }, @@ -54,12 +56,14 @@ module.exports = { _handleLoginCallback(err, result) { if(!err) {//save user id and token AsyncStorage.setItem(TOKEN_KEY, result.token); + this._tokenIdSaved = result.token; this._userIdSaved = result.id; } }, async _loadInitialUser() { try { var value = await AsyncStorage.getItem(TOKEN_KEY); + this._tokenIdSaved = value; if (value !== null){ this._startLoggingIn(); this.call('login', { resume: value }, (err, result) => {