def create
      @file = @data.is_a?(String) ? File.open(@data) : @data[:tempfile]

      # Return error if no file
      return (@status = {:error => 'upload_file_not_found'}) unless @file

      @name = filename(@data.is_a?(String) ? @data : @data[:filename])

      # Check if it's more than max or return error
      return (@status = {:error => 'upload_file_size_is_too_big'}) if @max and @file.size > @max

      # Resize file and create thumbnail for image
      if @name =~ IMAGE_REGEX
        resize! if @width or @height
        thumbnail!
      end

      # Store timestamp and type
      @timestamp = @data[:timestamp].nil? ? Time.now.to_i.to_s : @data[:timestamp].to_s
      @type = @data[:type]

      begin
        # Upload file
        @service.directories.new(:key => @container).files.create(
          :content_type => @type,
          :key => (@timestamp ? "#{@timestamp}_#{@name}" : @name),
          :body => @file
        )

        # Upload thumbnail
        if @thumb
          @service.directories.new(:key => @container).files.create(
            :content_type => @type,
            :key => "#{@timestamp}_#{@thumb}",
            :body => File.open("/tmp/#{@thumb}")
          )
          # Delete thumb file
          File.delete("/tmp/#{@thumb}")
        end
      rescue => x
        # Can't connect, report exception.
        @status = {:error => 'upload_cant_connect_to_cdn'}

      else
        thumb_url = @thumb ? "#{@cdn}/#{@timestamp}_#{@thumb}" : nil
        @status = {:url => "#{@cdn}/#{@timestamp}_#{@name}", :thumb_url => thumb_url, :size => @file.size, :mimetype => @type}
        @status
      end
    end
def create
      @file = @data.is_a?(String) ? File.open(@data) : @data[:tempfile]

      # Return error if no file
      return (@status = {:error => 'upload_file_not_found'}) unless @file

      @name = filename(@data.is_a?(String) ? @data : @data[:filename])

      # Check if it's more than max or return error
      return (@status = {:error => 'upload_file_size_is_too_big'}) if @max and @file.size > @max

      # Resize file and create thumbnail for image
      if @name =~ IMAGE_REGEX
        resize! if @width or @height
        thumbnail!
      end

      # Store timestamp and type
      @timestamp = @data[:timestamp].nil? ? Time.now.to_i.to_s : @data[:timestamp].to_s
      @type = @data[:type]

      begin
        # Upload file
        @service.directories.new(:key => @container).files.create(
          :content_type => @type,
          :key => (@timestamp ? "#{@timestamp}_#{@name}" : @name),
          :body => @file
        )

        # Upload thumbnail
        if @thumb
          @service.directories.new(:key => @container).files.create(
            :content_type => @type,
            :key => "#{@timestamp}_#{@thumb}",
            :body => File.open("/tmp/#{@thumb}")
          )
          # Delete thumb file
          File.delete("/tmp/#{@thumb}")
        end
      rescue => x
        # Can't connect, report exception.
        @status = {:error => 'upload_cant_connect_to_cdn'}

      else
        thumb_url = @thumb ? "#{@cdn}/#{@timestamp}_#{@thumb}" : nil
        @status = {:url => "#{@cdn}/#{@timestamp}_#{@name}", :thumb_url => thumb_url, :size => @file.size, :mimetype => @type}
        @status
      end
    end
Create upload