This repository has been archived on 2025-06-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
server/fileexporter/archive.go
Pierre-Olivier Mercier ac5982f905
All checks were successful
continuous-integration/drone/push Build is passing
fileexporter: Close opened fd
2025-05-07 14:01:12 +02:00

58 lines
1 KiB
Go

package main
import (
"archive/zip"
"errors"
"io"
"log"
"os"
"path"
)
type archiveFileCreator interface {
Create(name string) (io.Writer, error)
Close() error
}
type filesCloser []io.Closer
func (fds filesCloser) Close() error {
log.Println("Closing fd..")
for _, fd := range fds {
err := fd.Close()
if err != nil {
return err
}
}
return nil
}
func init() {
OutputFormats["archive"] = func(args ...string) (func(string) (io.WriteCloser, error), io.Closer, error) {
if len(args) != 1 {
return nil, nil, errors.New("archive has 1 required argument: [destination-file]")
}
fd, err := os.Create(args[0])
if err != nil {
return nil, nil, err
}
var w archiveFileCreator
if path.Ext(args[0]) == ".zip" {
w = zip.NewWriter(fd)
} else {
return nil, nil, errors.New("destination file has to have .zip extension")
}
return func(dest string) (io.WriteCloser, error) {
fw, err := w.Create(dest)
if err != nil {
return nil, err
}
return NopCloser(fw), nil
}, filesCloser{w, fd}, nil
}
}