|
| 1 | +package web |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/amir20/dozzle/internal/auth" |
| 12 | + "github.com/amir20/dozzle/internal/docker" |
| 13 | + "github.com/docker/docker/pkg/stdcopy" |
| 14 | + "github.com/go-chi/chi/v5" |
| 15 | +) |
| 16 | + |
| 17 | +func (h *handler) downloadLogs(w http.ResponseWriter, r *http.Request) { |
| 18 | + hostIds := strings.Split(chi.URLParam(r, "hostIds"), ",") |
| 19 | + if len(hostIds) == 0 { |
| 20 | + http.Error(w, "no container ids provided", http.StatusBadRequest) |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + usersFilter := h.config.Filter |
| 25 | + if h.config.Authorization.Provider != NONE { |
| 26 | + user := auth.UserFromContext(r.Context()) |
| 27 | + if user.ContainerFilter.Exists() { |
| 28 | + usersFilter = user.ContainerFilter |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + now := time.Now() |
| 33 | + nowFmt := now.Format("2006-01-02T15-04-05") |
| 34 | + |
| 35 | + var stdTypes docker.StdType |
| 36 | + if r.URL.Query().Has("stdout") { |
| 37 | + stdTypes |= docker.STDOUT |
| 38 | + } |
| 39 | + if r.URL.Query().Has("stderr") { |
| 40 | + stdTypes |= docker.STDERR |
| 41 | + } |
| 42 | + |
| 43 | + if stdTypes == 0 { |
| 44 | + http.Error(w, "stdout or stderr is required", http.StatusBadRequest) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + // Set headers for zip file |
| 49 | + w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=container-logs-%s.zip", nowFmt)) |
| 50 | + w.Header().Set("Content-Type", "application/zip") |
| 51 | + |
| 52 | + // Create zip writer |
| 53 | + zw := zip.NewWriter(w) |
| 54 | + defer zw.Close() |
| 55 | + |
| 56 | + // Process each container |
| 57 | + for _, hostId := range hostIds { |
| 58 | + parts := strings.Split(hostId, ":") |
| 59 | + if len(parts) != 2 { |
| 60 | + http.Error(w, fmt.Sprintf("invalid host id: %s", hostId), http.StatusBadRequest) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + host := parts[0] |
| 65 | + id := parts[1] |
| 66 | + containerService, err := h.multiHostService.FindContainer(host, id, usersFilter) |
| 67 | + if err != nil { |
| 68 | + http.Error(w, fmt.Sprintf("error finding container %s: %v", id, err), http.StatusBadRequest) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + // Create new file in zip for this container's logs |
| 73 | + fileName := fmt.Sprintf("%s-%s.log", containerService.Container.Name, nowFmt) |
| 74 | + f, err := zw.Create(fileName) |
| 75 | + if err != nil { |
| 76 | + http.Error(w, fmt.Sprintf("error creating zip entry: %v", err), http.StatusInternalServerError) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + // Get container logs |
| 81 | + reader, err := containerService.RawLogs(r.Context(), time.Time{}, now, stdTypes) |
| 82 | + if err != nil { |
| 83 | + http.Error(w, fmt.Sprintf("error getting logs for container %s: %v", id, err), http.StatusInternalServerError) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + // Copy logs directly to zip entry |
| 88 | + if containerService.Container.Tty { |
| 89 | + if _, err := io.Copy(f, reader); err != nil { |
| 90 | + http.Error(w, fmt.Sprintf("error copying logs for container %s: %v", id, err), http.StatusInternalServerError) |
| 91 | + return |
| 92 | + } |
| 93 | + } else { |
| 94 | + if _, err := stdcopy.StdCopy(f, f, reader); err != nil { |
| 95 | + http.Error(w, fmt.Sprintf("error copying logs for container %s: %v", id, err), http.StatusInternalServerError) |
| 96 | + return |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments