2011-05-30 23:41:56 +00:00
/ *
Copyright 2011 Google Inc .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
2014-01-07 04:52:30 +00:00
http : //www.apache.org/licenses/LICENSE-2.0
2011-05-30 23:41:56 +00:00
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2014-01-08 05:28:01 +00:00
goog . provide ( 'cam.BlobPage' ) ;
2013-06-11 09:50:56 +00:00
goog . require ( 'goog.dom' ) ;
goog . require ( 'goog.ui.Component' ) ;
2014-01-08 06:07:26 +00:00
2014-01-08 05:28:01 +00:00
goog . require ( 'cam.ServerConnection' ) ;
2013-06-11 09:50:56 +00:00
2014-01-08 05:28:01 +00:00
// @param {cam.ServerType.DiscoveryDocument} config Global config of the current server this page is being rendered for.
2014-01-08 03:41:58 +00:00
// @param {goog.dom.DomHelper=} opt_domHelper DOM helper to use.
2014-01-08 05:28:01 +00:00
cam . BlobPage = function ( config , opt _domHelper ) {
2013-06-11 09:50:56 +00:00
goog . base ( this , opt _domHelper ) ;
this . config _ = config ;
2014-01-08 05:28:01 +00:00
this . connection _ = new cam . ServerConnection ( config ) ;
2013-06-11 09:50:56 +00:00
} ;
2014-01-08 05:28:01 +00:00
goog . inherits ( cam . BlobPage , goog . ui . Component ) ;
2013-06-11 09:50:56 +00:00
2014-01-08 05:28:01 +00:00
cam . BlobPage . prototype . thumbnailSize _ = 200 ;
2013-06-11 09:50:56 +00:00
2014-01-08 05:28:01 +00:00
cam . BlobPage . prototype . enterDocument = function ( ) {
2013-06-11 09:50:56 +00:00
var blobref = getBlobParam ( ) ;
if ( ! blobref ) {
alert ( "missing blob param in url" ) ;
return ;
}
var blobmeta = goog . dom . getElement ( 'blobmeta' ) ;
blobmeta . innerText = "(loading)" ;
var blobdescribe = goog . dom . getElement ( 'blobdescribe' ) ;
2014-01-07 04:52:30 +00:00
blobdescribe . innerHTML = "<a href='" + goog . uri . utils . appendPath ( this . config _ . searchRoot , 'camli/search/describe?blobref=' + blobref ) + "'>describe</a>" ;
2013-06-11 09:50:56 +00:00
this . describeBlob _ ( blobref ) ;
2011-05-30 23:41:56 +00:00
}
2014-01-08 05:28:01 +00:00
cam . BlobPage . prototype . describeBlob _ = function ( blobRef ) {
2014-01-07 04:52:30 +00:00
this . connection _ . describeWithThumbnails ( blobRef , 0 , goog . bind ( this . handleDescribeBlob _ , this ) ,
2013-06-11 09:50:56 +00:00
function ( msg ) {
alert ( "Error describing blob " + blobRef + ": " + msg ) ;
}
) ;
} ;
// TODO(mpl): improve blob_item and redo the following based on it.
2014-01-08 05:28:01 +00:00
cam . BlobPage . prototype . handleDescribeBlob _ = function ( bmap ) {
2013-06-11 09:50:56 +00:00
var blobmeta = goog . dom . getElement ( 'blobmeta' ) ;
var bd = goog . dom . getElement ( "blobdownload" ) ;
bd . innerHTML = "" ;
var blobref = getBlobParam ( ) ;
if ( ! blobref ) {
alert ( "no blobref?" ) ;
return ;
}
var binfo = bmap . meta [ blobref ] ;
if ( ! binfo ) {
blobmeta . innerHTML = "(not found)" ;
return ;
}
2013-07-08 16:19:39 +00:00
blobmeta . innerHTML = htmlEscape ( JSON . stringify ( binfo , null , 2 ) ) ;
2013-06-11 09:50:56 +00:00
if ( binfo . camliType || ( binfo . type && binfo . type . indexOf ( "text/" ) == 0 ) ) {
2013-12-17 04:27:49 +00:00
var conf = this . config _ ;
2013-06-11 09:50:56 +00:00
this . connection _ . getBlobContents ( blobref ,
goog . bind ( function ( data ) {
goog . dom . getElement ( "blobdata" ) . innerHTML = linkifyBlobRefs ( data ) ;
var bb = goog . dom . getElement ( 'blobbrowse' ) ;
if ( binfo . camliType != "directory" ) {
bb . style . visibility = 'hidden' ;
} else {
bb . innerHTML = "<a href='?d=" + blobref + "'>browse</a>" ;
}
if ( binfo . camliType == "file" ) {
2014-01-07 04:52:30 +00:00
// TODO(mpl): we can't get the thumnails url in a describe response because the server only gives it for a permanode. That's why we do this messy business here. Fix it server side.
2013-06-27 14:30:09 +00:00
finfo = JSON . parse ( data ) ;
bd . innerHTML = "<a href=''></a>" ;
2013-07-08 16:19:39 +00:00
var fileName = htmlEscape ( finfo . fileName ) || blobref ;
2013-06-27 14:30:09 +00:00
bd . firstChild . href = "./download/" + blobref + "/" + fileName ;
2014-01-07 04:52:30 +00:00
// If the mime type was not detected by magic pkg, we end up with an empty mimetype value in the indexer's fileinfo, hence no mimeType in the returned JSON.
2013-06-27 14:30:09 +00:00
if ( ! ! binfo . file . mimeType &&
binfo . file . mimeType . indexOf ( "image/" ) == 0 ) {
2014-01-07 04:52:30 +00:00
var thumbURL = "<img src='./thumbnail/" + blobref + "/" + fileName + "?mw=" + this . thumbnailSize _ + "&mh=" + this . thumbnailSize _ + "&tv=" + ( conf . thumbVersion || '' ) + "'>" ;
2013-06-27 14:30:09 +00:00
goog . dom . getElement ( "thumbnail" ) . innerHTML = thumbURL ;
} else {
goog . dom . getElement ( "thumbnail" ) . innerHTML = "" ;
2013-06-11 09:50:56 +00:00
}
2013-06-27 14:30:09 +00:00
goog . dom . setTextContent ( bd . firstChild , fileName ) ;
bd . innerHTML = "download: " + bd . innerHTML ;
2013-06-11 09:50:56 +00:00
}
} , this ) ,
alert
) ;
} else {
goog . dom . getElement ( "blobdata" ) . innerHTML = "<em>Unknown/binary data</em>" ;
}
2014-01-07 04:52:30 +00:00
bd . innerHTML = "<a href='" + goog . uri . utils . appendPath ( this . config _ . blobRoot , "camli/" + blobref ) + "'>download</a>" ;
2013-06-11 09:50:56 +00:00
if ( binfo . camliType && binfo . camliType == "permanode" ) {
goog . dom . getElement ( "editspan" ) . style . display = "inline" ;
goog . dom . getElement ( "editlink" ) . href = "./?p=" + blobref ;
2011-06-03 22:23:23 +00:00
2013-06-11 09:50:56 +00:00
var claims = goog . dom . getElement ( "claimsdiv" ) ;
claims . style . visibility = "" ;
this . connection _ . permanodeClaims ( blobref ,
function ( data ) {
goog . dom . getElement ( "claims" ) . innerHTML = linkifyBlobRefs ( JSON . stringify ( data , null , 2 ) ) ;
} ,
function ( msg ) {
alert ( msg ) ;
}
) ;
}
2011-05-30 23:41:56 +00:00
}
2013-07-08 16:19:39 +00:00
function htmlEscape ( data ) {
return goog . string . htmlEscape ( data ) ;
}
2013-06-11 09:50:56 +00:00
function linkifyBlobRefs ( schemaBlob ) {
var re = /(\w{3,6}-[a-f0-9]{30,})/g ;
2013-07-08 16:19:39 +00:00
return htmlEscape ( schemaBlob ) . replace ( re , "<a href='./?b=$1'>$1</a>" ) ;
2013-06-11 09:50:56 +00:00
} ;
// Gets the |p| query parameter, assuming that it looks like a blobref.
function getBlobParam ( ) {
var blobRef = getQueryParam ( 'b' ) ;
return ( blobRef && isPlausibleBlobRef ( blobRef ) ) ? blobRef : null ;
2011-05-30 23:41:56 +00:00
}
2013-06-11 09:50:56 +00:00
// TODO(mpl): move it to a common place (used by permanode.js too).
2014-01-07 04:52:30 +00:00
// I suppose we could go back to depending on camli.js for these little helpers only. Returns the first value from the query string corresponding to |key|. Returns null if the key isn't present.
2013-06-11 09:50:56 +00:00
getQueryParam = function ( key ) {
var params = document . location . search . substring ( 1 ) . split ( '&' ) ;
for ( var i = 0 ; i < params . length ; ++ i ) {
var parts = params [ i ] . split ( '=' ) ;
if ( parts . length == 2 && decodeURIComponent ( parts [ 0 ] ) == key )
return decodeURIComponent ( parts [ 1 ] ) ;
}
return null ;
} ;
// Returns true if the passed-in string might be a blobref.
isPlausibleBlobRef = function ( blobRef ) {
return /^\w+-[a-f0-9]+$/ . test ( blobRef ) ;
} ;