Skip to content

Commit

Permalink
CollectionFS
Browse files Browse the repository at this point in the history
  • Loading branch information
Théo mathieu committed Mar 20, 2016
1 parent 44aa8b3 commit a5bae25
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
8 changes: 6 additions & 2 deletions src/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
74 changes: 74 additions & 0 deletions src/FSCollection.js
Original file line number Diff line number Diff line change
@@ -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
};
}
2 changes: 2 additions & 0 deletions src/Meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,6 +22,7 @@ module.exports = {
Accounts: Accounts,
MeteorListView: ListView,
collection: collection,
FSCollection: FSCollection,
getData() {
return Data
},
Expand Down
6 changes: 5 additions & 1 deletion src/user/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
},
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit a5bae25

Please sign in to comment.