Skip to content

Commit

Permalink
Revert "uncontrolled path fixes."
Browse files Browse the repository at this point in the history
This reverts commit 2d38829.

uncontrolled path fixes.

package updates.

automatic thumbnails for textfiles artifacts.

applied a 10 second timeout for all command run funcs.

fix file content readme button being too strict. TextImager and TextAmigaImager now always crop text before using ansilove.

fixed authors for the uploader not being saved.

fixed releaser #2 using wrong http method.

Editor Titleize button.

not working text update.

fixed wasm loading issues.

rename wasm to wasm.js
  • Loading branch information
bengarrett committed Sep 29, 2024
1 parent a1175d7 commit fc112ee
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 61 deletions.
11 changes: 10 additions & 1 deletion assets/js/editor-artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
youtube as validateYouTube,
number as validateNumber,
} from "./artifact-validate.mjs";
import { clipValue, getElmById } from "./helper.mjs";
import { clipValue, getElmById, titleize } from "./helper.mjs";

(() => {
"use strict";
Expand Down Expand Up @@ -283,6 +283,15 @@ import { clipValue, getElmById } from "./helper.mjs";
title.value = titleU[0].value;
title.classList.add("is-valid");
});
const titleizeBtn = document.getElementById("artifact-editor-titleize");
if (titleizeBtn.length === 0) {
throw new Error("The titleize button is missing.");
}
titleizeBtn.addEventListener("click", () => {
title.value = titleize(title.value);
let event = new Event("keyup");
title.dispatchEvent(event);
});

