Skip to content

Commit

Permalink
download content work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Aug 1, 2024
1 parent b4111b8 commit 62daa10
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 45 deletions.
26 changes: 24 additions & 2 deletions assets/js/artifact-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,38 @@ import { clipValue, getElmById } from "./helper.mjs";

const copyUUIDVal = getElmById(`artifact-editor-unique-id-value`);
if (copyUUIDVal === null) {
throw new Error("The key value is missing.");
throw new Error("The unique id value is missing.");
}
const copyUUIDBtn = getElmById(`artifact-editor-unique-id-label`);
if (copyUUIDBtn === null) {
throw new Error("The key label is missing.");
throw new Error("The unique id label is missing.");
}
copyUUIDBtn.addEventListener(`click`, () =>
clipValue(`artifact-editor-unique-id-value`)
);

const copyLocation = getElmById(`artifact-editor-location-value`);
if (copyLocation === null) {
throw new Error("The location value is missing.");
}
const copyLocationBtn = getElmById(`artifact-editor-location-label`);
if (copyLocationBtn === null) {
throw new Error("The location label is missing.");
}
copyLocationBtn.addEventListener(`click`, () =>
clipValue(`artifact-editor-location-value`)
);

const copyTempLocation = getElmById(`artifact-editor-templocation`);
if (copyTempLocation !== null) {
const copyTempLocationBtn = getElmById(
`artifact-editor-templocation-label`
);
copyTempLocationBtn.addEventListener(`click`, () =>
clipValue(`artifact-editor-templocation`)
);
}

const osLabel = document.getElementById("artifact-editor-os-label");
if (osLabel === null) {
throw new Error("The operating system label is missing.");
Expand Down
1 change: 1 addition & 0 deletions docs/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [ ] Handle magazines title on artifact page, http://localhost:1323/f/a55ed, this is hard to read, "Issue 4\nThe Pirate Syndicate +\nThe Pirate World"
- [ ] If artifact is a text file displayed in readme, then delete image preview, these are often super long, large and not needed.
- [ ] If a #hash is appended to a /f/<id> URL while signed out, then return a 404 or a redirect to the sign in page. Post signing should return to the #hash URL?
- [ ] Delete all previews that are unused, such as textfiles that are displayed as a readme.

- [ ] - http://www.platohistory.org/
- [ ] - https://portcommodore.com/dokuwiki/doku.php?id=larry:comp:bbs:about_cbbs
Expand Down
15 changes: 11 additions & 4 deletions handler/app/internal/mf/mf.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,24 @@ func isText(sign magicnumber.Signature) bool {
}

func entryHTML(images, programs, texts bool, rel, sign, size, unid string, bytes int64) string {
name := url.QueryEscape(rel)
htm := fmt.Sprintf(`<div class="col d-inline-block text-truncate" data-bs-toggle="tooltip" `+
`data-bs-title="%s">%s</div>`, rel, rel)
if images || texts {
htm += `<div class="col col-1 text-end"><svg width="16" height="16" fill="currentColor" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#images"></use></svg></div>`
if images {
htm += `<div class="col col-1 text-end">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" hx-patch="/editor/preview/copy/%s/%s">`, unid, name) +
`<svg width="16" height="16" fill="currentColor" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#images"></use></svg></a></div>`
} else if texts {
htm += `<div class="col col-1 text-end">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" hx-patch="/editor/readme/preview/%s/%s">`, unid, name) +
`<svg width="16" height="16" fill="currentColor" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#images"></use></svg></a></div>`
} else {
htm += `<div class="col col-1"></div>`
}
switch {
case texts:
name := url.QueryEscape(rel)
htm += `<div class="col col-1 text-end">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" hx-patch="/editor/readme/copy/%s/%s">`, unid, name) +
`<svg class="bi" width="16" height="16" fill="currentColor" aria-hidden="true">` +
Expand Down
16 changes: 6 additions & 10 deletions handler/app/internal/str/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func DemozooGetLink(filename, filesize, demozoo, unid any) template.HTML {
zooID = val.Int64
}
if zooID == 0 {
return "no demozoo ID provided"
return ""
}
var uID string
if val, valExists := unid.(null.String); valExists {
Expand All @@ -105,15 +105,11 @@ func DemozooGetLink(filename, filesize, demozoo, unid any) template.HTML {
uID = val.String
}
if uID == "" {
return "no unid provided"
}
s := fmt.Sprintf("<button type=\"button\" "+
"class=\"btn btn-outline-success me-2\" name=\"editorGetDemozoo\" "+
"data-id=\"%d\" data-uid=\"%s\" id=btn\"%s\">"+
`<svg width="16" height="16" fill="currentColor" aria-hidden="true">`+
`<use xlink:href="/svg/bootstrap-icons.svg#cloud-download"></use></svg>`+
" &nbsp; Use Demozoo</button>", zooID, uID, uID)
return template.HTML(s)
return ""
}
// s := fmt.Sprintf(`, <a href="" name="editorGetDemozoo"`+
// ` data-id="%d" data-uid="%s" id=btn"%s">Use demozoo assets</a>`, zooID, uID, uID)
return template.HTML(`clone the demozoo assets`)
}

// DownloadB returns a human readable string of the file size.
Expand Down
69 changes: 69 additions & 0 deletions handler/htmx/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/Defacto2/releaser"
"github.com/Defacto2/server/handler/app"
"github.com/Defacto2/server/internal/command"
"github.com/Defacto2/server/internal/demozoo"
"github.com/Defacto2/server/internal/form"
"github.com/Defacto2/server/internal/helper"
Expand All @@ -30,6 +31,74 @@ var (
ErrYT = errors.New("youtube watch video id needs to be empty or 11 characters")
)

