0.1.6 11bec3a

upload-image.go RawHistoryFolder

1package handler
2
3import (
4	"fmt"
5	"io"
6	"net/http"
7	"os"
8
9	"github.com/bnema/gordon/internal/server"
10	"github.com/bnema/gordon/internal/templating/render"
11	"github.com/bnema/gordon/pkg/docker"
12	"github.com/bnema/gordon/pkg/store"
13	"github.com/labstack/echo/v4"
14)
15
16// 10G
17const MaxUploadSize = 10 * 1024 * 1024 * 1024 // 10GB
18
19// UploadImageHandler handles the /upload-image route to show the form
20func UploadImageGETHandler(c echo.Context, a *server.App) error {
21	data := map[string]interface{}{
22		"Title": "Upload Image",
23	}
24
25	rendererData, err := render.GetHTMLRenderer("html/fragments", "uploadimage.gohtml", a.TemplateFS, a)
26	if err != nil {
27		return sendError(c, err)
28	}
29
30	renderedHTML, err := rendererData.Render(data, a)
31	if err != nil {
32		return err
33	}
34	return c.HTML(200, renderedHTML)
35}
36
37// UploadImageHandler handles the /upload-image
38func UploadImagePOSTHandler(c echo.Context, a *server.App) error {
39	// Set upload size limit
40	c.Request().Body = http.MaxBytesReader(c.Response(), c.Request().Body, MaxUploadSize)
41
42	// Parse the multipart form
43	form, err := c.MultipartForm()
44	if err != nil {
45		return sendError(c, err)
46	}
47
48	// Get the file from the form
49	files := form.File["file"]
50	if len(files) == 0 {
51		return c.String(http.StatusBadRequest, "No file uploaded")
52	}
53	file := files[0]
54
55	// Open the file
56	src, err := file.Open()
57	if err != nil {
58		return sendError(c, err)
59	}
60	defer src.Close()
61
62	// Create a temporary file to store the uploaded chunks
63	tempFile, err := os.CreateTemp("", "upload-*")
64	if err != nil {
65		return sendError(c, err)
66	}
67	defer tempFile.Close()
68
69	// Copy the file chunks to the temporary file
70	_, err = io.Copy(tempFile, src)
71	if err != nil {
72		return sendError(c, err)
73	}
74
75	// Reset the temporary file pointer
76	_, err = tempFile.Seek(0, io.SeekStart)
77	if err != nil {
78		return sendError(c, err)
79	}
80
81	// Save the image to the storage directory
82	saveInPath, err := store.SaveImageToStorage(&a.Config, file.Filename, tempFile)
83	if err != nil {
84		return sendError(c, err)
85	}
86
87	// Import the image into Docker
88	_, err = docker.ImportImageToEngine(saveInPath)
89	if err != nil {
90		return fmt.Errorf("failed to import image to Docker: %v", err)
91	}
92
93	// Remove the image from the storage directory
94	err = store.RemoveFromStorage(saveInPath)
95	if err != nil {
96		return sendError(c, err)
97	}
98
99	return c.HTML(http.StatusOK, ActionSuccess(a))
100}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close