Testing assets + refactoring

pull/3/head
Toni Melisma 3 years ago
parent 77209b2203
commit b10c8c3bd6
No known key found for this signature in database
GPG Key ID: FFF9A7EDDEA34756

3
.gitignore vendored

@ -6,9 +6,6 @@
*.dylib
# temp media files
*.mp4
*.jpg
*.heic
.DS_Store
# Test binary, built with `go test -c`

@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}",
"args": [
"../../testing/source/",
"../../testing/gallery/"
]
}
]
}

@ -14,7 +14,6 @@ import (
"github.com/cheggaaa/pb/v3"
"github.com/davidbyttow/govips/v2/vips"
"github.com/kr/pretty"
"github.com/alexflint/go-arg"
)
@ -365,11 +364,14 @@ func compareDirectoryTrees(source *directory, gallery *directory, config configu
source.exists = true
gallery.exists = true
// TODO fix bug where two source files with different extensions clash
// Iterate over each file in source directory to see whether it exists in gallery
for i, sourceFile := range source.files {
sourceFileBasename := stripExtension(sourceFile.name)
fmt.Println("checking:", sourceFile.absPath, sourceFileBasename)
var thumbnailFile, fullsizeFile, originalFile *file
fmt.Println(sourceFile, thumbnailFile, fullsizeFile, originalFile)
// Go through all subdirectories, and check the ones that match
// the thumbnail, full-size or original subdirectories
@ -378,13 +380,13 @@ func compareDirectoryTrees(source *directory, gallery *directory, config configu
for _, outputFile := range subDir.files {
outputFileBasename := stripExtension(outputFile.name)
if sourceFileBasename == outputFileBasename {
fmt.Println("match:", sourceFileBasename, outputFileBasename, thumbnailFile)
thumbnailFile = &outputFile
thumbnailFile.exists = true
fmt.Println("match2:", thumbnailFile)
}
}
}
if subDir.name == config.files.fullsizeDir {
} else if subDir.name == config.files.fullsizeDir {
for _, outputFile := range subDir.files {
outputFileBasename := stripExtension(outputFile.name)
if sourceFileBasename == outputFileBasename {
@ -392,9 +394,7 @@ func compareDirectoryTrees(source *directory, gallery *directory, config configu
fullsizeFile.exists = true
}
}
}
if subDir.name == config.files.originalDir {
} else if subDir.name == config.files.originalDir {
for _, outputFile := range subDir.files {
outputFileBasename := stripExtension(outputFile.name)
if sourceFileBasename == outputFileBasename {
@ -410,8 +410,12 @@ func compareDirectoryTrees(source *directory, gallery *directory, config configu
// Otherwise we overwrite gallery files in case source file's been updated since the thumbnail
// was created.
if thumbnailFile != nil && fullsizeFile != nil && originalFile != nil {
if !thumbnailFile.modTime.Before(sourceFile.modTime) {
fmt.Println("three matches", sourceFile.name)
fmt.Println(sourceFile, thumbnailFile, fullsizeFile, originalFile)
if thumbnailFile.modTime.After(sourceFile.modTime) {
source.files[i].exists = true
} else {
fmt.Println("source file (1) modified after thumbnail (2):", sourceFile.absPath, sourceFile.modTime.String(), " --- ", thumbnailFile.absPath, thumbnailFile.modTime.String())
}
}
}
@ -589,12 +593,14 @@ func transformImage(source string, fullsizeDestination string, thumbnailDestinat
return
}
// TODO document below
// Calculate the scaling factor used to make the image smaller
scale := float64(config.media.fullsizeMaxWidth) / float64(image.Width())
if (scale * float64(image.Height())) > float64(config.media.fullsizeMaxHeight) {
// If the image is tall vertically, use height instead of width to recalculate scaling factor
scale = float64(config.media.fullsizeMaxHeight) / float64(image.Height())
}
// TODO don't enlarge the file by accident
err = image.Resize(scale, vips.KernelAuto)
if err != nil {
log.Println("couldn't resize full-size image:", source, err.Error())
@ -632,6 +638,8 @@ func transformImage(source string, fullsizeDestination string, thumbnailDestinat
log.Println("couldn't write thumbnail image:", thumbnailDestination, err.Error())
return
}
fmt.Println("wrote thumbnailfile:", thumbnailDestination)
} else {
log.Println("Can't figure out what format to convert full size image to:", source)
exit(1)
@ -717,6 +725,7 @@ func createMedia(source directory, gallerySubdirectory string, dryRun bool, conf
for _, file := range source.files {
if !file.exists {
sourceFilepath := filepath.Join(source.absPath, file.name)
fmt.Println("updating:", sourceFilepath)
thumbnailFilename := stripExtension(file.name) + config.files.imageExtension
var fullsizeFilename string
if isImageFile(file.name) {
@ -859,10 +868,4 @@ func main() {
} else {
log.Println("Gallery already up to date!")
}
log.Println("source:")
pretty.Print(source)
log.Println("gallery:")
pretty.Print(gallery)
}

@ -7,7 +7,6 @@ require (
github.com/cheggaaa/pb/v3 v3.0.6
github.com/davidbyttow/govips/v2 v2.5.0
github.com/fatih/color v1.10.0 // indirect
github.com/kr/pretty v0.2.1
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/rivo/uniseg v0.2.0 // indirect

@ -15,8 +15,6 @@ github.com/davidbyttow/govips/v2 v2.5.0/go.mod h1:goq38QD8XEMz2aWEeucEZqRxAWsemI
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=

@ -1 +0,0 @@
/home/toni/go/src/github.com/tonimelisma/fastgallery/testing/source/2021-02-25 15.40.44.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

@ -1,33 +0,0 @@
.box {
cursor: pointer;
}
#modalMedia {
max-width: 100%;
max-height: calc(100% - 74px);
}
.modalImage {
object-fit: scale-down;
max-width: 100%;
}
video {
max-width: 100%;
max-height: 100%;
}
#modalDownload {
color: inherit;
}
#modalHeader,
#modalFooter {
height: 37px;
width: 360px;
}
.modalControl:hover,
.modalControl:focus {
background-color: rgba(0, 0, 0, 0.2);
}

@ -1,150 +0,0 @@
// check that the HTML page including us has set the pictures array
if (typeof pictures == 'undefined') {
throw new Error("pictures array not defined")
}
// Hard-coded configuration
const videoExtension = "mp4"
const videoMIMEType = "video/mp4"
// global variable maintains currently shown picture number (pictures[] array)
var currentPicture
// create hover effect shadow for all box elements
const hoverOnBox = (event) => {
event.target.classList.remove("box-shadow")
event.target.classList.remove("border-gray")
event.target.classList.add("box-shadow-large")
event.target.classList.add("border-gray-dark")
}
const hoverOffBox = (event) => {
event.target.classList.remove("border-gray-dark")
event.target.classList.remove("box-shadow-large")
event.target.classList.add("box-shadow")
event.target.classList.add("border-gray")
}
const registerBoxEventHandlers = (element) => {
element.addEventListener("mouseenter", hoverOnBox)
element.addEventListener("mouseleave", hoverOffBox)
}
var boxes = document.getElementsByClassName("box")
for (let box of boxes) {
registerBoxEventHandlers(box)
}
// create hover effect for modal navigation elements
// const hoverOnNav = (event) => {}
// logic to show and hide picture modal
const displayModal = (display) => {
if (display) {
document.getElementById("modal").hidden = false
document.getElementById("thumbnails").hidden = true
} else {
document.getElementById("thumbnails").hidden = false
document.getElementById("modal").hidden = true
document.getElementById("modalMedia").innerHTML = ""
window.location.hash = ""
}
}
// TODO add swipe support https://stackoverflow.com/questions/2264072/detect-a-finger-swipe-through-javascript-on-the-iphone-and-android
// modal previous and next picture button logic
const preload = (number) => {
var preloadLink = document.createElement("link")
preloadLink.rel = "prefetch"
preloadLink.href = encodeURI(pictures[number].fullsize)
const fileExtension = preloadLink.href.split("\.").pop()
if (fileExtension == videoExtension) {
preloadLink.as = "video"
} else {
preloadLink.as = "image"
}
document.head.appendChild(preloadLink)
}
const prevPicture = () => {
changePicture(getPrevPicture())
preload(getPrevPicture())
}
const getPrevPicture = () => {
if (!isNaN(currentPicture)) {
if (currentPicture === 0) {
return (pictures.length - 1)
} else if (currentPicture < 0 || currentPicture >= pictures.length) {
console.error("Invalid currentPicture, 0.." + pictures.length - 1 + ": " + currentPicture)
} else {
return (currentPicture - 1)
}
} else {
console.error("Invalid currentPicture, NaN: " + currentPicture)
}
}
const nextPicture = () => {
changePicture(getNextPicture())
preload(getNextPicture())
}
const getNextPicture = () => {
if (!isNaN(currentPicture)) {
if (currentPicture === pictures.length - 1) {
return (0)
} else if (currentPicture < 0 || currentPicture >= pictures.length) {
console.error("Invalid currentPicture, 0.." + pictures.length - 1 + ": " + currentPicture)
} else {
return (currentPicture + 1)
}
} else {
console.error("Invalid currentPicture, NaN: " + currentPicture)
}
}
// function to change picture in modal, used by hashNavigate, and next/prevPicture
const changePicture = (number) => {
thumbnailFilename = pictures[number].thumbnail
window.location.hash = pictures[number].filename
const fileExtension = pictures[number].fullsize.split("\.").pop()
if (fileExtension == videoExtension) {
document.getElementById("modalMedia").innerHTML = "<video controls><source src=\"" + encodeURI(pictures[number].fullsize) + "\" type=\"" + videoMIMEType + "\"></video>"
} else {
document.getElementById("modalMedia").innerHTML = "<img src=\"" + encodeURI(pictures[number].fullsize) + "\" alt=\"" + pictures[number].filename + "\" class=\"modalImage\">"
}
document.getElementById("modalDescription").innerHTML = pictures[number].filename
document.getElementById("modalDownload").href = pictures[number].original
currentPicture = number
}
// if URL links directly to thumbnail via hash link, open modal for that pic on page load
const hashNavigate = () => {
if (window.location.hash) {
const filename = decodeURI(window.location.hash.substring(1))
id = pictures.findIndex(item => item.filename == filename)
if (id != -1 && id >= 0 && id < pictures.length) {
changePicture(id)
displayModal(true)
} else {
displayModal(false)
console.error("Invalid thumbnail link provided after # in URI")
}
} else {
displayModal(false)
}
}
const checkKey = (event) => {
if (event.key === "ArrowLeft") {
prevPicture()
} else if (event.key === "ArrowRight") {
nextPicture()
} else if (event.key === "Escape") {
displayModal(false)
}
}
document.onkeydown = checkKey
window.onpopstate = hashNavigate

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 MiB

Loading…
Cancel
Save