func RecordImageCopier(c echo.Context, extraDir string) error {
path := c.Param("path")
name, err := url.QueryUnescape(path)
if err != nil {
return badRequest(c, err)
}
unid := c.Param("unid")
tmp, err := helper.MkContent(unid)
if err != nil {
return badRequest(c, err)
}
src := filepath.Join(tmp, name)
st, err := os.Stat(src)
if err != nil {
return badRequest(c, err)
}
if st.Size() == 0 {
return c.String(http.StatusOK, "The file is empty and was not copied.")
}
// TODO make images, png or avi?
// make thumbnails for images
// compress images

// dst := filepath.Join(extraDir, unid+".png")
// i, err := helper.DuplicateOW(src, dst)
// if err != nil {
// return badRequest(c, err)
// }
// c.Response().Header().Set("HX-Refresh", "true")
// return c.String(http.StatusOK, fmt.Sprintf("Copied %d bytes, refresh the browser.", i))
return c.String(http.StatusOK, "Not implemented.")
}

func RecordReadmeImager(c echo.Context, logger *zap.SugaredLogger, dirs command.Dirs) error {
path := c.Param("path")
name, err := url.QueryUnescape(path)
if err != nil {
return badRequest(c, err)
}
unid := c.Param("unid")
tmp, err := helper.MkContent(unid)
if err != nil {
return badRequest(c, err)
}
src := filepath.Join(tmp, name)
st, err := os.Stat(src)
if err != nil {
return badRequest(c, err)
}
if st.Size() == 0 {
return c.String(http.StatusOK, "The file is empty and was not used.")
}

if err := dirs.AnsiLove(logger, src, unid); err != nil {
return badRequest(c, err)
}
// TODO: REMOVE PNG created by ANSILOVE in working directory

return c.String(http.StatusOK, src)
// dst := filepath.Join(extraDir, unid+".txt")
// i, err := helper.DuplicateOW(src, dst)
// if err != nil {
// return badRequest(c, err)
// }
// c.Response().Header().Set("HX-Refresh", "true")
// return c.String(http.StatusOK, fmt.Sprintf("Copied %d bytes, refresh the browser.", i))
}

func RecordReadmeCopier(c echo.Context, extraDir string) error {
path := c.Param("path")
name, err := url.QueryUnescape(path)
Expand Down
17 changes: 17 additions & 0 deletions handler/routerlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Defacto2/server/handler/app"
"github.com/Defacto2/server/handler/htmx"
"github.com/Defacto2/server/internal/command"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -140,13 +141,29 @@ func editor(g *echo.Group, logger *zap.SugaredLogger, dir app.Dirs) {
return htmx.UploadReplacement(c, dir.Download)
})

dirs := command.Dirs{
Download: dir.Download,
Preview: dir.Preview,
Thumbnail: dir.Thumbnail,
}
me := g.Group("/readme")
me.PATCH("/copy/:unid/:path", func(c echo.Context) error {
return htmx.RecordReadmeCopier(c, dir.Extra)
})
me.PATCH("/preview/:unid/:path", func(c echo.Context) error {
return htmx.RecordReadmeImager(c, logger, dirs)
})
me.DELETE("/:unid", func(c echo.Context) error {
return htmx.RecordReadmeDeleter(c, dir.Extra)
})

pre := g.Group("/preview")
pre.PATCH("/copy/:unid/:path", func(c echo.Context) error {
return htmx.RecordImageCopier(c, dir.Extra)
})
pre.DELETE("/:unid", func(c echo.Context) error {
return htmx.RecordReadmeDeleter(c, dir.Extra)
})
}

func get(g *echo.Group, dir app.Dirs) {
Expand Down
20 changes: 7 additions & 13 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,8 @@ func LookVersion(name, flag, match string) error {

// Run looks for the command in the system path and executes it with the arguments.
// Any output to stderr is logged as a debug message.
func Run(logger *zap.SugaredLogger, name string, arg ...string) error {
if logger == nil {
return ErrZap
}
return run(logger, name, "", arg...)
func Run(debug *zap.SugaredLogger, name string, arg ...string) error {
return run(debug, name, "", arg...)
}

// RunOut looks for the command in the system path and executes it with the arguments.
Expand Down Expand Up @@ -288,14 +285,11 @@ func RunQuiet(name string, arg ...string) error {
// RunWD looks for the command in the system path and executes it with the arguments.
// An optional working directory is set for the command.
// Any output to stderr is logged as a debug message.
func RunWD(logger *zap.SugaredLogger, name, wdir string, arg ...string) error {
if logger == nil {
return ErrZap
}
return run(logger, name, wdir, arg...)
func RunWD(debug *zap.SugaredLogger, name, wdir string, arg ...string) error {
return run(debug, name, wdir, arg...)
}

func run(logger *zap.SugaredLogger, name, wdir string, arg ...string) error {
func run(debug *zap.SugaredLogger, name, wdir string, arg ...string) error {
if err := LookCmd(name); err != nil {
return fmt.Errorf("run %w", err)
}
Expand All @@ -312,8 +306,8 @@ func run(logger *zap.SugaredLogger, name, wdir string, arg ...string) error {
if err != nil {
return fmt.Errorf("run could not read stderr %w", err)
}
if len(b) > 0 {
logger.Debugf("run %q: %s\n", cmd, string(b))
if debug != nil && len(b) > 0 {
debug.Debugf("run %q: %s\n", cmd, string(b))
}

if err := cmd.Wait(); err != nil {
Expand Down
Loading

0 comments on commit 62daa10

Please sign in to comment.