baas.io Javascript SDK v0.9.0 src/file/file.js

File

method
Baas.File()

    Description

    A class to model a Baas File.
    Set the path in the options object.

    @constructor
    @param {object} options {client:client, data: {'key': 'value'}}

    Source

    Baas.File = function(options) {
    
    		if(options){
    			this._client = options.client;
    			this._data = options.data || {};
    			this._data.type = "files";
    			this._data.minsize = options.minsize || 1;
    			this._data.maxsize = options.maxsize || 10485760;
    			this._data.allowExts = options.allowExts || "";
    			this.count = 0;
    		}
    	}

      Description

      Inherit from Baas.Entity.
      Note: This only accounts for data on the file object itself.\

      Source

      Baas.File.prototype = new Baas.Entity();

      upload

      method
      Baas.File.prototype.upload()

        Description

        Upload files and Create file Entity

        @method save
        @public
        @param {object} fileObj
        @param {function} callback
        @return {callback} callback(err, data, entity)

        Source

        Baas.File.prototype.upload = function(fileObj, callback){
        
        		var type = this.get('type');
        
        		var fileNameRegex = /<|>|"|\\/;
        
        		var self = this;
        
        		var formData = new FormData();
        
        		var fileSize = fileObj.file[0].files[self.count].size;
        		var fileName = fileObj.file[0].files[self.count].name;
        
        		// fileSize Check
        		if(fileSize < this._data.minsize  || fileSize > this._data.maxsize){
        			callback(true, {'error_code':100, 'error_description' : 'min : 1byte, max : 10MB'});
        			return
        		} else if(fileNameRegex.test(fileName)){
        			callback(true, {'error_code':100, 'error_description' : 'Invalid file name'});
        			return 
        		}
        
        		//file ext check
        		if(this.get('allowExts')){
        			var allowExts = (this.get('allowExts')).toLowerCase().split(",");
        
        			if(fileName.length > 0){
        				var lidx = fileName.lastIndexOf(".");
        				var fext = fileName.substring(lidx+1).toLowerCase();  
        			}
        			
        			var checkCount = 0;
        
        			if(fext && fext.length > 0){
        				for(var i=0;i<allowExts.length;i++){
        					var ext = allowExts[i].toLowerCase();
        					if(fext == ext){
        						checkCount+=1;
        					}
        				}
        			}
        
        			if(checkCount === 0) {
        				callback(true, {'error_code':100, 'error_description' : 'Invalid file ext'});
        				return;
        			}
        		}
        
        		formData.append("file" , fileObj.file[0].files[self.count]);    
        
        		var options =  {
        			method:'POST',
        			endpoint:type,
        			body:formData
        		};
        
        		this._client.request(options, function(err, retdata){
        			if(err) {
        				if(self._client.logging) {
        					console.log('error getting push');
        				}
        				if(typeof(callback) === 'function') {
        					callback(err, retdata);
        				}
        			} else {
        				if (retdata.entities) {
        					if (retdata.entities.length) {
        						var entity = retdata.entities[0];
        						self.set(entity);
        					}
        				}
        				callback(err, retdata, self);
        			}
        		});
        	}

        download

        method
        Baas.File.prototype.download()

          Description

          download files

          @param {function} callback
          @return {callback} callback(err, data, entity)

          Source

          Baas.File.prototype.download = function(callback){
          
          		var type = 'files/' + this.get('uuid') + '/data';
          
          		var self = this;
          
          		var options =  {
          			method:'GET',
          			endpoint:type
          		};
          
          		this._client.request(options, function (err, retdata){
          			if(err) {
          				if(self._client.logging) {
          					console.log('error getting push');
          				}
          				if(typeof(callback) === 'function') {
          					callback(err, self);
          				}
          			} else {
          				callback(err, self);
          				window.location = self.getDownloadURL();
          			}
          		});
          	}

          save

          method
          Baas.Entity.prototype.save()

            Description

            Saves the entity back to the database

            @method save
            @public
            @param {function} callback
            @return {callback} callback(err, data)

            Source

            Baas.Entity.prototype.save = function (callback) {
            		//TODO:  API will be changed soon to accomodate PUTs via name which create new entities
            		//       This function should be changed to PUT only at that time, and updated to use
            		//       either uuid or name
            		var type = this.get('type');
            		var method = 'POST';
            		if (Baas.Utils.isUUID(this.get('uuid'))) {
            			method = 'PUT';
            			type += '/' + this.get('uuid');
            		}
            
            		//update the entity
            		var self = this;
            		var data = {};
            		var entityData = this.get();
            		//remove system specific properties
            		for (var item in entityData) {
            			if (item === 'metadata' || item === 'created' || item === 'modified' ||
            					item === 'type' || item === 'activated' || item ==='uuid' || item ==='minsize' || item ==='maxsize') { continue; }
            			data[item] = entityData[item];
            		}
            		var options =  {
            			method:method,
            			endpoint:type,
            			body:data
            		};
            		//save the entity first
            		this._client.request(options, function (err, retdata) {
            			if (err && self._client.logging) {
            				console.log('could not save entity');
            				if (typeof(callback) === 'function') {
            					return callback(err, retdata, self);
            				}
            			} else {
            				if (retdata.entities) {
            					if (retdata.entities.length) {
            						var entity = retdata.entities[0];
            						self.set(entity);
            					}
            				}
            				callback(err, retdata, self);
            			}
            		});
            	}

            fetch

            method
            Baas.File.prototype.fetch()

              Description

              refreshes the entity by making a GET call back to the database

              @method fetch
              @public
              @param {function} callback
              @return {callback} callback(err, data)

              Source

              Baas.File.prototype.fetch = function (callback) {
              		var type = this.get('type');
              		var self = this;
              
              		//if a uuid is available, use that, otherwise, use the name
              		if (this.get('uuid')) {
              			type += '/' + this.get('uuid');
              		} else {
              			if (typeof(callback) === 'function') {
              				var error = 'no_uuid_specified';
              				if (self._client.logging) {
              					console.log(error);
              				}
              				return callback(true, {error:error}, self)
              			}
              		}
              
              		var options = {
              			method:'GET',
              			endpoint:type
              		};
              
              		this._client.request(options, function (err, data) {
              			if (err && self._client.logging) {
              				console.log('could not get entity');
              			} else {
              					if (data.entities.length) {
              						var entity = data.entities[0];
              						self.set(entity);
              					}
              			}
              			if (typeof(callback) === 'function') {
              				callback(err, data, self);
              			}
              		});
              	}

              getDownloadURL

              method
              Baas.File.prototype.getDownloadURL()
              • @method: getDownloadURL
              • @return: {string}url

              Description

              html markup use file download url

              Source

              Baas.File.prototype.getDownloadURL = function(){
              		return this._client.URI + '/' + this._client.orgName + '/' + this._client.appName + '/' + this.get('type') + '/' + this.get('uuid') + '/data'
              	}