Search code examples
actionscript-3vimeovimeo-api

Vimeu resume PUT upload issue on air app


I'm working on an Adobe Air app that use the brilliant Vimeo upload API. Upload works really fine.

However, I now try to resume an upload and can't understand why it doesn't work. Could someone help me to understand why ?

Here is my process as proposed in the doc. Code is AS3:

verify the upload

var r:URLRequest = new URLRequest(_ticket.upload_link_secure);
r.requestHeaders = [
    new URLRequestHeader("Authorization", "bearer " + TOKEN),
    new URLRequestHeader("Content-Length", "0"),
    new URLRequestHeader("Content-Range", "bytes */*"),
];
r.contentType = "video/mp4"
r.method = URLRequestMethod.PUT;

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, _resumeProgressComplete);
loader.load(r);

==> the result of the request :
status 308
the range header is bytes=0-474295

resume upload

My video size : 11685783
The size of the ByteArray is send : 6533957
The range header : 5151826-11685783/11685783
The content length : 6533957 (I also tried ByteArray size +1 as shown on the doc)

var r:URLRequest = new URLRequest(_ticket.upload_link_secure);
r.requestHeaders = [
    new URLRequestHeader("Authorization", "bearer " + TOKEN),
    new URLRequestHeader("Content-Length", contentLength),
    new URLRequestHeader("Content-Range", range),
];
r.contentType = "video/mp4"
r.method = URLRequestMethod.PUT;
r.data = ba;
_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
_urlLoader.addEventListener(Event.COMPLETE, _resumeComplete);
_urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, _resumeHTTPStatus);
_urlLoader.load(r);

Result status of the request is 200

While uploading the missing part of my video I asynchronously send requests to get the progress of the upload

var r:URLRequest = new URLRequest(_ticket.upload_link_secure);
r.requestHeaders = [
    new URLRequestHeader("Authorization", "bearer " + TOKEN),
    new URLRequestHeader("Content-Length", "0"),
    new URLRequestHeader("Content-Range", "bytes */*"),
];
r.contentType = "video/mp4"
r.method = URLRequestMethod.PUT;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, _uploadProgressComplete);
loader.load(r);

The results :

bytes=0-0  
bytes=0-228970  
bytes=0-441585  
bytes=0-556070  
...  
bytes=0-6247610  
bytes=0-6492935  
bytes=0-6533957 

The last verification done, I send want to complete the upload

var r:URLRequest = new URLRequest("https://api.vimeo.com/" + _ticket.complete_uri);
r.requestHeaders = [
    new URLRequestHeader("Authorization", "bearer " + TOKEN)
];
r.method = URLRequestMethod.DELETE;
_urlLoader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, _completeUploadComplete);
_urlLoader.addEventListener(Event.COMPLETE, _completeUploadCompleteData);
_urlLoader.load(r);

And that's where I receive a 400 status an the error message below:

{"error":"Your video file is not valid. Either you have uploaded an invalid file format, or your upload is incomplete. Make sure you verify your upload before marking it as complete."}

Is there something wrong on my code, I followed the documentation?


Solution

  • thanks to Upload a video file by chunks i could see where was my mistake :

    new URLRequestHeader("Content-Range", "bytes " + range),
    

    i simply forgot the "byte " in the header value...

    By the way, i modified my range as written in the answer of the question below

    And here is the result

    var range:String = String(firstByte) + "-" + String(_videoSize - 1) + "/" + String(_videoSize);
    
    // envoi de la vidéo
    var r:URLRequest = new URLRequest(_ticket.upload_link_secure);
    r.requestHeaders = [
        new URLRequestHeader("Authorization", "bearer " + TOKEN),
        new URLRequestHeader("Content-Length", contentLength),
        new URLRequestHeader("Content-Range",  "bytes " + range),
    ];
    r.contentType = "video/mp4"
    r.method = URLRequestMethod.PUT;
    r.data = ba;
    
    _urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
    _urlLoader.addEventListener(Event.COMPLETE, _resumeComplete);
    _urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, _resumeHTTPStatus);
    _urlLoader.load(r);
    

    Happy :)