Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
j_platform / app / assets / javascripts / j_platform / uploader.js.coffee
Size: Mime:
class UploaderFactory
  @IMAGE_EXTENSIONS = "jpg,jpeg,gif,png,bmp"

  @getShortURL = (url) ->
    @_platformHost ||= $('meta[name=platform-host]').attr("content")
    result = url
    $.ajax {
      type: "POST",
      async: false,
      url: "#{@_platformHost}/api/v1/urls/shorten",
      data: {'url': url},
      dataType: 'text',
      success: (data) ->
        unless data == 'false'
          result = data
    }
    result

  @buildFromDiv = ($uploaderDiv, $textarea) ->
    # if already initialized, return existing uploader
    $containerEl = $uploaderDiv.parent('.uploader-container')
    if $uploaderDiv.hasClass('uploader-ready')
      return $containerEl.data('uploader')

    fileSize = "100mb"
    S3Data = jugnooAppContext.S3Data
    s3Url = "https://#{S3Data['bucket']}.s3.amazonaws.com/"

    uploader = new plupload.Uploader {
      runtimes : 'html5,flash,html4',
      browse_button : $uploaderDiv.attr('id'),
      browse_button_hover: "hover-icon",
      browse_button_active: "active-icon",
      container : $containerEl.attr('id'),
      max_file_size : fileSize,
      url : s3Url,
      multi_selection: false,
      flash_swf_url : '/assets/j_platform/plupload.flash.swf',
      silverlight_xap_url : '/assets/j_platform/plupload.silverlight.xap',
      preinit : {
        UploadFile: (up, file) ->
          contentType = "binary/octet-stream"

          # Set content-type as image if file extension indicates same
          # Not foolproof, but not really different than what we were doing previously
          # when using separate image uploader
          fileNameTokens = file.name.split('.')
          if fileNameTokens[fileNameTokens.length - 1] in UploaderFactory.IMAGE_EXTENSIONS.split(',')
            contentType = "image"

          up.settings.multipart_params = {
            key: "#{S3Data['project_id']}/#{file.name}",
            filename: file.name,
            AWSAccessKeyId: S3Data['key'],
            acl: 'public-read',
            policy: S3Data['policy'],
            signature: S3Data['signature'],
            success_action_status: '201',
            'Content-Type': contentType
          }
      },
      filters : [
        {title : "Image files", extensions : UploaderFactory.IMAGE_EXTENSIONS},
        {title : "Audio files", extensions : "asnd,aac,m4a,aif,aiff,mp3,mpeg,mpg,mpa,mpe,avi,wav,wmv,wma"},
        {title : "Video files", extensions : "wmv,3gp,avi,mov,mp4,mpeg,mpg,flv,swf,mkv"},
        {title : "Text document files", extensions : "pdf,ps,doc,docx,ppt,pps,pptx,xls,xlsx,odt,fodt,sxw,odp,fodp,sxi,ods,fods,sxc,txt,rtf"}
      ]
    }
    uploader.init()

    uploader.bind 'FilesAdded', (up, files) ->
      up.start()

    uploader.bind 'UploadProgress', (up, file) ->
      showFileUploadIndicator()
      $("#upload-progressbar .bar").progressbar
        value: file.percent

    uploader.bind 'FileUploaded', (up, file, response) ->
      # Flash-based uploads do not give us a status code
      if response.status == undefined || response.status == 200 || response.status == 201
        if response.response.search("<error-code>8003") != -1
          alert("The file name is too long. Please remame it and try again.");
          return false;
        else
          oldVal = $textarea.val()
          s3BaseUrl = $('meta[name="s3-base-url"]').attr('content')
          oldUrl = $($.parseXML(response.response)).find('Location').text().replace("%2F", "/").replace(s3Url, s3BaseUrl)
          url = UploaderFactory.getShortURL(oldUrl)

          $uploaderDiv.trigger('attach:successful', {url: {full: oldUrl, min: url}, type: up.settings.multipart_params['Content-Type']})
          $textarea.val("#{oldVal} #{url}")
          $uploaderDiv.trigger('uploader:done')
          completedFileUploadIndicator()
          hideFileUploadIndicator()

      up.refresh();

    uploader.bind 'Error', (up, error) ->
      if(error.code == plupload.FILE_SIZE_ERROR)
        alert("The file you attempted to upload is too large. The maximum size for this file type is: " + fileSize.toUpperCase())
      else
        alert("An error occurred while trying to upload the file: " + error.message)

    # Workaround for IE9 issues if Flash not available
    $html4InputEl = $containerEl.find('form').find('input').first()
    if($html4InputEl.length > 0)
      $uploaderDiv.click ->
        $html4InputEl.click();

    $containerEl.data('uploader', uploader)

    $uploaderDiv.addClass('uploader-ready')

    return uploader

window.UploaderFactory = UploaderFactory