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,7 +28,16 @@ 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 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 { if err != nil {
fmt.Println(err) fmt.Println(err)
return err return err
@ -42,10 +51,9 @@ func main() {
return nil return nil
} }
return readAnimeFile(name) return onFile(name)
}) })
} }
}
func readAnimeFile(name string) error { func readAnimeFile(name string) error {
file, err := os.Open(name) file, err := os.Open(name)

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,13 +111,43 @@ 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> {
return new Promise((resolve, reject) => {
let reader = new FileReader() let reader = new FileReader()
reader.onloadend = () => { reader.onloadend = () => {
let dataURL = reader.result as string let dataURL = reader.result as string
resolve(dataURL)
}
reader.onerror = event => {
reader.abort()
reject(event)
}
reader.readAsDataURL(file)
})
}
// 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") { if(endpoint === "/api/upload/avatar") {
let svgPreview = document.getElementById("avatar-input-preview-svg") as HTMLImageElement let svgPreview = document.getElementById("avatar-input-preview-svg") as HTMLImageElement
@ -121,9 +170,6 @@ function previewImage(file: File, endpoint: string, previews: HTMLCollectionOf<E
} }
} }
reader.readAsDataURL(file)
}
// Update sidebar avatar // Update sidebar avatar
function updateSideBarAvatar(url: string) { function updateSideBarAvatar(url: string) {
let sidebar = document.getElementById("sidebar") let sidebar = document.getElementById("sidebar")