You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by ch...@apache.org on 2009/08/13 20:53:37 UTC
svn commit: r803987 - in /incubator/shindig/trunk/php: src/social/sample/
src/social/service/ src/social/servlet/ src/social/spi/ test/misc/album/
test/misc/upload/ test/social/
Author: chabotc
Date: Thu Aug 13 18:53:37 2009
New Revision: 803987
URL: http://svn.apache.org/viewvc?rev=803987&view=rev
Log:
SHINDIG-1136 by Jinhui Du - Add support for uploading media content to the social end-points.
Description:
It'll move the uploaded file via the REST(image/*,video/*,audio/*) or
JSON-RPC(multipart/form-data) api to a temporary directory.
And pass the file info to the container specified method.
With partuza the upload.xml can be used test the JSON RPC(multipart/form-data) upload
api and the REST(multipart/form-data) upload api.
The upload.php can be used to test the REST(image/*) upload api.
Added:
incubator/shindig/trunk/php/test/misc/album/
incubator/shindig/trunk/php/test/misc/album/album.xml
incubator/shindig/trunk/php/test/misc/upload/
incubator/shindig/trunk/php/test/misc/upload/upload.php
incubator/shindig/trunk/php/test/misc/upload/upload.xml
Modified:
incubator/shindig/trunk/php/src/social/sample/JsonDbOpensocialService.php
incubator/shindig/trunk/php/src/social/service/AlbumHandler.php
incubator/shindig/trunk/php/src/social/service/MediaItemHandler.php
incubator/shindig/trunk/php/src/social/servlet/ApiServlet.php
incubator/shindig/trunk/php/src/social/spi/MediaItemService.php
incubator/shindig/trunk/php/test/social/MediaItemRestTest.php
Modified: incubator/shindig/trunk/php/src/social/sample/JsonDbOpensocialService.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/sample/JsonDbOpensocialService.php?rev=803987&r1=803986&r2=803987&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/sample/JsonDbOpensocialService.php (original)
+++ incubator/shindig/trunk/php/src/social/sample/JsonDbOpensocialService.php Thu Aug 13 18:53:37 2009
@@ -814,7 +814,7 @@
return self::paginateResults($results, $options);
}
- public function createMediaItem($userId, $groupId, $mediaItem, $token) {
+ public function createMediaItem($userId, $groupId, $mediaItem, $file, $token) {
$all = $this->getAllMediaItems();
$albumId = $mediaItem['albumId'];
$id = isset($all[$albumId]) ? (count($all[$albumId]) + 1) : 0;
Modified: incubator/shindig/trunk/php/src/social/service/AlbumHandler.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/service/AlbumHandler.php?rev=803987&r1=803986&r2=803987&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/service/AlbumHandler.php (original)
+++ incubator/shindig/trunk/php/src/social/service/AlbumHandler.php Thu Aug 13 18:53:37 2009
@@ -41,9 +41,9 @@
HandlerPreconditions::requireSingular($userIds, "userId must be singular value.");
HandlerPreconditions::requireNotEmpty($groupId, "groupId must be specified.");
- HandlerPreconditions::requireSingular($albumIds, "albumId must singular value.");
+ HandlerPreconditions::requireNotEmpty($albumIds, "albumId must be specified.");
- $this->service->deleteAlbum($userIds[0], $groupId, $albumIds[0], $requestItem->getToken());
+ $this->service->deleteAlbum($userIds[0], $groupId, $albumIds, $requestItem->getToken());
}
/**
Modified: incubator/shindig/trunk/php/src/social/service/MediaItemHandler.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/service/MediaItemHandler.php?rev=803987&r1=803986&r2=803987&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/service/MediaItemHandler.php (original)
+++ incubator/shindig/trunk/php/src/social/service/MediaItemHandler.php Thu Aug 13 18:53:37 2009
@@ -97,8 +97,94 @@
HandlerPreconditions::requireSingular($albumIds, "albumId must be sigular value.");
HandlerPreconditions::requireNotEmpty($mediaItem, "mediaItem must be specified.");
$mediaItem['albumId'] = $albumIds[0];
+ $file = array();
+ if (isset($mediaItem['url']) && substr($mediaItem['url'], 0, strlen('@field:')) != '@field:') {
+ $file = $this->processRemoteContent($mediaItem['url']);
+ } else {
+ $file = $this->processUploadedContent();
+ }
- return $this->service->createMediaItem($userIds[0], $groupId, $mediaItem, $requestItem->getToken());
+ $ret = $this->service->createMediaItem($userIds[0], $groupId, $mediaItem, $file, $requestItem->getToken());
+ if (isset($file['tmp_name']) && file_exists($file['tmp_name'])) {
+ @unlink($file['tmp_name']);
+ }
+ return $ret;
+ }
+
+ /**
+ * Fetches the remote media content and saves it as a temporary file. Returns the meta data of the file.
+ */
+ private function processRemoteContent($uri) {
+ $request = new RemoteContentRequest($uri);
+ $request->createRemoteContentRequestWithUri($uri);
+ $brc = new BasicRemoteContent();
+ $response = $brc->fetch($request);
+ if ($response->getHttpCode() != 200) {
+ throw new SocialSpiException("Failed to fetch the content from $uri code: " . $response->getHttpCode(), ResponseError::$BAD_REQUEST);
+ }
+ if (!$this->isValidContentType($response->getContentType())) {
+ throw new SocialSpiException("The content type " . $response->getContentType() .
+ " fetched from $uri is not valid.", ResponseError::$BAD_REQUEST);
+ }
+ return $this->writeBinaryContent($response->getResponseContent(), $response->getContentType());
+ }
+
+ /**
+ * Checks the $_FILES and HTTP_RAW_POST_DATA variables to write the user uploaded content as a temporary file.
+ * Returns the meta data of the file.
+ */
+ private function processUploadedContent() {
+ $file = array();
+ if (! empty($_FILES)) {
+ // The RPC api supports to post the file using the content type 'multipart/form-data'.
+ $uploadedFile = current($_FILES);
+ if ($uploadedFile['error'] != UPLOAD_ERR_OK) {
+ if ($uploadedFile['error'] == UPLOAD_ERR_INI_SIZE || $uploadedFile == UPLOAD_ERR_FORM_SIZE) {
+ throw new SocialSpiException("The uploaded file is too large.", ResponseError::$REQUEST_TOO_LARGE);
+ } else {
+ throw new SocialSpiException("Failed to upload the file.", ResponseError::$BAD_REQUEST);
+ }
+ }
+ if (!$this->isValidContentType($uploadedFile['type'])) {
+ throw new SocialSpiException("The content type of the uploaded file " . $uploadedFile['type'] . " is not valid.", ResponseError::$BAD_REQUEST);
+ }
+ $tmpName = tempnam('', 'shindig');
+ if (!move_uploaded_file($uploadedFile['tmp_name'], $tmpName)) {
+ throw new SocialSpiException("Failed to move the uploaded file.", ResponseError::$INTERNAL_ERROR);
+ }
+ $file['tmp_name'] = $tmpName;
+ $file['size'] = $uploadedFile['size'];
+ $file['type'] = $uploadedFile['type'];
+ $file['name'] = $uploadedFile['name'];
+ } else if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
+ // The REST api supports to post the file using the content type 'image/*', 'video/*' or 'audio/*'.
+ if ($this->isValidContentType($_SERVER['CONTENT_TYPE'])) {
+ $file = $this->writeBinaryContent($GLOBALS['HTTP_RAW_POST_DATA'], $_SERVER['CONTENT_TYPE']);
+ }
+ }
+ return $file;
+ }
+
+ /**
+ * Writes the binary content to a temporary file and returns the meta data of the file.
+ */
+ private function writeBinaryContent(&$rawData, $contentType) {
+ $tmpName = tempnam('', 'shindig');
+ $fp = fopen($tmpName, 'w');
+ if (!fwrite($fp, $rawData)) {
+ throw new SocialSpiException("Failed to write the uploaded file.", ResponseError::$INTERNAL_ERROR);
+ }
+ fclose($fp);
+ return array('tmp_name' => $tmpName, 'size' => filesize($tmpName), 'name' => basename($tmpName), 'type' => $contentType);
+ }
+
+ /**
+ * Returns true if the given content type is valid.
+ */
+ private function isValidContentType($contentType) {
+ $acceptedMediaPrefixes = array('image', 'video', 'audio');
+ $prefix = substr($contentType, 0, strpos($contentType, '/'));
+ return in_array($prefix, $acceptedMediaPrefixes);
}
/**
Modified: incubator/shindig/trunk/php/src/social/servlet/ApiServlet.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/servlet/ApiServlet.php?rev=803987&r1=803986&r2=803987&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/servlet/ApiServlet.php (original)
+++ incubator/shindig/trunk/php/src/social/servlet/ApiServlet.php Thu Aug 13 18:53:37 2009
@@ -80,7 +80,13 @@
}
if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
if (! isset($_SERVER['CONTENT_TYPE']) || ! in_array($_SERVER['CONTENT_TYPE'], $acceptedContentTypes)) {
- throw new Exception("When posting to the social end-point you have to specify a content type, supported content types are: 'application/json', 'application/xml' and 'application/atom+xml'");
+ $prefix = substr($_SERVER['CONTENT_TYPE'], 0, strpos($_SERVER['CONTENT_TYPE'], '/'));
+ $acceptedMediaPrefixes = array('image', 'video', 'audio');
+ if (! in_array($prefix, $acceptedMediaPrefixes)) {
+ throw new Exception("When posting to the social end-point you have to specify a content type,
+ supported content types are: 'application/json', 'application/xml' and 'application/atom+xml'.
+ For content upload, content type can be 'image/*', 'audio/*' and 'video/*'");
+ }
}
}
}
Modified: incubator/shindig/trunk/php/src/social/spi/MediaItemService.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/spi/MediaItemService.php?rev=803987&r1=803986&r2=803987&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/spi/MediaItemService.php (original)
+++ incubator/shindig/trunk/php/src/social/spi/MediaItemService.php Thu Aug 13 18:53:37 2009
@@ -38,16 +38,24 @@
public function getMediaItems($userId, $groupId, $albumId, $mediaItemIds, $collectionOptions, $fields, $token);
/**
- * Creates a media item in a specified album. The album-id is taken from the
- * albumMediaItem object. id of the media item object should not be set.
+ * Creates a media item in a specified album. The albumId is taken from the
+ * mediaItem object. id of the media item object should not be set. A file may
+ * be uploaded with the content type 'multipart/form-data', 'image/*', 'video/*'
+ * or 'audio/*'. The uploaded file is moved to a temporary location. The file info
+ * is stored in the 'file' param. After this method is invoked the file is deleted.
*
* @param userId id of the user for whom a media item is to be created
* @param groupId group id
* @param mediaItem specifies album-id and media item fields
+ * @param An associative array that describes the uploaded file. The array is empty if
+ * there is no uploaded file. It has 'name', 'tmp_name', 'type' and 'size' fields.
+ * i.e. [tmp_name] => /tmp/upload//tmp/php/php1h4j1o, [type] => image/png,
+ * [size] => 123, [name] = user_file_name.png.
+ * The file is a regular file and should not be moved by the move_uploaded_file method.
* @param token security token to authorize this request
* @return the created media item
*/
- public function createMediaItem($userId, $groupId, $mediaItem, $token);
+ public function createMediaItem($userId, $groupId, $mediaItem, $file, $token);
/**
* Updates a media item in an album. Album id and media item id is taken in
Added: incubator/shindig/trunk/php/test/misc/album/album.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/test/misc/album/album.xml?rev=803987&view=auto
==============================================================================
--- incubator/shindig/trunk/php/test/misc/album/album.xml (added)
+++ incubator/shindig/trunk/php/test/misc/album/album.xml Thu Aug 13 18:53:37 2009
@@ -0,0 +1,168 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Module>
+ <ModulePrefs title="Album test">
+ <Require feature="dynamic-height"/>
+ </ModulePrefs>
+ <Content type="html">
+ <![CDATA[
+ <script type="text/javascript">
+
+$ = function(id) {
+ return document.getElementById(id);
+}
+
+
+function sendRequest(url, method, data) {
+ var xhr = new window.XMLHttpRequest();
+ xhr.open(method, url, true);
+ xhr.setRequestHeader("Content-type", "application/json");
+ var body = data ? gadgets.json.stringify(data) : null;
+ xhr.send(body);
+}
+
+function restCreate() {
+ var title = $('create-title');
+ var desc = $('create-description');
+ var data = {};
+ data['title'] = title.value;
+ data['description'] = desc.value;
+ data['mediaType'] = 'IMAGE';
+ var url = $('rest-url').value + '?' + $('param').value;
+ sendRequest(url, 'POST', data);
+}
+
+function jsonCreate() {
+ var title = $('create-title');
+ var desc = $('create-description');
+ var data = {};
+ data['method'] = 'albums.create';
+ data['params'] = {'album' : {'title': title.value, 'description': desc.value, 'mediaType': 'IMAGE'}};
+ data['params']['userId'] = '@me';
+ data['params']['groupId'] = '@self';
+ data['id'] = 'createAlbum';
+ var url = $('json-rpc-url').value + '?' + $('param').value;
+ sendRequest(url, 'POST', data);
+}
+
+function restGet() {
+ var albumId = $('get-album-id').value;
+ var url = '';
+ if (albumId) {
+ url = $('rest-url').value + '/' + albumId + '?' + $('param').value;
+ } else {
+ url = $('rest-url').value + '?' + $('param').value;
+ }
+ var startIndex = $('get-start-index').value;
+ if (startIndex) {
+ url += '&startIndex=' + startIndex;
+ }
+ var count = $('get-count').value;
+ if (count) {
+ url += '&count=' + count;
+ }
+ sendRequest(url, 'GET', null);
+}
+
+function jsonGet() {
+ var data = {'method': 'albums.get', 'id': 'getAlbum'};
+ data['params'] = {'userId': '@me', 'groupId': '@self'};
+ var albumId = $('get-album-id').value;
+ var url = $('json-rpc-url').value + '?' + $('param').value;
+ var id = [];
+ if (albumId) {
+ id = albumId.split(',');
+ }
+ data['params']['albumId'] = id;
+ var startIndex = $('get-start-index').value;
+ if (startIndex) {
+ data['params']['startIndex'] = startIndex;
+ }
+ var count = $('get-count').value;
+ if (count) {
+ data['params']['count'] = count;
+ }
+ sendRequest(url, 'POST', data);
+}
+
+function restDelete() {
+ var albumId = $('delete-album-id').value;
+ var url = $('rest-url').value + '/' + albumId + '?' + $('param').value;
+ sendRequest(url, 'DELETE', null);
+}
+
+function jsonDelete() {
+ var albumId = $('delete-album-id').value;
+ var ids = albumId.split(',');
+ var data = {};
+ data['method'] = 'albums.delete';
+ data['params'] = {'albumId': ids};
+ var url = $('json-rpc-url').value + '?' + $('param').value;
+ sendRequest(url, 'POST', data);
+}
+
+function restUpdate() {
+ var title = $('update-title');
+ var desc = $('update-description');
+ var data = {'title': title.value, 'description': desc.value, 'mediaType': 'IMAGE', 'location':{'latitude':100, 'longitude':200}};
+ var albumId = $('update-album-id').value;
+ var url = $('rest-url').value + '/' + albumId + '?' + $('param').value;
+ sendRequest(url, 'PUT', data);
+}
+
+function jsonUpdate() {
+ var title = $('update-title');
+ var desc = $('update-description');
+ var data = {};
+ data['method'] = 'albums.update';
+ data['params'] = {'album' : {'title': title.value, 'description': desc.value, 'mediaType': 'IMAGE', 'location':{'latitude':100, 'longitude':200}}};
+ data['params']['userId'] = '@me';
+ data['params']['groupId'] = '@self';
+ data['id'] = 'updateAlbum';
+ data['params']['albumId'] = $('update-album-id').value;
+ var url = $('json-rpc-url').value + '?' + $('param').value;
+ sendRequest(url, 'POST', data);
+}
+
+function init() {
+ gadgets.window.adjustHeight();
+}
+
+gadgets.util.registerOnLoadHandler(init);
+ </script>
+ <p>The gadget is used to test the create/update/delete/get albums
+ functionality via the REST and JSON-RPC api.<br/> Please use the firebug to
+ check the request and the response. </p>
+
+ <div>
+ REST URL: <input id="rest-url" style="margin-left:40px" type="text" size=60 value="http://shindig/social/rest/albums/@me/@self"><br/>
+ JSON-RPC URL: <input id="json-rpc-url" type="text" size=60 value="http://shindig/social/rpc"/><br/>
+ Param: <input id="param" style="margin-left:70px" type="text" size=60 value="st=1:1:1:partuza:test.com:1:0"/>
+ </div>
+ <p><b>Create the album</b></p>
+ Title:<input id="create-title" type="text" style="margin-left:50px" size=60 value="default album title"/><br/>
+ Description:<input id="create-description" type="text" size=60 value="the description of the create album"/><br/>
+ <input type="submit" value="REST" onclick=restCreate() />
+ <input type="submit" value="JSON-RPC" onclick=jsonCreate() />
+
+ <p><b>Get the album.</b> </p>
+ Album Id:<input id="get-album-id" size=3 type="text"/>
+ startIndex:<input id="get-start-index" size=3 type="text"/>
+ count:<input id="get-count" size=3 type="text"/>
+
+ <input type="submit" value="REST" onclick=restGet() />
+ <input type="submit" value="JSON-RPC" onclick=jsonGet() /><br/>
+
+ <p><b>Update the album</b></p>
+ Album Id:<input id="update-album-id" size=5 type="text"/><br/>
+ Title:<input id="update-title" type="text" style="margin-left:50px" size=60 value="updated album title"/><br/>
+ Description:<input id="update-description" type="text" size=60 value="updated description"/><br/>
+ <input type="submit" value="REST" onclick=restUpdate() />
+ <input type="submit" value="JSON-RPC" onclick=jsonUpdate() />
+
+ <p><b>Delete the album</b><p>
+ Album Id: <input id="delete-album-id" size=5 type="text"/><br/>
+ <input type="submit" value="REST" onclick=restDelete() />
+ <input type="submit" value="JSON-RPC" onclick=jsonDelete() />
+ ]]>
+ </Content>
+</Module>
Added: incubator/shindig/trunk/php/test/misc/upload/upload.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/test/misc/upload/upload.php?rev=803987&view=auto
==============================================================================
--- incubator/shindig/trunk/php/test/misc/upload/upload.php (added)
+++ incubator/shindig/trunk/php/test/misc/upload/upload.php Thu Aug 13 18:53:37 2009
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * Post the file to the specified REST api endpoint. It's used to test the REST
+ * api for content upload.
+ */
+function curlRest($url, $fileName, $contentType) {
+ $fp = fopen($fileName, 'r');
+ $fileSize = filesize($fileName);
+
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: $contentType", "Expect:"));
+ curl_setopt($ch, CURLOPT_HEADER, 0);
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
+ curl_setopt($ch, CURLOPT_UPLOAD, 1);
+
+ curl_setopt($ch, CURLOPT_INFILESIZE, $fileSize);
+ curl_setopt($ch, CURLOPT_INFILE, $fp);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ $ret = curl_exec($ch);
+ curl_close($ch);
+ return $ret;
+}
+
+// The title is 'opensocial' and the description is 'icon'.
+$url = "http://shindig/social/rest/mediaitems/@me/@self/1?st=1:1:1:partuza:test.com:1:0&mediaType=IMAGE&title=opensocial&description=icon";
+// Create a media item with a image file uploaded.
+$ret = curlRest($url, "icon.jpg", "image/jpg");
+
+var_dump($ret);
Added: incubator/shindig/trunk/php/test/misc/upload/upload.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/test/misc/upload/upload.xml?rev=803987&view=auto
==============================================================================
--- incubator/shindig/trunk/php/test/misc/upload/upload.xml (added)
+++ incubator/shindig/trunk/php/test/misc/upload/upload.xml Thu Aug 13 18:53:37 2009
@@ -0,0 +1,128 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Module>
+ <ModulePrefs title="Content upload">
+ <Require feature="dynamic-height"/>
+ </ModulePrefs>
+ <Content type="html">
+ <![CDATA[
+ <script type="text/javascript">
+
+function initData() {
+ var jsonInput = document.getElementById("json-input");
+ jsonInput.value = ' [ { "method":"mediaitems.create", "params": {"albumId":"1", "mediaItem": { "id" : "11223344", "thumbnailUrl" : "http://pages.example.org/images/11223344-tn.png", "mimeType" : "image/png", "type" : "image", "url" : "http://pages.example.org/images/11223344.png", "albumId" : "1" } } } ]';
+
+ gadgets.window.adjustHeight();
+}
+
+
+function restDelete() {
+ var restUrl = document.getElementById("rest-delete-url");
+ var xhr = new window.XMLHttpRequest();
+ xhr.open("DELETE", restUrl.value, true);
+ xhr.setRequestHeader("Content-type", "application/json");
+ xhr.send(null);
+}
+
+function restGet() {
+ var restUrl = document.getElementById("rest-get-url");
+ var xhr = new window.XMLHttpRequest();
+ xhr.open("GET", restUrl.value, true);
+ xhr.setRequestHeader("Content-type", "application/json");
+ xhr.send(null);
+}
+
+gadgets.util.registerOnLoadHandler(initData);
+ </script>
+
+ please use firebug to see the requests and the responses and copy the created url to browser address bar to fetch the created image.
+ <p>The JSON RPC method.</p>
+ The JSON data that will be posted.
+ <form enctype="multipart/form-data"
+ action="http://shindig/social/rpc?st=1:1:1:partuza:test.com:1:0" method="POST">
+ <textarea id="json-input" cols=100 rows=4 name="request" ></textarea><br/>
+ The image file to upload:
+ <input name="uploadedfile" type="file" /><br />
+ <input type="submit" value="JsonRpc create media item." />
+ </form>
+ <br/>
+ <p> The REST method.</p>
+ <form enctype="multipart/form-data" action="http://shindig/social/rest/mediaitems/@me/@self/1?st=1:1:1:partuza:test.com:1:0&mediaType=IMAGE&title=mobile&description=g1" method="POST">
+ Choose an image file to upload:
+ <input type="file" name="uploadedfile" />
+ <input type="submit" value="Rest create media item." />
+ </form>
+ <p> REST delete </p>
+ The url: <input id="rest-delete-url" size=60 type="text" value="http://shindig/social/rest/mediaitems/@me/@self/1/64?st=1:1:1:partuza:test.com:1:0" />
+ <input type="button" onclick="restDelete()" value="delete"/>
+
+ <p> REST get </p>
+ The url: <input id="rest-get-url" size=60 type="text" value="http://shindig/social/rest/mediaitems/@me/@self/1/64?st=1:1:1:partuza:test.com:1:0" />
+ <input type="button" onclick="restGet()" value="get"/>
+
+ ]]>
+ </Content>
+</Module>
+<?xml version="1.0" encoding="UTF-8"?>
+<Module>
+ <ModulePrefs title="Content upload">
+ <Require feature="dynamic-height"/>
+ </ModulePrefs>
+ <Content type="html">
+ <![CDATA[
+ <script type="text/javascript">
+
+function initData() {
+ var jsonInput = document.getElementById("json-input");
+ jsonInput.value = ' [ { "method":"mediaitems.create", "params": {"albumId":"1", "mediaItem": { "id" : "11223344", "thumbnailUrl" : "http://pages.example.org/images/11223344-tn.png", "mimeType" : "image/png", "type" : "image", "url" : "http://pages.example.org/images/11223344.png", "albumId" : "1" } } } ]';
+
+ gadgets.window.adjustHeight();
+}
+
+
+function restDelete() {
+ var restUrl = document.getElementById("rest-delete-url");
+ var xhr = new window.XMLHttpRequest();
+ xhr.open("DELETE", restUrl.value, true);
+ xhr.setRequestHeader("Content-type", "application/json");
+ xhr.send(null);
+}
+
+function restGet() {
+ var restUrl = document.getElementById("rest-get-url");
+ var xhr = new window.XMLHttpRequest();
+ xhr.open("GET", restUrl.value, true);
+ xhr.setRequestHeader("Content-type", "application/json");
+ xhr.send(null);
+}
+
+gadgets.util.registerOnLoadHandler(initData);
+ </script>
+
+ please use firebug to see the requests and the responses and copy the created url to browser address bar to fetch the created image.
+ <p>The JSON RPC method.</p>
+ The JSON data that will be posted.
+ <form enctype="multipart/form-data"
+ action="http://shindig/social/rpc?st=1:1:1:partuza:test.com:1:0" method="POST">
+ <textarea id="json-input" cols=100 rows=4 name="request" ></textarea><br/>
+ The image file to upload:
+ <input name="uploadedfile" type="file" /><br />
+ <input type="submit" value="JsonRpc create media item." />
+ </form>
+ <br/>
+ <p> The REST method.</p>
+ <form enctype="multipart/form-data" action="http://shindig/social/rest/mediaitems/@me/@self/1?st=1:1:1:partuza:test.com:1:0&mediaType=IMAGE&title=mobile&description=g1" method="POST">
+ Choose an image file to upload:
+ <input type="file" name="uploadedfile" />
+ <input type="submit" value="Rest create media item." />
+ </form>
+ <p> REST delete </p>
+ The url: <input id="rest-delete-url" size=60 type="text" value="http://shindig/social/rest/mediaitems/@me/@self/1/64?st=1:1:1:partuza:test.com:1:0" />
+ <input type="button" onclick="restDelete()" value="delete"/>
+
+ <p> REST get </p>
+ The url: <input id="rest-get-url" size=60 type="text" value="http://shindig/social/rest/mediaitems/@me/@self/1/64?st=1:1:1:partuza:test.com:1:0" />
+ <input type="button" onclick="restGet()" value="get"/>
+
+ ]]>
+ </Content>
+</Module>
Modified: incubator/shindig/trunk/php/test/social/MediaItemRestTest.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/test/social/MediaItemRestTest.php?rev=803987&r1=803986&r2=803987&view=diff
==============================================================================
--- incubator/shindig/trunk/php/test/social/MediaItemRestTest.php (original)
+++ incubator/shindig/trunk/php/test/social/MediaItemRestTest.php Thu Aug 13 18:53:37 2009
@@ -45,6 +45,16 @@
// $this->assertTrue(empty($ret), "Delete the created album failed. Response: $ret");
}
+ private function curlGet($url) {
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_HEADER, 0);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ $ret = curl_exec($ch);
+ curl_close($ch);
+ return $ret;
+ }
+
private function verifyLifeCycle($postData, $postDataFormat) {
$url = '/mediaitems/1/@self/' . $this->album['id'];
$ret = $this->curlRest($url, $postData, $postDataFormat);
@@ -55,20 +65,19 @@
$this->assertFalse(empty($ret));
$fetched = json_decode($ret, true);
$fetched = $fetched['entry'][0];
- $this->assertEquals('http://pages.example.org/images/11223344.png', $fetched['url'], "url should be same.");
+ $data = $this->curlGet($fetched['url']);
+ $this->assertTrue(substr($data, 0, strlen('GIF')) == 'GIF');
$this->assertEquals('http://pages.example.org/images/11223344-tn.png', $fetched['thumbnailUrl'], "thumbnailUrl should be same.");
- $this->assertEquals('image/png', $fetched['mimeType'], "mimeType should be same.");
+ $this->assertEquals('image/gif', $fetched['mimeType'], "mimeType should be same.");
$this->assertEquals('IMAGE', $fetched['type'], "type should be same.");
-
$fetched['thumbnailUrl'] = 'http://changed.com/tn.png';
$ret = $this->curlRest($url . '/' . urlencode($mediaItem['id']), json_encode($fetched), 'application/json', 'PUT');
$ret = $this->curlRest($url . '/' . urlencode($mediaItem['id']), '', 'application/json', 'GET');
$this->assertFalse(empty($ret));
$fetched = json_decode($ret, true);
$fetched = $fetched['entry'][0];
- $this->assertEquals('http://pages.example.org/images/11223344.png', $fetched['url'], "url should be same.");
$this->assertEquals('http://changed.com/tn.png', $fetched['thumbnailUrl'], "thumbnailUrl should be same.");
- $this->assertEquals('image/png', $fetched['mimeType'], "mimeType should be same.");
+ $this->assertEquals('image/gif', $fetched['mimeType'], "mimeType should be same.");
$this->assertEquals('IMAGE', $fetched['type'], "type should be same.");
$ret = $this->curlRest($url . '/' . urlencode($mediaItem['id']), '', 'application/json', 'DELETE');
@@ -85,7 +94,7 @@
"thumbnailUrl" : "http://pages.example.org/images/11223344-tn.png",
"mimeType" : "image/png",
"type" : "image",
- "url" : "http://pages.example.org/images/11223344.png",
+ "url" : "http://www.google.com/intl/en_ALL/images/logo.gif",
"albumId" : "' . $this->album['id'] . '"
}';
$this->verifyLifeCycle($postData, 'application/json');
@@ -98,7 +107,7 @@
<thumbnailUrl>http://pages.example.org/images/11223344-tn.png</thumbnailUrl>
<mimeType>image/png</mimeType>
<type>image</type>
- <url>http://pages.example.org/images/11223344.png</url>
+ <url>http://www.google.com/intl/en_ALL/images/logo.gif</url>
<albumId>' . $this->album['id'] . '</albumId>
</mediaItem>';
$this->verifyLifeCycle($postData, 'application/xml');
@@ -112,7 +121,7 @@
<thumbnailUrl>http://pages.example.org/images/11223344-tn.png</thumbnailUrl>
<mimeType>image/png</mimeType>
<type>image</type>
- <url>http://pages.example.org/images/11223344.png</url>
+ <url>http://www.google.com/intl/en_ALL/images/logo.gif</url>
<albumId>' . $this->album['id'] . '</albumId>
</mediaItem>
</content>