github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/server/camlistored/ui/blobinfo.js (about) 1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 goog.provide('cam.BlobPage'); 18 19 goog.require('goog.dom'); 20 goog.require('goog.ui.Component'); 21 22 goog.require('cam.ServerConnection'); 23 24 // @param {cam.ServerType.DiscoveryDocument} config Global config of the current server this page is being rendered for. 25 // @param {goog.dom.DomHelper=} opt_domHelper DOM helper to use. 26 cam.BlobPage = function(config, opt_domHelper) { 27 goog.base(this, opt_domHelper); 28 29 this.config_ = config; 30 this.connection_ = new cam.ServerConnection(config); 31 }; 32 goog.inherits(cam.BlobPage, goog.ui.Component); 33 34 cam.BlobPage.prototype.thumbnailSize_ = 200; 35 36 cam.BlobPage.prototype.enterDocument = function() { 37 var blobref = getBlobParam(); 38 if (!blobref) { 39 alert("missing blob param in url"); 40 return; 41 } 42 var blobmeta = goog.dom.getElement('blobmeta'); 43 blobmeta.innerText = "(loading)"; 44 45 var blobdescribe = goog.dom.getElement('blobdescribe'); 46 blobdescribe.innerHTML = "<a href='" + goog.uri.utils.appendPath(this.config_.searchRoot, 'camli/search/describe?blobref=' + blobref) + "'>describe</a>"; 47 this.describeBlob_(blobref); 48 49 } 50 51 cam.BlobPage.prototype.describeBlob_ = function(blobRef) { 52 this.connection_.describeWithThumbnails(blobRef, 0, goog.bind(this.handleDescribeBlob_, this), 53 function(msg) { 54 alert("Error describing blob " + blobRef + ": " + msg); 55 } 56 ); 57 }; 58 59 // TODO(mpl): improve blob_item and redo the following based on it. 60 cam.BlobPage.prototype.handleDescribeBlob_ = function(bmap) { 61 var blobmeta = goog.dom.getElement('blobmeta'); 62 var bd = goog.dom.getElement("blobdownload"); 63 bd.innerHTML = ""; 64 var blobref = getBlobParam(); 65 if (!blobref) { 66 alert("no blobref?"); 67 return; 68 } 69 var binfo = bmap.meta[blobref]; 70 if (!binfo) { 71 blobmeta.innerHTML = "(not found)"; 72 return; 73 } 74 blobmeta.innerHTML = htmlEscape(JSON.stringify(binfo, null, 2)); 75 if (binfo.camliType || (binfo.type && binfo.type.indexOf("text/") == 0)) { 76 var conf = this.config_; 77 this.connection_.getBlobContents(blobref, 78 goog.bind(function(data) { 79 goog.dom.getElement("blobdata").innerHTML = linkifyBlobRefs(data); 80 var bb = goog.dom.getElement('blobbrowse'); 81 if (binfo.camliType != "directory") { 82 bb.style.visibility = 'hidden'; 83 } else { 84 bb.innerHTML = "<a href='?d=" + blobref + "'>browse</a>"; 85 } 86 if (binfo.camliType == "file") { 87 // 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. 88 finfo = JSON.parse(data); 89 bd.innerHTML = "<a href=''></a>"; 90 var fileName = htmlEscape(finfo.fileName) || blobref; 91 bd.firstChild.href = "./download/" + blobref + "/" + fileName; 92 // 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. 93 if (!!binfo.file.mimeType && 94 binfo.file.mimeType.indexOf("image/") == 0) { 95 var thumbURL = "<img src='./thumbnail/" + blobref + "/" + fileName + "?mw=" + this.thumbnailSize_ + "&mh=" + this.thumbnailSize_ + "&tv=" + (conf.thumbVersion || '') + "'>"; 96 goog.dom.getElement("thumbnail").innerHTML = thumbURL; 97 } else { 98 goog.dom.getElement("thumbnail").innerHTML = ""; 99 } 100 goog.dom.setTextContent(bd.firstChild, fileName); 101 bd.innerHTML = "download: " + bd.innerHTML; 102 } 103 }, this), 104 alert 105 ); 106 } else { 107 goog.dom.getElement("blobdata").innerHTML = "<em>Unknown/binary data</em>"; 108 } 109 bd.innerHTML = "<a href='" + goog.uri.utils.appendPath(this.config_.blobRoot, "camli/" + blobref) + "'>download</a>"; 110 111 if (binfo.camliType && binfo.camliType == "permanode") { 112 goog.dom.getElement("editspan").style.display = "inline"; 113 goog.dom.getElement("editlink").href = "./?p=" + blobref; 114 115 var claims = goog.dom.getElement("claimsdiv"); 116 claims.style.visibility = ""; 117 this.connection_.permanodeClaims(blobref, 118 function(data) { 119 goog.dom.getElement("claims").innerHTML = linkifyBlobRefs(JSON.stringify(data, null, 2)); 120 }, 121 function(msg) { 122 alert(msg); 123 } 124 ); 125 } 126 } 127 128 function htmlEscape(data) { 129 return goog.string.htmlEscape(data); 130 } 131 132 function linkifyBlobRefs(schemaBlob) { 133 var re = /(\w{3,6}-[a-f0-9]{30,})/g; 134 return htmlEscape(schemaBlob).replace(re, "<a href='./?b=$1'>$1</a>"); 135 }; 136 137 // Gets the |p| query parameter, assuming that it looks like a blobref. 138 function getBlobParam() { 139 var blobRef = getQueryParam('b'); 140 return (blobRef && isPlausibleBlobRef(blobRef)) ? blobRef : null; 141 } 142 143 // TODO(mpl): move it to a common place (used by permanode.js too). 144 // 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. 145 getQueryParam = function(key) { 146 var params = document.location.search.substring(1).split('&'); 147 for (var i = 0; i < params.length; ++i) { 148 var parts = params[i].split('='); 149 if (parts.length == 2 && decodeURIComponent(parts[0]) == key) 150 return decodeURIComponent(parts[1]); 151 } 152 return null; 153 }; 154 155 // Returns true if the passed-in string might be a blobref. 156 isPlausibleBlobRef = function(blobRef) { 157 return /^\w+-[a-f0-9]+$/.test(blobRef); 158 };