暂停/恢复使用HTML5和php上传到S3存储桶的功能

时间:2015-07-30 20:55:21

标签: php html5 file-upload amazon-s3 resume-upload

我需要帮助在文件上传器中实现暂停/恢复功能。目前,上传者按照预期工作,不受互联网条件恶劣的影响。我们遇到过将用户的文件上传到我们的上传存储桶已损坏和损坏的情况。缓解此问题的一种方法是将此功能实现到上传器。我们目前的分段上传部分

instructorUpload.js

var eventedMongoose = require('../models/EventModel')(mongoose);

var Events = {
    api: true,
    path: '/api/events',
    model: eventedMongoose,
    controller: require('../controllers/EventController')(eventedMongoose)
};

FileUpload.php

/**
 * Begins AWS S3 Multipart Upload for object creation in S3
 *
 * @param {Object} i   - File index
 * @param {Number} key - Unique key for file upload
 */
function createMultipartUpload(i, key){
    var formdata = new FormData();
    formdata.append("command"  , 'CreateMultipartUpload');
    formdata.append("fileindex", i                      );
    formdata.append("key"      , key                    );
    var xhr = request(false, "/api/UploadFile_v0/FileUploader.php", new   XMLHttpRequest(), formdata);
    xhr.onreadystatechange = function() {
       if (xhr.readyState === 4)
           uploadPart(JSON.parse(xhr.responseText), 1);
    };
  }
  /**
   * Upload indivdual part for AWS S3 Multipart Upload for object creation in S3
   *
   * @param {Object} sendBackData - Parsed JSON information receive after POST is complete
   * @param {Number} partNum    - File chunk index
   *
   * @returns
   */
  function uploadPart(data, partNum){
    var fileIndex = data['fileindex'];
    var file    = files[fileIndex];
    if (partNum > Math.ceil(file.size / chunkSize)) {
      completeMultipartUpload(data);
      return;
    }
    var blobs = chunkFile(file, partNum);
    var formdata = new FormData();
    for(var i = 0; i < blobs.length; i++)
      formdata.append("file[]", blobs[i]);
    formdata.append("command", 'UploadPart');
    formdata.append("uploadId", data['uploadId']);
    formdata.append("key", data['key']);
    formdata.append("partNumber", partNum);
    var xhr = request(true, "/api/UploadFile_v0/FileUploader.php", new XMLHttpRequest(), formdata);
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4)
        uploadPart(data, partNum + 1);
    };
  }
  /**
   * Completes Multipart Upload
   *
   * @param {Object} data - Parsed JSON information receive after POST is complete
   */
  function completeMultipartUpload(data) {
    var fileIndex = data['fileindex'];
    var file = files[fileIndex];
    var type = file.type.split("/")[0];
    var formdata = new FormData();
    var key = data['key'];
    formdata.append("command" , 'CompleteMultipartUpload');
    formdata.append("uploadId", data['uploadId']);
    formdata.append("key", key);  
    var xhr = request(false, "/api/UploadFile_v0/FileUploader.php", new XMLHttpRequest(), formdata);
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4) {
        var sendBackData = JSON.parse(xhr.responseText);
        if(sendBackData['success']){
          if(type === "video")
            elasticTranscoder(data['key'], file.name);
          var urlEmbed = "";
          if(type === "video"){
            urlEmbed = createEmbed((key.split('.')[0]+"/"+key.split("/")[key.split("/").length - 1]).split(".")[0]);
          } else {
            urlEmbed = '<img alt="Embed Imgage" height="100%" width="100%" src="' + sendBackData['url'] + '"/>';
          }
          directoryInsert(fileIndex, urlEmbed, type);
        }
      }
    };
  }

根据我收集的内容,我需要文件,密钥,上传ID以及已成功上传的部分。我有什么建议可以做到这一点吗?

0 个答案:

没有答案