Skip to content

Instantly share code, notes, and snippets.

@usmanfarman
Created October 12, 2013 18:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usmanfarman/6953473 to your computer and use it in GitHub Desktop.
Save usmanfarman/6953473 to your computer and use it in GitHub Desktop.
Excerpts from two classes that I'm messing around with.
/**
* Download an attachment's content.
*
* @param string item_id ID of the timeline item the attachment belongs to.
* @param Google_Attachment $attachment Attachment's metadata.
* @return string The attachment's content if successful, null otherwise.
*/
function download_attachment($item_id, $attachment) {
$request = new Google_HttpRequest($attachment->getContentUrl(), 'GET', null, null);
$httpRequest = Google_Client::$io->authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
error_log("Error Downloading Attachment - HTTP ResponseCode:".$httpRequest->getResponseHttpCode());
error_log("ContentUrl: ".$attachment->getContentUrl());
error_log("ContentType: ".$attachment->getContentType());
error_log("id ".$attachment->getId());
error_log("IsProcessing: ".(int)$attachment->getIsProcessingContent());
return $httpRequest->getResponseBody();
} else {
// An error occurred.
error_log("Error Downloading Attachment - HTTP ResponseCode:".$httpRequest->getResponseHttpCode());
error_log("ContentUrl: ".$attachment->getContentUrl());
error_log("ContentType: ".$attachment->getContentType());
error_log("id ".$attachment->getId());
error_log("IsProcessing: ".(int)$attachment->getIsProcessingContent());
return null;
}
}
//$myGlass class is just a wrapper for mirror-client.php from google's quick-start php project'
switch ($request['collection']) {
case 'timeline':
foreach ($request['userActions'] as $i => $user_action) {
if ($user_action['type'] == 'SHARE') {
$timeline_item_id = $request['itemId'];
$timeline_item = $mirror_service->timeline->get($timeline_item_id);
error_log('Incoming Google Glass Attachment');
$timeline_caption = $timeline_item->getText();
if ($timeline_item->getAttachments() != null) {
$attachments = $timeline_item->getAttachments();
foreach ($attachments as $attachment){
$myId = $timeline_item_id;
$attachmentId = $attachment->getId();
$contentType = $attachment->getContentType();
$contentUrl = $attachment->getContentUrl();
$isMovie = false;
if ($contentType == 'video/mp4'){
$isMovie = true;
}
error_log($contentType);
//munge paths and save the file to the filesystem
$upload_dir = get_upload_dir();
$savePath = $upload_dir['path'];
$fileName = 'Test_'.uniqid('', true);
//just sets the correct extension based on mimetype
$fileExt = getExtension($contentType);
$finalFilePath = $savePath."/".$fileName.".".$fileExt;
//save the file down to the filesystem
$fileData = $myGlass->download_attachment($myId, $attachment);
file_put_contents($finalFilePath, $fileData);
}
}
//let the user know its been received and posted with a patch-update to their timeline
$patch = new Google_TimelineItem();
$patch->setText("Upload Complete! ".$timeline_item->getText());
$mirror_service->timeline->patch($timeline_item_id, $patch);
error_log('Finished receiving data');
break;
}
}
break;
default:
error_log("I don't know how to process this notification: $request");
}
@Etheriq
Copy link

Etheriq commented Oct 12, 2013

cool

@kudataz
Copy link

kudataz commented Jan 20, 2015

what function this?
any example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment