-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(thumbnail): Polyfilling async thumbnail for being compatible
Signed-off-by: Vincent Boutour <[email protected]>
- Loading branch information
Showing
12 changed files
with
170 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
# scripts | ||
scripts/ | ||
/scripts/ | ||
|
||
# Golang | ||
bin/ | ||
release/ | ||
coverage.* | ||
.env | ||
|
||
# NPM | ||
node_modules/ | ||
|
||
# Fibr | ||
.fibr/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,7 +72,7 @@ func main() { | |
loggerConfig := logger.Flags(fs, "logger") | ||
tracerConfig := tracer.Flags(fs, "tracer") | ||
prometheusConfig := prometheus.Flags(fs, "prometheus", flags.NewOverride("Gzip", false)) | ||
owaspConfig := owasp.Flags(fs, "", flags.NewOverride("FrameOptions", "SAMEORIGIN"), flags.NewOverride("Csp", "default-src 'self'; base-uri 'self'; script-src 'httputils-nonce' unpkg.com/[email protected]/dist/ unpkg.com/[email protected]/; style-src 'httputils-nonce' unpkg.com/[email protected]/dist/ unpkg.com/[email protected]/; img-src 'self' data: a.tile.openstreetmap.org b.tile.openstreetmap.org c.tile.openstreetmap.org")) | ||
owaspConfig := owasp.Flags(fs, "", flags.NewOverride("FrameOptions", "SAMEORIGIN"), flags.NewOverride("Csp", "default-src 'self'; base-uri 'self'; script-src 'self' 'httputils-nonce' unpkg.com/[email protected]/dist/ unpkg.com/[email protected]/; style-src 'httputils-nonce' unpkg.com/[email protected]/dist/ unpkg.com/[email protected]/; img-src 'self' data: a.tile.openstreetmap.org b.tile.openstreetmap.org c.tile.openstreetmap.org")) | ||
|
||
basicConfig := basicMemory.Flags(fs, "auth", flags.NewOverride("Profiles", "1:admin")) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// from https://developers.google.com/speed/webp/faq#how_can_i_detect_browser_support_for_webp | ||
async function isWebPCompatible() { | ||
const animatedImage = | ||
'UklGRlIAAABXRUJQVlA4WAoAAAASAAAAAAAAAAAAQU5JTQYAAAD/////AABBTk1GJgAAAAAAAAAAAAAAAAAAAGQAAABWUDhMDQAAAC8AAAAQBxAREYiI/gcA'; | ||
|
||
return new Promise((resolve, reject) => { | ||
var image = new Image(); | ||
image.onload = () => { | ||
if (image.width > 0 && image.height > 0) { | ||
resolve(); | ||
} else { | ||
reject(); | ||
} | ||
}; | ||
|
||
image.onerror = reject; | ||
image.src = `data:image/webp;base64,${animatedImage}`; | ||
}); | ||
} | ||
|
||
// From https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/read#example_2_-_handling_text_line_by_line | ||
async function* readLineByLine(response) { | ||
const utf8Decoder = new TextDecoder('utf-8'); | ||
const reader = response.body.getReader(); | ||
let { value: chunk, done: readerDone } = await reader.read(); | ||
chunk = chunk ? utf8Decoder.decode(chunk, { stream: true }) : ''; | ||
|
||
let re = /\r\n|\n|\r/gm; | ||
let startIndex = 0; | ||
|
||
for (;;) { | ||
const result = re.exec(chunk); | ||
if (!result) { | ||
if (readerDone) { | ||
break; | ||
} | ||
|
||
const remainder = chunk.substr(startIndex); | ||
({ value: chunk, done: readerDone } = await reader.read()); | ||
chunk = | ||
remainder + (chunk ? utf8Decoder.decode(chunk, { stream: true }) : ''); | ||
startIndex = re.lastIndex = 0; | ||
continue; | ||
} | ||
|
||
yield chunk.substring(startIndex, result.index); | ||
startIndex = re.lastIndex; | ||
} | ||
|
||
if (startIndex < chunk.length) { | ||
yield chunk.substr(startIndex); | ||
} | ||
} | ||
|
||
/** | ||
* Async image loading | ||
*/ | ||
async function fetchThumbnail() { | ||
fetchURL = document.location.search; | ||
if (fetchURL.includes('?')) { | ||
fetchURL += '&thumbnail'; | ||
} else { | ||
fetchURL += '?thumbnail'; | ||
} | ||
|
||
const response = await fetch(fetchURL, { credentials: 'same-origin' }); | ||
|
||
if (response.status >= 400) { | ||
throw new Error('unable to load thumbnails'); | ||
} | ||
|
||
for await (let line of readLineByLine(response)) { | ||
const parts = line.split(','); | ||
if (parts.length != 2) { | ||
console.error('invalid line for thumbnail:', line); | ||
continue; | ||
} | ||
|
||
const picture = document.getElementById(`picture-${parts[0]}`); | ||
if (!picture) { | ||
continue; | ||
} | ||
|
||
const img = new Image(); | ||
img.src = `data:image/webp;base64,${parts[1]}`; | ||
img.alt = picture.dataset.alt; | ||
img.dataset.src = picture.dataset.src; | ||
img.classList.add('thumbnail', 'full', 'block'); | ||
|
||
replaceContent(picture, img); | ||
} | ||
} | ||
|
||
window.addEventListener( | ||
'load', | ||
async () => { | ||
const thumbnailsElem = document.querySelectorAll('[data-thumbnail]'); | ||
if (!thumbnailsElem) { | ||
return; | ||
} | ||
|
||
try { | ||
await isWebPCompatible(); | ||
} catch (e) { | ||
console.error('Your browser is not compatible with WebP format.', e); | ||
thumbnailsElem.forEach(displayNoThumbnail); | ||
return; | ||
} | ||
|
||
thumbnailsElem.forEach((picture) => { | ||
replaceContent(picture, generateThrobber(['throbber-white'])); | ||
}); | ||
|
||
try { | ||
await fetchThumbnail(); | ||
window.dispatchEvent(new Event('thumbnail-done')); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}, | ||
false, | ||
); |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.