Skip to content

Commit

Permalink
Replacement file upload checks for duplicates.
Browse files Browse the repository at this point in the history
Lastmod date is now updated to the database.
  • Loading branch information
bengarrett committed Sep 22, 2024
1 parent 4810f80 commit 56c09c1
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 27 deletions.
59 changes: 35 additions & 24 deletions assets/js/editor-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
* @file editor-assets.js
* This script is the entry point for the artifact editor assets page.
*/
import { progress } from "./uploader.mjs";
import {
checkDuplicate,
checkErrors,
checkSize,
progress,
resetInput,
} from "./uploader.mjs";
import { getElmById } from "./helper.mjs";

(() => {
"use strict";
Expand All @@ -11,29 +18,31 @@ import { progress } from "./uploader.mjs";
progress(`artifact-editor-dl-form`, `artifact-editor-dl-progress`);
progress(`artifact-editor-preview-form`, `artifact-editor-preview-progress`);

const previewReset = document.getElementById(`artifact-editor-preview-reset`);
if (previewReset == null) {
console.error(`the reset preview button is missing`);
return;
}
const previewInput = document.getElementById(
`artifact-editor-replace-preview`
);
if (previewInput == null) {
console.error(`the form preview input is missing`);
return;
}
const previewReset = getElmById(`artifact-editor-preview-reset`);
const previewInput = getElmById(`artifact-editor-replace-preview`);
const alert = getElmById(`artifact-editor-dl-alert`);
const reset = getElmById(`artifact-editor-dl-reset`);
const lastMod = getElmById(`artifact-editor-last-modified`);
const results = getElmById("artifact-editor-dl-results");
const fileInput = getElmById(`artifact-editor-dl-up`);
fileInput.addEventListener("change", checkFile);

const reset = document.getElementById(`artifact-editor-dl-reset`);
if (reset == null) {
console.error(`the reset button is missing`);
return;
}
const artifact = document.getElementById(`artifact-editor-dl-up`);
if (artifact == null) {
console.error(`the artifact file input is missing`);
return;
async function checkFile() {
resetInput(fileInput, alert, results);
const file1 = this.files[0];
let errors = [checkSize(file1)];
checkErrors(errors, alert, fileInput, results);
checkDuplicate(file1, alert, fileInput, results);

const lastModified = file1.lastModified,
currentTime = new Date().getTime(),
oneHourMs = 60 * 60 * 1000;
const underOneHour = currentTime - lastModified < oneHourMs;
if (!underOneHour) {
lastMod.value = lastModified;
}
}

const dataEditor = document.getElementById("artifact-editor-modal");
if (dataEditor == null) {
console.error(`the data editor modal is missing`);
Expand Down Expand Up @@ -264,8 +273,10 @@ import { progress } from "./uploader.mjs";

// New file download form reset button.
reset.addEventListener(`click`, function () {
artifact.value = ``;
artifact.classList.remove(`is-invalid`, `is-valid`);
alert.innerText = "";
alert.classList.add("d-none");
fileInput.value = ``;
fileInput.classList.remove(`is-invalid`, `is-valid`);
});

previewReset.addEventListener(`click`, function () {
Expand Down
3 changes: 2 additions & 1 deletion handler/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ func LinkScnr(name string) (string, error) {
func LinkScnrs(s string) template.HTML {
x := []string{}
y := strings.Split(s, ",")
cls := "link-dark link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover"
cls := "link-dark link-offset-2 link-offset-3-hover link-underline " +
"link-underline-opacity-0 link-underline-opacity-75-hover"
for _, z := range y {
z = strings.TrimSpace(z)
if z == "" {
Expand Down
9 changes: 8 additions & 1 deletion handler/app/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,14 @@ func detectANSI(db *sql.DB, logger *zap.SugaredLogger, id int64, data map[string
if db == nil {
return data
}
mos, numb := data["modOS"].(string), data["modMagicNumber"].(string)
mos, valid := data["modOS"].(string)
if !valid {
return data
}
numb, valid := data["modMagicNumber"].(string)
if !valid {
return data
}
textfile := strings.EqualFold(mos, tags.Text.String())
if textfile && numb == magicnumber.ANSIEscapeText.Title() {
if err := model.UpdatePlatform(db, id, tags.ANSI.String()); err != nil && logger != nil {
Expand Down
7 changes: 7 additions & 0 deletions handler/htmx/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"slices"
"strconv"
"strings"
"time"

"github.com/Defacto2/archive"
"github.com/Defacto2/helper"
Expand Down Expand Up @@ -638,6 +639,12 @@ func UploadReplacement(c echo.Context, db *sql.DB, downloadDir string) error {
return checkFileOpen(c, nil, name, err)
}
defer src.Close()
lastmod := c.FormValue("artifact-editor-lastmodified")
lm, err := strconv.ParseInt(lastmod, 10, 64)
if err == nil && lm > 0 {
lmod := time.UnixMilli(lm)
fu.LastMod = lmod
}
sign := magicnumber.Find(src)
fu.MagicNumber = sign.Title()
dst, err := copier(c, nil, file, up.key)
Expand Down
2 changes: 2 additions & 0 deletions model/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ type FileUpload struct {
MagicNumber string
Content string
Filesize int64
LastMod time.Time
}

// Update the file record with the values provided in the FileUpload struct.
Expand All @@ -753,6 +754,7 @@ func (fu FileUpload) Update(ctx context.Context, exec boil.ContextExecutor, id i
return fmt.Errorf("file upload zip content: %w", err)
}
f.Filesize = null.Int64From(fu.Filesize)
f.FileLastModified = null.TimeFrom(fu.LastMod)
if _, err = f.Update(ctx, exec, boil.Infer()); err != nil {
return fmt.Errorf("file upload update record %w: %d", err, id)
}
Expand Down
2 changes: 1 addition & 1 deletion public/js/editor-assets.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions view/app/artifactfile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<input type="hidden" name="artifact-editor-record-key" value="{{$key}}">
<input type="hidden" name="artifact-editor-unid" value="{{$unid}}">
<input type="hidden" name="artifact-editor-download-classify" value="{{$os}}">
<input type="hidden" name="artifact-editor-lastmodified" id="artifact-editor-last-modified">
{{/* artifact-editor-unique-id-value */}}
<div class="input-group input-group-sm mb-1 has-validation">
<input type="file" name="artifact-editor-replace-file"
Expand All @@ -133,6 +134,7 @@
<div class="form-text" id="artifact-editor-dl-results"><span class="text-warning-emphasis">Normally not required</span>, upload and replace the artifact download.</div>
{{- end}}
<progress class="w-100" id="artifact-editor-dl-progress" value="0" max="100"></progress>
<div class="d-none alert alert-warning mt-2" role="alert" id="artifact-editor-dl-alert"></div>
</form>
{{- /* Download content */}}
<legend class="mt-3">Download content</legend>
Expand Down

0 comments on commit 56c09c1

Please sign in to comment.