Skip to content

Commit

Permalink
Also update download remaining counter #206
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceu committed Dec 12, 2024
1 parent b230f86 commit 1611127
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
17 changes: 11 additions & 6 deletions internal/webserver/sse/Sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
}
Expand Down
17 changes: 14 additions & 3 deletions internal/webserver/sse/Sse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
14 changes: 12 additions & 2 deletions internal/webserver/web/static/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}


Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand Down
Loading

0 comments on commit 1611127

Please sign in to comment.