github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/packaging/ci/bucketsite/list.js (about) 1 /* 2 * Copyright 2012-2016 Rufus Pollock. 3 * 4 * Licensed under the MIT license: 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 * 8 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 * 12 * https://github.com/rgrp/s3-bucket-listing 13 */ 14 if (typeof S3BL_IGNORE_PATH == 'undefined' || S3BL_IGNORE_PATH!=true) { 15 var S3BL_IGNORE_PATH = false; 16 } 17 18 if (typeof BUCKET_URL == 'undefined') { 19 var BUCKET_URL = location.protocol + '//' + location.hostname; 20 } 21 22 if (typeof BUCKET_NAME != 'undefined') { 23 // if bucket_url does not start with bucket_name, 24 // assume path-style url 25 if (!~BUCKET_URL.indexOf(location.protocol + '//' + BUCKET_NAME)) { 26 BUCKET_URL += '/' + BUCKET_NAME; 27 } 28 } 29 30 if (typeof BUCKET_WEBSITE_URL == 'undefined') { 31 var BUCKET_WEBSITE_URL = BUCKET_URL; 32 } 33 34 if (typeof S3B_ROOT_DIR == 'undefined') { 35 var S3B_ROOT_DIR = ''; 36 } 37 38 jQuery(function($) { 39 getS3Data(); 40 }); 41 42 function getS3Data(marker, html) { 43 var s3_rest_url = createS3QueryUrl(marker); 44 // set loading notice 45 $('#listing').html('<img src="/assets/ajaxload-circle.gif" />'); 46 $.get(s3_rest_url) 47 .done(function(data) { 48 // clear loading notice 49 $('#listing').html(''); 50 var xml = $(data); 51 var info = getInfoFromS3Data(xml); 52 53 buildNavigation(info) 54 55 html = typeof html !== 'undefined' ? html + prepareTable(info) : prepareTable(info); 56 if (info.nextMarker != "null") { 57 getS3Data(info.nextMarker, html); 58 } else { 59 document.getElementById('listing').innerHTML = '<pre>' + html + '</pre>'; 60 } 61 }) 62 .fail(function(error) { 63 console.error(error); 64 $('#listing').html('<strong>Error: ' + error + '</strong>'); 65 }); 66 } 67 68 function buildNavigation(info) { 69 var root = '<a href="?prefix=">' + BUCKET_WEBSITE_URL + '</a> / ' 70 if (info.prefix) { 71 var processedPathSegments = '' 72 var content = $.map(info.prefix.split('/'), function(pathSegment){ 73 processedPathSegments = processedPathSegments + encodeURIComponent(pathSegment) + '/' 74 return '<a href="?prefix=' + processedPathSegments + '">' + pathSegment + '</a>' 75 }); 76 $('#navigation').html(root + content.join(' / ')) 77 } else { 78 $('#navigation').html(root) 79 } 80 } 81 82 function createS3QueryUrl(marker) { 83 var s3_rest_url = BUCKET_URL; 84 s3_rest_url += '?delimiter=/'; 85 86 // 87 // Handling paths and prefixes: 88 // 89 // 1. S3BL_IGNORE_PATH = false 90 // Uses the pathname 91 // {bucket}/{path} => prefix = {path} 92 // 93 // 2. S3BL_IGNORE_PATH = true 94 // Uses ?prefix={prefix} 95 // 96 // Why both? Because we want classic directory style listing in normal 97 // buckets but also allow deploying to non-buckets 98 // 99 100 var rx = '.*[?&]prefix=' + S3B_ROOT_DIR + '([^&]+)(&.*)?$'; 101 var prefix = ''; 102 if (S3BL_IGNORE_PATH==false) { 103 var prefix = location.pathname.replace(/^\//, S3B_ROOT_DIR); 104 } 105 var match = location.search.match(rx); 106 if (match) { 107 prefix = S3B_ROOT_DIR + match[1]; 108 } else { 109 if (S3BL_IGNORE_PATH) { 110 var prefix = S3B_ROOT_DIR; 111 } 112 } 113 if (prefix) { 114 // make sure we end in / 115 var prefix = prefix.replace(/\/$/, '') + '/'; 116 s3_rest_url += '&prefix=' + prefix; 117 } 118 if (marker) { 119 s3_rest_url += '&marker=' + marker; 120 } 121 return s3_rest_url; 122 } 123 124 function getInfoFromS3Data(xml) { 125 var files = $.map(xml.find('Contents'), function(item) { 126 item = $(item); 127 return { 128 Key: item.find('Key').text(), 129 LastModified: item.find('LastModified').text(), 130 Size: bytesToHumanReadable(item.find('Size').text()), 131 Type: 'file' 132 } 133 }); 134 var directories = $.map(xml.find('CommonPrefixes'), function(item) { 135 item = $(item); 136 return { 137 Key: item.find('Prefix').text(), 138 LastModified: '', 139 Size: '0', 140 Type: 'directory' 141 } 142 }); 143 if ($(xml.find('IsTruncated')[0]).text() == 'true') { 144 var nextMarker = $(xml.find('NextMarker')[0]).text(); 145 } else { 146 var nextMarker = null; 147 } 148 return { 149 files: files, 150 directories: directories, 151 prefix: $(xml.find('Prefix')[0]).text(), 152 nextMarker: encodeURIComponent(nextMarker) 153 } 154 } 155 156 // info is object like: 157 // { 158 // files: .. 159 // directories: .. 160 // prefix: ... 161 // } 162 function prepareTable(info) { 163 var files = info.files.concat(info.directories) 164 , prefix = info.prefix 165 ; 166 var cols = [ 45, 30, 15 ]; 167 var content = []; 168 content.push(padRight('Last Modified', cols[1]) + ' ' + padRight('Size', cols[2]) + 'Key \n'); 169 content.push(new Array(cols[0] + cols[1] + cols[2] + 4).join('-') + '\n'); 170 171 // add the ../ at the start of the directory listing, unless when at the root dir already 172 if (prefix && prefix !== S3B_ROOT_DIR) { 173 var up = prefix.replace(/\/$/, '').split('/').slice(0, -1).concat('').join('/'), // one directory up 174 item = { 175 Key: up, 176 LastModified: '', 177 Size: '', 178 keyText: '../', 179 href: S3BL_IGNORE_PATH ? '?prefix=' + up : '../' 180 }, 181 row = renderRow(item, cols); 182 content.push(row + '\n'); 183 } 184 185 jQuery.each(files, function(idx, item) { 186 // strip off the prefix 187 item.keyText = item.Key.substring(prefix.length); 188 if (item.Type === 'directory') { 189 if (S3BL_IGNORE_PATH) { 190 item.href = location.protocol + '//' + location.hostname + location.pathname + '?prefix=' + item.Key; 191 } else { 192 item.href = item.keyText; 193 } 194 } else { 195 item.href = BUCKET_WEBSITE_URL + '/' + encodeURIComponent(item.Key); 196 item.href = item.href.replace(/%2F/g, '/'); 197 } 198 var row = renderRow(item, cols); 199 content.push(row + '\n'); 200 }); 201 202 return content.join(''); 203 } 204 205 function renderRow(item, cols) { 206 var row = ''; 207 row += padRight(item.LastModified, cols[1]) + ' '; 208 row += padRight(item.Size, cols[2]); 209 row += '<a href="' + item.href + '">' + item.keyText + '</a>'; 210 return row; 211 } 212 213 function padRight(padString, length) { 214 var str = padString.slice(0, length-3); 215 if (padString.length > str.length) { 216 str += '...'; 217 } 218 while (str.length < length) { 219 str = str + ' '; 220 } 221 return str; 222 } 223 224 function bytesToHumanReadable(sizeInBytes) { 225 var i = -1; 226 var units = [' kB', ' MB', ' GB']; 227 do { 228 sizeInBytes = sizeInBytes / 1024; 229 i++; 230 } while (sizeInBytes > 1024); 231 return Math.max(sizeInBytes, 0.1).toFixed(1) + units[i]; 232 }