2019-09-01 19:23:16 +00:00
|
|
|
/* global lsKeys, page */
|
|
|
|
|
|
|
|
// keys for localStorage
|
|
|
|
lsKeys.siBytes = 'siBytes'
|
|
|
|
|
|
|
|
page.prepareShareX = function () {
|
2019-09-08 02:55:25 +00:00
|
|
|
const values = page.token ? {
|
2019-09-08 01:56:29 +00:00
|
|
|
token: page.token || '',
|
2019-09-08 02:55:25 +00:00
|
|
|
albumid: page.album || ''
|
|
|
|
} : {}
|
|
|
|
values.filelength = page.filelength || ''
|
|
|
|
values.age = page.uploadAge || ''
|
2019-09-08 01:56:29 +00:00
|
|
|
|
|
|
|
const headers = []
|
|
|
|
const keys = Object.keys(values)
|
|
|
|
for (let i = 0; i < keys.length; i++)
|
|
|
|
// Pad by 4 space
|
|
|
|
headers.push(` "${keys[i]}": "${values[keys[i]]}"`)
|
|
|
|
|
2019-09-01 19:23:16 +00:00
|
|
|
const origin = (location.hostname + location.pathname).replace(/\/(dashboard)?$/, '')
|
|
|
|
const originClean = origin.replace(/\//g, '_')
|
2019-09-08 01:56:29 +00:00
|
|
|
|
2019-09-01 19:23:16 +00:00
|
|
|
const sharexElement = document.querySelector('#ShareX')
|
|
|
|
const sharexFile = `{
|
|
|
|
"Name": "${originClean}",
|
|
|
|
"DestinationType": "ImageUploader, FileUploader",
|
2019-09-08 02:55:25 +00:00
|
|
|
"RequestMethod": "POST",
|
2019-09-01 19:23:16 +00:00
|
|
|
"RequestURL": "${location.protocol}//${origin}/api/upload",
|
|
|
|
"Headers": {
|
2019-09-08 01:56:29 +00:00
|
|
|
${headers.join(',\n')}
|
2019-09-01 19:23:16 +00:00
|
|
|
},
|
2019-09-08 02:55:25 +00:00
|
|
|
"Body": "MultipartFormData",
|
|
|
|
"FileFormName": "files[]",
|
2019-09-01 19:23:16 +00:00
|
|
|
"URL": "$json:files[0].url$",
|
|
|
|
"ThumbnailURL": "$json:files[0].url$"
|
2019-09-08 01:56:29 +00:00
|
|
|
}`
|
|
|
|
|
2019-09-01 19:23:16 +00:00
|
|
|
const sharexBlob = new Blob([sharexFile], { type: 'application/octet-binary' })
|
|
|
|
sharexElement.setAttribute('href', URL.createObjectURL(sharexBlob))
|
|
|
|
sharexElement.setAttribute('download', `${originClean}.sxcu`)
|
|
|
|
}
|
|
|
|
|
|
|
|
page.getPrettyDate = function (date) {
|
|
|
|
return date.getFullYear() + '-' +
|
|
|
|
(date.getMonth() < 9 ? '0' : '') + // month's index starts from zero
|
|
|
|
(date.getMonth() + 1) + '-' +
|
|
|
|
(date.getDate() < 10 ? '0' : '') +
|
|
|
|
date.getDate() + ' ' +
|
|
|
|
(date.getHours() < 10 ? '0' : '') +
|
|
|
|
date.getHours() + ':' +
|
|
|
|
(date.getMinutes() < 10 ? '0' : '') +
|
|
|
|
date.getMinutes() + ':' +
|
|
|
|
(date.getSeconds() < 10 ? '0' : '') +
|
|
|
|
date.getSeconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
page.getPrettyBytes = function (num) {
|
|
|
|
// MIT License
|
|
|
|
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
|
|
|
if (!Number.isFinite(num)) return num
|
|
|
|
|
|
|
|
const si = localStorage[lsKeys.siBytes] !== '0'
|
|
|
|
const neg = num < 0 ? '-' : ''
|
|
|
|
const scale = si ? 1000 : 1024
|
|
|
|
if (neg) num = -num
|
|
|
|
if (num < scale) return `${neg}${num} B`
|
|
|
|
|
|
|
|
const exponent = Math.min(Math.floor(Math.log10(num) / 3), 8) // 8 is count of KMGTPEZY
|
|
|
|
const numStr = Number((num / Math.pow(scale, exponent)).toPrecision(3))
|
|
|
|
const pre = (si ? 'kMGTPEZY' : 'KMGTPEZY').charAt(exponent - 1) + (si ? '' : 'i')
|
|
|
|
return `${neg}${numStr} ${pre}B`
|
|
|
|
}
|