Also update download remaining counter #206

This commit is contained in:
Marc Ole Bulling
2024-12-12 13:01:55 +01:00
parent b230f86ed0
commit 1611127bfa
5 changed files with 39 additions and 13 deletions

View File

@@ -35,9 +35,10 @@ func removeListener(id string) {
}
type eventFileDownload struct {
Event string `json:"event"`
FileId string `json:"file_id"`
DownloadCount int `json:"download_count"`
Event string `json:"event"`
FileId string `json:"file_id"`
DownloadCount int `json:"download_count"`
DownloadsRemaining int `json:"downloads_remaining"`
}
type eventUploadStatus struct {
Event string `json:"event"`
@@ -71,9 +72,13 @@ func publishMessage[d eventData](data d) {
func PublishDownloadCount(file models.File) {
event := eventFileDownload{
Event: "download",
FileId: file.Id,
DownloadCount: file.DownloadCount,
Event: "download",
FileId: file.Id,
DownloadCount: file.DownloadCount,
DownloadsRemaining: file.DownloadsRemaining,
}
if file.UnlimitedDownloads {
event.DownloadsRemaining = -1
}
publishMessage(event)
}

View File

@@ -54,11 +54,22 @@ func TestPublishNewStatus(t *testing.T) {
test.IsEqualString(t, receivedStatus, "event: message\ndata: {\"event\":\"uploadStatus\",\"chunk_id\":\"testChunkId\",\"upload_status\":4}\n\n")
go PublishDownloadCount(models.File{
Id: "testFileId",
DownloadCount: 3,
Id: "testFileId",
DownloadCount: 3,
DownloadsRemaining: 1,
UnlimitedDownloads: false,
})
receivedStatus = <-replyChannel
test.IsEqualString(t, receivedStatus, "event: message\ndata: {\"event\":\"download\",\"file_id\":\"testFileId\",\"download_count\":3}\n\n")
test.IsEqualString(t, receivedStatus, "event: message\ndata: {\"event\":\"download\",\"file_id\":\"testFileId\",\"download_count\":3,\"downloads_remaining\":1}\n\n")
go PublishDownloadCount(models.File{
Id: "testFileId",
DownloadCount: 3,
DownloadsRemaining: 2,
UnlimitedDownloads: true,
})
receivedStatus = <-replyChannel
test.IsEqualString(t, receivedStatus, "event: message\ndata: {\"event\":\"download\",\"file_id\":\"testFileId\",\"download_count\":3,\"downloads_remaining\":-1}\n\n")
removeListener("test_id")
}

View File

@@ -599,7 +599,7 @@ function parseSseData(data) {
}
switch (eventData.event) {
case "download":
setNewDownloadCount(eventData.file_id, eventData.download_count);
setNewDownloadCount(eventData.file_id, eventData.download_count, eventData.downloads_remaining);
return;
case "uploadStatus":
setProgressStatus(eventData.chunk_id, eventData.upload_status);
@@ -609,13 +609,21 @@ function parseSseData(data) {
}
}
function setNewDownloadCount(id, downloadCount) {
function setNewDownloadCount(id, downloadCount, downloadsRemaining) {
let downloadCell = document.getElementById("cell-downloads-" + id);
if (downloadCell != null) {
downloadCell.innerHTML = downloadCount;
downloadCell.classList.add("updatedDownloadCount");
setTimeout(() => downloadCell.classList.remove("updatedDownloadCount"), 500);
}
if (downloadsRemaining != -1) {
let downloadsRemainingCell = document.getElementById("cell-downloadsRemaining-" + id);
if (downloadsRemainingCell != null) {
downloadsRemainingCell.innerHTML = downloadsRemaining;
downloadsRemainingCell.classList.add("updatedDownloadCount");
setTimeout(() => downloadsRemainingCell.classList.remove("updatedDownloadCount"), 500);
}
}
}
@@ -732,6 +740,7 @@ function addRow(jsonText) {
cellRemainingDownloads.innerText = "Unlimited";
} else {
cellRemainingDownloads.innerText = item.DownloadsRemaining;
cellRemainingDownloads.id = "cell-downloadsRemaining-" + item.Id;
}
if (item.UnlimitedTime) {
cellStoredUntil.innerText = "Unlimited";
@@ -756,6 +765,7 @@ function addRow(jsonText) {
cellFilename.classList.add('newItem');
cellFileSize.classList.add('newItem');
cellRemainingDownloads.classList.add('newItem');
cellRemainingDownloads.style.backgroundColor = "green";
cellStoredUntil.classList.add('newItem');
cellDownloadCount.classList.add('newItem');
cellDownloadCount.style.backgroundColor = "green";

File diff suppressed because one or more lines are too long

View File

@@ -70,7 +70,7 @@
{{ if .UnlimitedDownloads }}
<td>Unlimited</td>
{{ else }}
<td>{{ .DownloadsRemaining }}</td>
<td id="cell-downloadsRemaining-{{ .Id }}">{{ .DownloadsRemaining }}</td>
{{ end }}
{{ if .UnlimitedTime }}
<td>Unlimited</td>