2018-04-02 05:34:16 +00:00
|
|
|
import AnimeNotifier from "../AnimeNotifier"
|
|
|
|
import StatusMessage from "../StatusMessage"
|
2018-04-17 12:41:31 +00:00
|
|
|
import { bytesHumanReadable, uploadWithProgress } from "../Utils"
|
2018-03-02 16:18:29 +00:00
|
|
|
|
|
|
|
// Select file
|
|
|
|
export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) {
|
2018-03-16 21:40:38 +00:00
|
|
|
if(button.dataset.endpoint === "/api/upload/cover" && arn.user.dataset.pro !== "true") {
|
2018-03-07 14:57:03 +00:00
|
|
|
alert("Please buy a PRO account to use this feature.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-14 22:20:53 +00:00
|
|
|
let fileType = button.dataset.type
|
2018-03-07 13:00:14 +00:00
|
|
|
let endpoint = button.dataset.endpoint
|
|
|
|
|
|
|
|
// Click on virtual file input element
|
|
|
|
let input = document.createElement("input")
|
2018-03-02 16:18:29 +00:00
|
|
|
input.setAttribute("type", "file")
|
2018-07-19 05:10:43 +00:00
|
|
|
input.value = null
|
2018-03-02 16:18:29 +00:00
|
|
|
|
|
|
|
input.onchange = () => {
|
2018-03-02 20:42:34 +00:00
|
|
|
let file = input.files[0]
|
|
|
|
|
2018-03-03 17:38:21 +00:00
|
|
|
if(!file) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-14 22:20:53 +00:00
|
|
|
// Check mime type for images
|
|
|
|
if(fileType === "image" && !file.type.startsWith("image/")) {
|
2018-03-03 18:58:18 +00:00
|
|
|
arn.statusMessage.showError(file.name + " is not an image file!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-14 22:20:53 +00:00
|
|
|
// Check mime type for videos
|
|
|
|
if(fileType === "video" && !file.type.startsWith("video/webm")) {
|
|
|
|
arn.statusMessage.showError(file.name + " is not a WebM video file!")
|
|
|
|
return
|
|
|
|
}
|
2018-03-02 16:18:29 +00:00
|
|
|
|
2018-04-14 22:20:53 +00:00
|
|
|
// Preview image
|
|
|
|
if(fileType === "image") {
|
2018-04-22 13:24:17 +00:00
|
|
|
let previews = document.getElementsByClassName(button.id + "-preview")
|
2018-07-19 05:10:43 +00:00
|
|
|
previewImage(file, endpoint, previews)
|
2018-03-03 17:29:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 22:20:53 +00:00
|
|
|
uploadFile(file, fileType, endpoint, arn)
|
2018-03-02 16:18:29 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 22:20:53 +00:00
|
|
|
input.click()
|
2018-03-02 16:18:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 23:20:10 +00:00
|
|
|
// Upload file
|
2018-04-14 22:20:53 +00:00
|
|
|
function uploadFile(file: File, fileType: string, endpoint: string, arn: AnimeNotifier) {
|
2018-03-02 20:42:34 +00:00
|
|
|
let reader = new FileReader()
|
|
|
|
|
|
|
|
reader.onloadend = async () => {
|
2018-09-27 04:54:02 +00:00
|
|
|
let result = reader.result as ArrayBuffer
|
|
|
|
let fileSize = result.byteLength
|
2018-04-16 21:23:14 +00:00
|
|
|
|
2018-04-17 11:01:51 +00:00
|
|
|
if(fileSize === 0) {
|
|
|
|
arn.statusMessage.showError("File is empty")
|
|
|
|
return
|
2018-04-16 21:23:14 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 11:01:51 +00:00
|
|
|
arn.statusMessage.showInfo(`Preparing to upload ${fileType} (${bytesHumanReadable(fileSize)})`, -1)
|
2018-04-16 19:41:05 +00:00
|
|
|
|
|
|
|
try {
|
2018-04-17 12:41:31 +00:00
|
|
|
let responseText = await uploadWithProgress(endpoint, {
|
2018-04-16 19:41:05 +00:00
|
|
|
method: "POST",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/octet-stream"
|
|
|
|
},
|
|
|
|
body: reader.result
|
|
|
|
}, e => {
|
2018-04-17 11:01:51 +00:00
|
|
|
let progress = e.loaded / (e.lengthComputable ? e.total : fileSize) * 100
|
2018-04-16 19:41:05 +00:00
|
|
|
arn.statusMessage.showInfo(`Uploading ${fileType}...${progress.toFixed(1)}%`, -1)
|
|
|
|
})
|
2018-03-03 15:03:18 +00:00
|
|
|
|
2018-04-14 22:20:53 +00:00
|
|
|
arn.statusMessage.showInfo(`Successfully uploaded your new ${fileType}.`)
|
2018-04-16 19:41:05 +00:00
|
|
|
|
|
|
|
if(endpoint === "/api/upload/avatar") {
|
|
|
|
// We received the new avatar URL
|
|
|
|
updateSideBarAvatar(responseText)
|
|
|
|
}
|
|
|
|
} catch(err) {
|
2018-04-14 22:20:53 +00:00
|
|
|
arn.statusMessage.showError(`Failed uploading your new ${fileType}.`)
|
2018-04-16 19:41:05 +00:00
|
|
|
console.error(err)
|
2018-03-03 15:03:18 +00:00
|
|
|
}
|
2018-03-02 20:42:34 +00:00
|
|
|
}
|
2018-03-02 16:18:29 +00:00
|
|
|
|
2018-04-16 19:41:05 +00:00
|
|
|
arn.statusMessage.showInfo(`Reading ${fileType} from disk...`, -1)
|
2018-03-02 23:04:54 +00:00
|
|
|
reader.readAsArrayBuffer(file)
|
2018-03-03 17:29:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 22:20:53 +00:00
|
|
|
// Preview image
|
2018-04-22 13:24:17 +00:00
|
|
|
function previewImage(file: File, endpoint: string, previews: HTMLCollectionOf<Element>) {
|
2018-04-14 22:20:53 +00:00
|
|
|
let reader = new FileReader()
|
|
|
|
|
|
|
|
reader.onloadend = () => {
|
2018-09-27 04:54:02 +00:00
|
|
|
let dataURL = reader.result as string
|
|
|
|
|
2018-04-14 22:20:53 +00:00
|
|
|
if(endpoint === "/api/upload/avatar") {
|
|
|
|
let svgPreview = document.getElementById("avatar-input-preview-svg") as HTMLImageElement
|
|
|
|
|
|
|
|
if(svgPreview) {
|
|
|
|
svgPreview.classList.add("hidden")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-22 13:24:17 +00:00
|
|
|
for(let preview of previews) {
|
|
|
|
let img = preview as HTMLImageElement
|
|
|
|
img.classList.remove("hidden")
|
2018-04-24 00:51:00 +00:00
|
|
|
|
|
|
|
// Make not found images visible again
|
|
|
|
if(img.classList.contains("lazy")) {
|
|
|
|
img.classList.remove("element-not-found")
|
|
|
|
img.classList.add("element-found")
|
|
|
|
}
|
|
|
|
|
2018-09-27 04:54:02 +00:00
|
|
|
img.src = dataURL
|
2018-04-22 13:24:17 +00:00
|
|
|
}
|
2018-04-14 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reader.readAsDataURL(file)
|
|
|
|
}
|
|
|
|
|
2018-03-03 17:29:39 +00:00
|
|
|
// Update sidebar avatar
|
|
|
|
function updateSideBarAvatar(url: string) {
|
|
|
|
let sidebar = document.getElementById("sidebar")
|
|
|
|
let userImage = sidebar.getElementsByClassName("user-image")[0] as HTMLImageElement
|
|
|
|
let lazyLoad = userImage["became visible"]
|
|
|
|
|
|
|
|
if(lazyLoad) {
|
|
|
|
userImage.dataset.src = url
|
|
|
|
lazyLoad()
|
|
|
|
} else {
|
|
|
|
location.reload()
|
|
|
|
}
|
2018-03-02 16:18:29 +00:00
|
|
|
}
|