const ct = document.getElementById("artifact-editor-credit-text");
if (ct === null) {
Expand Down
34 changes: 34 additions & 0 deletions assets/js/helper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@
* This module provides functions for handling common tasks.
*/

/**
* Titleizes a string of text but keeps common adverbs in lowercase.
*
* @param {string} text - The input text to be titleized.
* @returns {string} - The titleized text.
*/
export function titleize(text) {
const commonAdverbs = [
"a",
"an",
"and",
"as",
"but",
"for",
"if",
"of",
"or",
"so",
"the",
"to",
];
return text
.split(" ")
.map((word, index) => {
// Capitalize the first word and any word not in the common adverbs list
if (index === 0 || !commonAdverbs.includes(word.toLowerCase())) {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
// Keep common adverbs in lowercase
return word.toLowerCase();
})
.join(" ");
}

/**
* Copies the text content of an HTML element to the clipboard.
* @async
Expand Down
11 changes: 3 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/Defacto2/server
go 1.22.7

require (
github.com/Defacto2/archive v1.0.2
github.com/Defacto2/helper v1.1.2
github.com/Defacto2/magicnumber v1.0.1
github.com/Defacto2/archive v1.0.3
github.com/Defacto2/helper v1.1.3
github.com/Defacto2/magicnumber v1.0.2
github.com/Defacto2/releaser v1.0.4
github.com/caarlos0/env/v11 v11.2.2
github.com/carlmjohnson/versioninfo v0.22.5
Expand Down Expand Up @@ -33,11 +33,6 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

// replace (
// github.com/Defacto2/archive => /home/ben/github/archive
// github.com/Defacto2/helper => /home/ben/github/helper
// )

require (
cloud.google.com/go/auth v0.9.4 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/DATA-DOG/go-sqlmock v1.4.1 h1:ThlnYciV1iM/V0OSF/dtkqWb6xo5qITT1TJBG1MRDJM=
github.com/DATA-DOG/go-sqlmock v1.4.1/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/Defacto2/archive v1.0.2 h1:kibNK3ZFoB87XIeDolxQ6uDbdCnBvDNygJtgngE3prM=
github.com/Defacto2/archive v1.0.2/go.mod h1:0/aa1BwDOBp4d87R/N33Lcr3+WzELv041wunK8R5A5c=
github.com/Defacto2/helper v1.1.2 h1:HVJnWI9a0nA7vOcfO3BZTSPGNftNS678wf6daMrjbE0=
github.com/Defacto2/helper v1.1.2/go.mod h1:IikMmXvNy3uOcLk4/cEi9mb+xcx8AHPsSmVGd8SKpB0=
github.com/Defacto2/magicnumber v1.0.1 h1:UmiBUrYwTk3NPR1lGaaQ3Js+3YllBRf9XxSKInE3Da4=
github.com/Defacto2/magicnumber v1.0.1/go.mod h1:SIr4UUO0htkx89FNTdUIpuCbuZqW9uYC5spvWl9bBtk=
github.com/Defacto2/archive v1.0.3 h1:7iidbFf3yBJ0e2XWsx/uhB4ciWysE5nMejzRCQSgops=
github.com/Defacto2/archive v1.0.3/go.mod h1:1nM/bBCir4nz1t/HFYnZk9tXc/yaWzJTEu1+CDoGyUE=
github.com/Defacto2/helper v1.1.3 h1:BS7PqOR31AtXC+BTKM9oL2OE+NLrdLqd8xyBxwAsCps=
github.com/Defacto2/helper v1.1.3/go.mod h1:IikMmXvNy3uOcLk4/cEi9mb+xcx8AHPsSmVGd8SKpB0=
github.com/Defacto2/magicnumber v1.0.2 h1:zElw+GCZiS0n4xta6g7WDp9ovx1RZKRYL+q9zj0l4dk=
github.com/Defacto2/magicnumber v1.0.2/go.mod h1:U64FNRfNyv5tBE6jPU00ortSbrIAVO5V34tZFwaeA3s=
github.com/Defacto2/releaser v1.0.4 h1:dM7epo3Rk+l2NAH4/dMHzo9QCIC6VejhptlhPUtfg3U=
github.com/Defacto2/releaser v1.0.4/go.mod h1:Jt9HqJDl4ol1oKFb2efvO+8d8buFwSYfn/Trh7O3oLw=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
Expand Down
61 changes: 52 additions & 9 deletions handler/app/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/Defacto2/server/handler/readme"
"github.com/Defacto2/server/handler/render"
"github.com/Defacto2/server/handler/sess"
"github.com/Defacto2/server/internal/command"
"github.com/Defacto2/server/internal/postgres/models"
"github.com/Defacto2/server/internal/tags"
"github.com/Defacto2/server/model"
Expand Down Expand Up @@ -182,18 +183,23 @@ func (dir Dirs) updateMagics(db *sql.DB, logger *zap.SugaredLogger,
}
return data
}
switch modMagic {
case
magicnumber.ARChiveSEA.Title(),
magicnumber.YoshiLHA.Title(),
magicnumber.ArchiveRobertJung.Title(),
magicnumber.PKWAREZipImplode.Title(),
magicnumber.PKWAREZipReduce.Title(),
magicnumber.PKWAREZipShrink.Title():
case magicnumber.PKWAREZip.Title():
switch {
case redundantArchive(modMagic):
case modMagic == magicnumber.PKWAREZip.Title():
if !repackZIP(name) {
return data
}
case plainText(modMagic):
dirs := command.Dirs{
Download: dir.Download,
Preview: dir.Preview,
Thumbnail: dir.Thumbnail,
}
if err := dirs.TextImager(logger, name, uid); err != nil {
logger.Error(errorWithID(err, "text imager", uid))
}
data["missingAssets"] = ""
return data
default:
return data
}
Expand All @@ -211,6 +217,43 @@ func (dir Dirs) updateMagics(db *sql.DB, logger *zap.SugaredLogger,
return data
}

func redundantArchive(modMagic interface{}) bool {
switch modMagic.(type) {
case string:
default:
return false
}
switch modMagic.(string) {
case
magicnumber.ARChiveSEA.Title(),
magicnumber.YoshiLHA.Title(),
magicnumber.ArchiveRobertJung.Title(),
magicnumber.PKWAREZipImplode.Title(),
magicnumber.PKWAREZipReduce.Title(),
magicnumber.PKWAREZipShrink.Title():
return true
default:
return false
}
}

func plainText(modMagic interface{}) bool {
switch modMagic.(type) {
case string:
default:
return false
}
switch modMagic.(string) {
case
magicnumber.UTF8Text.Title(),
magicnumber.ANSIEscapeText.Title(),
magicnumber.PlainText.Title():
return true
default:
return false
}
}

func (dir Dirs) embed(art *models.File, data map[string]interface{}) (map[string]interface{}, error) {
if art == nil {
return data, nil
Expand Down
2 changes: 1 addition & 1 deletion handler/app/internal/filerecord/filerecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (m ListEntry) Column2(htm string) string {
`<use xlink:href="/svg/bootstrap-icons.svg#terminal-plus"></use></svg></div>`
case osTool(ext):
htm += blank
case m.Texts && !soloText():
case m.Texts || soloText():
htm += readmecopy(m.UniqueID, name)
default:
htm += blank
Expand Down
1 change: 1 addition & 0 deletions handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (c Configuration) embed(e *echo.Echo, public embed.FS) *echo.Echo {
e.FileFS("/osd.xml", "public/text/osd.xml", public)
e.FileFS("/robots.txt", "public/text/robots.txt", public)
e.FileFS("/site.webmanifest", "public/text/site.webmanifest.json", public)
e.FileFS("/wdosbox.wasm.js", "public/js/wdosbox.wasm", public) // this is required by `js-dos.js`
return e
}

Expand Down
49 changes: 29 additions & 20 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,38 @@ package command

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"go.uber.org/zap"
)

const (
pattern = "defacto2-" // prefix for temporary directories
patternS = "defacto2-server"
arc = ".arc" // arc file extension
arj = ".arj" // arj file extension
bmp = ".bmp" // bmp file extension
gif = ".gif" // gif file extension
gzip = ".gz" // gzip file extension
jpg = ".jpg" // jpg file extension
jpeg = ".jpeg" // jpeg file extension
png = ".png" // png file extension
rar = ".rar" // rar file extension
tar = ".tar" // tar file extension
tiff = ".tiff" // tiff file extension
txt = ".txt" // txt file extension
webp = ".webp" // webp file extension
zip = ".zip" // zip file extension
zip7 = ".7z" // 7zip file extension
cmdTimeout = 10 * time.Second
pattern = "defacto2-" // prefix for temporary directories
patternS = "defacto2-server"
arc = ".arc" // arc file extension
arj = ".arj" // arj file extension
bmp = ".bmp" // bmp file extension
gif = ".gif" // gif file extension
gzip = ".gz" // gzip file extension
jpg = ".jpg" // jpg file extension
jpeg = ".jpeg" // jpeg file extension
png = ".png" // png file extension
rar = ".rar" // rar file extension
tar = ".tar" // tar file extension
tiff = ".tiff" // tiff file extension
txt = ".txt" // txt file extension
webp = ".webp" // webp file extension
zip = ".zip" // zip file extension
zip7 = ".7z" // 7zip file extension
)

var (
Expand Down Expand Up @@ -215,7 +218,9 @@ func RunStdOut(name string, arg ...string) ([]byte, error) {
return nil, fmt.Errorf("run output %w", err)
}
var out bytes.Buffer
cmd := exec.Command(name, arg...)
ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, name, arg...)
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("run output cmd.run %w", err)
Expand All @@ -228,7 +233,9 @@ func RunQuiet(name string, arg ...string) error {
if err := LookCmd(name); err != nil {
return fmt.Errorf("run quiet %w", err)
}
cmd := exec.Command(name, arg...)
ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, name, arg...)
if err := cmd.Start(); err != nil {
return fmt.Errorf("run quiet start %w", err)
}
Expand All @@ -249,7 +256,9 @@ func run(debug *zap.SugaredLogger, name, wdir string, arg ...string) error {
if err := LookCmd(name); err != nil {
return fmt.Errorf("run %w", err)
}
cmd := exec.Command(name, arg...)
ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, name, arg...)
cmd.Dir = wdir
if debug != nil {
p, err := cmd.CombinedOutput()
Expand Down
Loading

0 comments on commit fc112ee

Please sign in to comment.