1

ファイル アップローダに一時停止/再開機能を実装するためのサポートが必要です。現在、インターネットの状態が悪い場合を除き、アップローダは意図したとおりに動作します。ユーザーのファイルが破損したり破損したりして、アップロード バケットにアップロードされる事例がありました。この問題を軽減する方法は、この機能をアップローダに実装することです。マルチパートアップロードの現在のセクション

インストラクターアップロード.js

/**
 * 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);
        }
      }
    };
  }

FileUpload.php

function s3Command($command){
    switch ($command) {
        case 'CreateMultipartUpload':
            $fileindex = $_POST['fileindex'];
            $key       = preg_replace('/\s+/', '_', $_POST['key']);

            $response = $GLOBALS["s3"]->createMultipartUpload(array(
                'Bucket' => TMP_IMG,
                'Key'    => $key,
                'ACL'    => 'public-read'
            ));

            $uploadId = $response['UploadId'];

            sendJson(array(
                'uploadId'  => $uploadId,
                'key'       => $key,
                'fileindex' => $fileindex
            ));
            break;

        case 'UploadPart':
            $key       = preg_replace('/\s+/', '_', $_POST['key']);
            $tmp_files = $_FILES['file'];
            $files    = array();
            for ($i = 0; $i < count($tmp_files['name']); $i++){
                $files[] = array(
                    'name'     => $tmp_files['name'][$i],
                    'tmp_name' => $tmp_files['tmp_name'][$i],
                    'type'     => $tmp_files['type'][$i],
                    'size'     => $tmp_files['size'][$i],
                    'error'    => $tmp_files['error'][$i]
                );
            }

            $body = mergeFiles($files);

            $result = $GLOBALS["s3"]->uploadPart(array(
                'Bucket'     => TMP_IMG,
                'Key'        => $key,
                'UploadId'   => $_POST['uploadId'],
                'PartNumber' => $_POST['partNumber'],
                'Body'       => $body
            ));
            break;

        case 'CompleteMultipartUpload':
            $key       = preg_replace('/\s+/', '_', $_POST['key']);
            $partsModel = $GLOBALS["s3"]->listParts(array(
                'Bucket'   => TMP_IMG,
                'Key'      => $key,
                'UploadId' => $_POST['uploadId']
            ));

            $model = $GLOBALS["s3"]->completeMultipartUpload(array(
                'Bucket'   => TMP_IMG,
                'Key'      => $key,
                'UploadId' => $_POST['uploadId'],
                'Parts'    => $partsModel['Parts'],
            ));

            $url = $GLOBALS["s3"]->getObjectUrl(TMP_IMG, $key);
            $tmp  = explode('.', $key);
            $type = getFileType(end($tmp));

            if($type == 'image'){
                $url = resizeImage($url);
            }
            else if($type != 'video'){
                copyObject($key, FINISHED_IMG);
                $url = $GLOBALS["s3"]->getObjectUrl(FINISHED_IMG, $key);
            }

            sendJson(array(
                'success' => true,
                'url'     => $url,
                'key'     => $key,
                'type'    => $type
            ));
            break;

        case 'AbortMultipartUpload':
            $key = preg_replace('/\s+/', '_', $_POST['key']);
            $s3->abortMultipartUpload(array(
                'Bucket'   => TMP_IMG,
                'Key'      => $key,
                'UploadId' => $_POST['uploadId']
            ));
            break;

        default:
            # code...
            break;
    }
}

収集したものから、ファイル、そのキー、アップロード ID、および正常にアップロードされた部分が必要です。これを達成するために私ができることについて何か提案はありますか?

4

0 に答える 0