Files
fnx_web/res/include/script/history.js

77 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-02-03 17:28:26 +01:00
function renderFileButton(apiURL, id, title, subtitle) {
2020-02-04 19:37:46 +01:00
let btn = document.createElement("a")
btn.classList = "file_button"
btn.href = "/u/"+id
btn.target = "_blank"
let thumbnail = document.createElement("img")
thumbnail.src = apiURL+"/file/"+id+"/thumbnail?width=80&height=80"
thumbnail.alt = title
let titleSpan = document.createElement("span")
titleSpan.classList = "file_button_title"
titleSpan.innerText = title
let br = document.createElement("br")
let subtitleSpan = document.createElement("span")
subtitleSpan.classList = "file_button_subtitle"
subtitleSpan.innerText = subtitle
2020-02-03 17:28:26 +01:00
2020-02-04 19:37:46 +01:00
btn.appendChild(thumbnail)
btn.appendChild(titleSpan)
btn.appendChild(br)
btn.appendChild(subtitleSpan)
return btn
2020-02-03 17:28:26 +01:00
}
2020-01-20 12:43:43 +01:00
function getCookie(name) {
2020-02-04 19:37:46 +01:00
var result = new RegExp('(?:^|; )' + encodeURIComponent(name) + '=([^;]*)').exec(document.cookie)
return result ? result[1] : null
2020-01-20 12:43:43 +01:00
}
2017-11-10 12:39:55 +01:00
2020-01-20 12:43:43 +01:00
// Get the uploads from localstorage
2020-02-04 19:37:46 +01:00
let uploadsStr = localStorage.getItem("uploaded_files")
if (uploadsStr === null) { uploadsStr = "" }
2020-02-04 19:37:46 +01:00
let uploads = Array()
2020-01-20 12:43:43 +01:00
if (uploadsStr != "") {
2020-02-11 13:58:19 +01:00
// Strip the trailing comma
uploads = uploadsStr.slice(0, -1).split(",")
2020-01-20 12:43:43 +01:00
}
2017-11-10 12:39:55 +01:00
2020-01-20 12:43:43 +01:00
// Get the uploads from a cookie
2020-02-04 19:37:46 +01:00
uploadsStr = getCookie("pduploads")
if (uploadsStr === null) { uploadsStr = "" }
2020-01-20 12:43:43 +01:00
if (uploadsStr != "") {
2020-02-04 19:37:46 +01:00
uploadsStr = uploadsStr.slice(0, -1) // Strip the trailing dot
uploads.push(uploadsStr.split(".").reverse())
}
2020-01-20 12:43:43 +01:00
// Render all the items
function getHistoryItem() {
2020-02-04 19:37:46 +01:00
let item = uploads.shift()
if (item === undefined || item === "") { return }
2020-01-20 12:43:43 +01:00
fetch(
apiEndpoint+"/file/"+item+"/info"
).then(resp => {
if (!resp.ok) {
2020-02-04 19:37:46 +01:00
return Promise.reject()
2020-01-20 12:43:43 +01:00
}
2020-02-04 19:37:46 +01:00
return resp.json()
2020-01-20 12:43:43 +01:00
}).then(resp => {
document.getElementById("uploaded_files").appendChild(
renderFileButton(
apiEndpoint,
resp.id,
resp.name,
2020-02-04 19:37:46 +01:00
printDate(new Date(resp.date_upload), true, true, true),
2020-01-20 12:43:43 +01:00
)
2020-02-04 19:37:46 +01:00
)
getHistoryItem()
2020-01-20 12:43:43 +01:00
}).catch(err => {
console.log("Fetch failed: "+err)
2020-02-04 19:37:46 +01:00
getHistoryItem()
2020-01-20 12:43:43 +01:00
})
}
2020-02-04 19:37:46 +01:00
getHistoryItem()