Picasa image/video resumable upload issue

287 views Asked by At

I am trying to upload a video/image by using google api resumable upload option .

If i have 200MB file So i have to do chunk upload N times . My code return 308 status code for N-1 times (Except last one). For last chunk google not responding me (My php server says "empty-reply-from-server")

Please help me where i am wrong.

global $access_token;
$title = basename($videoPath);
$mimeType = mime_content_type($videoPath);
$content_length = getSize($videoPath);
$curl = new Curl();
$curl->setHeader("Host", "picasaweb.google.com");
$curl->setHeader("Content-Length", 0);
$curl->setHeader("X-upload-content-length", $content_length);
$curl->setHeader("X-upload-content-type", $mimeType);
$curl->setHeader("Slug", $title);
$curl->setHeader("Authorization", "Bearer $access_token");
$curl->setHeader("Content-Type", 'application/atom+xml');
$curl->setHeader("User-Agent", "CXchange-CXchange-1.0 cURL/1.11.3");
$curl->setHeader("Accept-encoding", "identity");
$curl->setHeader("GData-Version", "2");
$curl->setHeader("MIME-version", "1.0");
try {
    //Get the resumable uri
    $url = "https://picasaweb.google.com/data/upload/resumable/media/create-session/feed/api/user/default/albumid/$id";
    $responce = $curl->post($url);
    $responce_code = $curl->http_status_code;
    if ($responce_code == 200) {
        $headers = $curl->response_headers;
        foreach ($headers as $header) {
            if (strstr($header, "Location")) {
                echo $uri = str_replace("Location:", "", $header);
                break;
            }
        }
    } else {
        echo "Upload faild";
    }

    $cnt = 0;
    $chunk_size = 256 * 1024 * 1;
    $handle = fopen($videoPath, 'rb');
    if ($handle === false) {
        return false;
    }
    $progress = 0;
    while (!feof($handle)) {
        $buffer = fread($handle, $chunk_size);
        $cnt = strlen($buffer);
        $lastBytePos = $progress + $cnt - 1;
        $curl = new Curl();
        $curl->setHeader("Host", "picasaweb.google.com");
        $curl->setHeader("Content-Length", $chunk_size);
        $curl->setHeader("Authorization", "Bearer $access_token");
        $curl->setHeader("Content-Type", $mimeType);
        $curl->setHeader("User-Agent", "CXchange-CXchange-1.0 cURL/1.11.3");
        $curl->setHeader("Accept-encoding", "identity");
        $curl->setHeader("Content-Range", "bytes $progress-$lastBytePos/$content_length");
        $curl->setHeader("GData-Version", "2");
        $curl->setHeader("MIME-version", "1.0");
        $curl->setHeader("Expect", "");

        $responce = $curl->put(trim($uri), $buffer);
        $responce_code = $curl->http_status_code;
        if ($responce_code == 308) {
            $headers = $curl->response_headers;
            foreach ($headers as $header) {
                if (strstr($header, "Range:")) {
                    $temp = explode("-", $header);
                    $progress = end($temp)+1;
                    break;
                }
            }
        } else {
            echo "Upload faild";
        }
    }
    fclose($handle);
} catch (Exception $e) {
    echo '';
}
1

There are 1 answers

0
user3401652 On

code: 400 msg: Premature end of file.

After read this topic, I found the solution is to give something to first session request. You just can't leave it without a body.

$xmldata =
'<entry xmlns="http://www.w3.org/2005/Atom">
    <title>cats.jpg</title>
    <summary>Real cat wants attention too.</summary>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/photos/2007#photo"/>
 </entry>';
$request  = new Google_Http_Request(
    "https://picasaweb.google.com/data/upload/resumable/media/create-session/feed/api/user/{$person->id}/albumid/6185252512807301377?alt=json&access_token=" .$token['access_token'],
    "POST",
    array('GData-Version' => 2,
        'Content-Type' => 'application/atom+xml',
        'MIME-Version' => '1.0',
        'Content-Length' => strlen($xmldata),
        'X-Upload-Content-Length' => $size,
        'X-Upload-Content-Type' => 'image/jpeg',
        'Slug' => 'xxxx.jpg',
        'Authorization' => "Bearer {$token['access_token']}"),
        $xmldata
);
$response = Google_Http_REST::doExecuteRaw($client, $request);

Slug header is required but file name will be cats.jpg (xml).