Enforce avatar image dimensions

This commit is contained in:
Eduard Urbach 2018-10-31 18:57:28 +09:00
parent 6ee4b0248d
commit 694ec7c2c5
2 changed files with 94 additions and 40 deletions

View File

@ -28,23 +28,31 @@ func main() {
} }
if objectType == "all" || objectType == "anime" { if objectType == "all" || objectType == "anime" {
filepath.Walk(path.Join(arn.Root, "jobs/mal-download/anime"), func(name string, info os.FileInfo, err error) error { readFiles(path.Join(arn.Root, "jobs", "mal-download", "anime"), readAnimeFile)
if err != nil {
fmt.Println(err)
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(name, ".html.gz") {
return nil
}
return readAnimeFile(name)
})
} }
if objectType == "all" || objectType == "character" {
readFiles(path.Join(arn.Root, "jobs", "mal-download", "character"), readCharacterFile)
}
}
func readFiles(root string, onFile func(string) error) {
filepath.Walk(root, func(name string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(name, ".html.gz") {
return nil
}
return onFile(name)
})
} }
func readAnimeFile(name string) error { func readAnimeFile(name string) error {

View File

@ -17,7 +17,7 @@ export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) {
input.setAttribute("type", "file") input.setAttribute("type", "file")
input.value = null input.value = null
input.onchange = () => { input.onchange = async () => {
let file = input.files[0] let file = input.files[0]
if(!file) { if(!file) {
@ -39,7 +39,26 @@ export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) {
// Preview image // Preview image
if(fileType === "image") { if(fileType === "image") {
let previews = document.getElementsByClassName(button.id + "-preview") let previews = document.getElementsByClassName(button.id + "-preview")
previewImage(file, endpoint, previews) let dataURL = await readImageAsDataURL(file)
let img = await loadImage(dataURL)
switch(endpoint) {
case "/api/upload/avatar":
if(img.naturalWidth <= 280 || img.naturalHeight < 280) {
arn.statusMessage.showError(`Your image has a resolution of ${img.naturalWidth} x ${img.naturalHeight} pixels which is too small. Recommended: 560 x 560. Minimum: 280 x 280.`, 8000)
return
}
break
case "/api/upload/cover":
if(img.naturalWidth <= 960 || img.naturalHeight < 225) {
arn.statusMessage.showError(`Your image has a resolution of ${img.naturalWidth} x ${img.naturalHeight} pixels which is too small. Recommended: 1920 x 450. Minimum: 960 x 225.`, 8000)
return
}
break
}
previewImage(dataURL, endpoint, previews)
} }
uploadFile(file, fileType, endpoint, arn) uploadFile(file, fileType, endpoint, arn)
@ -92,36 +111,63 @@ function uploadFile(file: File, fileType: string, endpoint: string, arn: AnimeNo
reader.readAsArrayBuffer(file) reader.readAsArrayBuffer(file)
} }
// Preview image // Read image as data URL
function previewImage(file: File, endpoint: string, previews: HTMLCollectionOf<Element>) { function readImageAsDataURL(file: File): Promise<string> {
let reader = new FileReader() return new Promise((resolve, reject) => {
let reader = new FileReader()
reader.onloadend = () => { reader.onloadend = () => {
let dataURL = reader.result as string let dataURL = reader.result as string
resolve(dataURL)
if(endpoint === "/api/upload/avatar") {
let svgPreview = document.getElementById("avatar-input-preview-svg") as HTMLImageElement
if(svgPreview) {
svgPreview.classList.add("hidden")
}
} }
for(let preview of previews) { reader.onerror = event => {
let img = preview as HTMLImageElement reader.abort()
img.classList.remove("hidden") reject(event)
}
// Make not found images visible again reader.readAsDataURL(file)
if(img.classList.contains("lazy")) { })
img.classList.remove("element-not-found") }
img.classList.add("element-found")
}
img.src = dataURL // Load image and resolve when loading has finished
function loadImage(url: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
let img = new Image()
img.src = url
img.onload = () => {
resolve(img)
}
img.onerror = error => {
reject(error)
}
})
}
// Preview image
function previewImage(dataURL: string, endpoint: string, previews: HTMLCollectionOf<Element>) {
if(endpoint === "/api/upload/avatar") {
let svgPreview = document.getElementById("avatar-input-preview-svg") as HTMLImageElement
if(svgPreview) {
svgPreview.classList.add("hidden")
} }
} }
reader.readAsDataURL(file) for(let preview of previews) {
let img = preview as HTMLImageElement
img.classList.remove("hidden")
// Make not found images visible again
if(img.classList.contains("lazy")) {
img.classList.remove("element-not-found")
img.classList.add("element-found")
}
img.src = dataURL
}
} }
// Update sidebar avatar // Update sidebar avatar