Skip to content

Commit

Permalink
Merge pull request #103 from gdcc/develop
Browse files Browse the repository at this point in the history
Updating master w/recent changes
  • Loading branch information
qqmyers authored Feb 18, 2025
2 parents a2120cf + 6c3e560 commit a214203
Show file tree
Hide file tree
Showing 7 changed files with 1,990 additions and 798 deletions.
41 changes: 41 additions & 0 deletions 6.1curlcommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,47 @@ curl -X POST -H 'Content-type: application/json' http://localhost:8080/api/admin
]
}'
```
```bash
curl -X POST -H 'Content-type: application/json' http://localhost:8080/api/admin/externalTools -d \
'{
"displayName":"Play Audio",
"description":"Listen to an audio file.",
"toolName":"audioPreviewer",
"scope":"file",
"types":["preview"],
"toolUrl":"https://gdcc.github.io/dataverse-previewers/previewers/v1.4/AudioPreview.html",
"toolParameters": {
"queryParameters":[
{"fileid":"{fileId}"},
{"siteUrl":"{siteUrl}"},
{"datasetid":"{datasetId}"},
{"datasetversion":"{datasetVersion}"},
{"locale":"{localeCode}"}
]
},
"contentType":"audio/mp4",
"allowedApiCalls": [
{
"name": "retrieveFileContents",
"httpMethod": "GET",
"urlTemplate": "/api/v1/access/datafile/{fileId}?gbrecs=true",
"timeOut": 3600
},
{
"name": "downloadFile",
"httpMethod": "GET",
"urlTemplate": "/api/v1/access/datafile/{fileId}?gbrecs=false",
"timeOut": 3600
},
{
"name": "getDatasetVersionMetadata",
"httpMethod": "GET",
"urlTemplate": "/api/v1/datasets/{datasetId}/versions/{datasetVersion}",
"timeOut": 3600
}
]
}'
```

```bash
curl -X POST -H 'Content-type: application/json' http://localhost:8080/api/admin/externalTools -d \
Expand Down
22 changes: 20 additions & 2 deletions localinstall.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@ source replace_js.sh
cat urls_js.txt

echo Downloading local copies of remote CSS files:
sed -n 's/.*<link.*href="\(http[^"]*\)".*/\1/p' *.html | sort -u | sed -n 's/^\(.*\/\)*\(.*\)/sed -i \x27s,\0\,lib\/\2,\x27 *.html/p' > replace_css.sh
sed -n 's/.*<link.*href="\(http[^"]*\)".*/\1/p' *.html | sort -u | sed -n 's/^\(.*\/\)*\(.*\)/sed -i \x27s,\0\,css\/\2,\x27 *.html/p' > replace_css.sh
sed -n 's/.*<link.*href="\(http[^"]*\)".*/\1/p' *.html | sort -u > urls_css.txt
source replace_css.sh
cat urls_css.txt


echo Downloading local copies of remote fonts:
fonturl=`grep -m 1 https://stackpath.bootstrapcdn.com/ replace_css.sh | cut -d',' -f 2 | sed s,css/bootstrap.min.css,fonts,`
echo $fonturl/glyphicons-halflings-regular.eot > urls_fonts.txt
echo $fonturl/glyphicons-halflings-regular.svg >> urls_fonts.txt
echo $fonturl/glyphicons-halflings-regular.ttf >> urls_fonts.txt
echo $fonturl/glyphicons-halflings-regular.woff >> urls_fonts.txt
echo $fonturl/glyphicons-halflings-regular.woff2 >> urls_fonts.txt

if [ ! -d ./lib ]; then
mkdir ./lib
fi
Expand All @@ -38,6 +47,14 @@ while read url; do
wget --quiet $url
done < "../urls_js.txt"

cd ".."
if [ ! -d ./fonts ]; then
mkdir ./fonts
fi
cd ./fonts
while read url; do
wget --quiet $url
done < "../urls_fonts.txt"

cd ".."
if [ ! -d ./css ]; then
Expand All @@ -63,8 +80,9 @@ fi
echo Cleaning Up...
rm urls_js.txt
rm urls_css.txt
rm urls_fonts.txt
rm replace_js.sh
rm replace_css.sh

echo Done
exit 0
exit 0
3 changes: 2 additions & 1 deletion previewers/betatest/css/preview.css
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ This has been done for the Rich Html Previewer, which also has the reverse issue
min-width: 600px;
display: table;
max-width: 100%;
max-height: 100%;
}

#logo {
Expand Down Expand Up @@ -204,4 +205,4 @@ This has been done for the Rich Html Previewer, which also has the reverse issue
max-height: 500px;
word-break: break-all;
overflow-y: auto;
}
}
6 changes: 4 additions & 2 deletions previewers/betatest/js/spreadsheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ function writeContent(fileUrl, file, title, authors) {
var csv = e.target.result;
var data = Papa.parse(csv, {
header: true,
skipEmptyLines: true
skipEmptyLines: true,
quoteChar:'"',
delimitersToGuess:['\t',',']
})

handsontableContainer.innerHTML = '';
Expand All @@ -42,4 +44,4 @@ function writeContent(fileUrl, file, title, authors) {
request.send();
}



116 changes: 115 additions & 1 deletion previewers/betatest/js/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,119 @@ function translateBaseHtmlPage() {

function writeContent(fileUrl, file, title, authors) {
addStandardPreviewHeader(file, title, authors);
$(".preview").append($("<video/>").prop("controls",true).append($('<source/>').attr("src",fileUrl)));

const queryParams = new URLSearchParams(window.location.search.substring(1));
const id = queryParams.get("datasetid");
const siteUrl = queryParams.get("siteUrl");
const apiKey = queryParams.get("key");
if (!siteUrl || !id) {
// fallback to simple video element in case of signed URLs
$(".preview").append($("<video/>") .prop("controls", true) .append($('<source/>').attr("src", fileUrl)))
return;
}
const versionUrl = `${siteUrl}/api/datasets/${id}/versions/`
+ queryParams.get("datasetversion")
+ (apiKey?`?key=${apiKey}`:'');
const videoId =queryParams.get("fileid") * 1; // converted to number
const userLanguages = [...navigator.languages];
const locale = queryParams.get("locale");
if (locale && !userLanguages.includes(locale)) {
userLanguages.unshift(locale); // add query argument as first element
}

$.ajax({
type: 'GET',
dataType: 'json',
crosssite: true,
url: versionUrl,
success: function(data, status) {
appendVideoElements(fileUrl, videoId, data.data.files, siteUrl, userLanguages, apiKey);
},
error: function(response, status, error) {
// fallback to simple video element
$(".preview").append($("<video/>") .prop("controls", true) .append($('<source/>').attr("src", fileUrl)))
}
});
}

function appendVideoElements(fileUrl, videoId, files, siteUrl, userLanguages, apiKey) {

const baseName = files // the video file name without extension
.find(item => item.dataFile.id === videoId)
.label.replace(/\.[a-z0-9]+$/i,'');

// find labels like "baseName.en.vtt", "baseName.de-CH.vtt" or "baseName.vtt"
const regex = new RegExp(`${baseName}(\\.([-a-z]+))?\\.vtt$`, 'i')

// create a map of URLs with their (optional) language
let trackUrlWithoutLang = null;
const subtitles = files
.filter(item => regex.test(item.label))
.reduce((map, item) => {
const lang = item.label.match(regex)[2];
const url = apiKey
?`${siteUrl}/api/access/datafile/${item.dataFile.id}?gbrecs=true&amp;key=${apiKey}`
:`${siteUrl}/api/access/datafile/${item.dataFile.id}?gbrecs=true`
map.set(url, lang);
if (!lang) {
trackUrlWithoutLang = url;
}
return map;
}, new Map());

// sort subtitles by language value, 'de-CH' before 'de'
const sortedSubtitles = new Map([...subtitles.entries()].sort((a, b) => {
if (!a[1]) return 1;
if (!b[1]) return -1;
if (a[1].startsWith(b[1])) return -1;
if (b[1].startsWith(a[1])) return 1;
return a[1].localeCompare(b[1]);
}));

// determine default track
let defaultTrackUrl = null;
loop: for (const lang of userLanguages) {
// match user preferences with available subtitles
for (const [url, trackLang] of sortedSubtitles) {
if (trackLang) {
if (trackLang === lang || trackLang.startsWith(lang.replace(/-.*/, ''))) {
defaultTrackUrl = url;
break loop;
}
}
}
}
if (!defaultTrackUrl && subtitles) {
// no match found, use track without language
defaultTrackUrl = trackUrlWithoutLang;
}
if (!defaultTrackUrl && subtitles.size === 1) {
// no match found, and only one track available, use that one
defaultTrackUrl = subtitles.keys().next().value;
}

const videoElement = $("<video/>")
.attr("crossorigin", "anonymous") // required for getting subtitles from object storage
.prop("controls", true)
.append($('<source/>').attr("src", fileUrl));

sortedSubtitles.forEach((trackLang, url) => {
const trackElement = $('<track/>')
.attr("kind", "subtitles")
.attr("src", url);
if (trackLang) {
trackElement
.attr("label", trackLang)
.attr("srclang", trackLang);
} else {
trackElement.attr("label", "default");
}
console.log("url: ", url, "defaultTrackUrl: ", defaultTrackUrl);
if (url === defaultTrackUrl) {
trackElement.attr("default", true);
}
videoElement.append(trackElement);
});

$(".preview").append(videoElement);
}
Loading

0 comments on commit a214203

Please sign in to comment.