internal/storage/files/writer.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | package files import ( "compress/gzip" "io" "os" "path/filepath" "time" "go.alanpearce.eu/website/internal/buffer" "go.alanpearce.eu/website/internal/storage" "go.alanpearce.eu/x/log" "github.com/andybalholm/brotli" "gitlab.com/tozd/go/errors" ) const ( gzipLevel = 6 brotliLevel = 9 ) type Files struct { outputDirectory string options *Options log *log.Logger } type Options struct { Compress bool } func NewWriter(outputDirectory string, logger *log.Logger, opts *Options) (*Files, error) { return &Files{ outputDirectory: outputDirectory, options: opts, log: logger, }, nil } func (f *Files) NewFile(path string) *storage.File { return &storage.File{ Path: path, ContentType: "", LastModified: time.Time{}, Etag: "", Encodings: map[string]*buffer.Buffer{ "identity": nil, }, } } func (f *Files) OpenRead(filename string) (io.ReadCloser, error) { return os.Open(f.join(filename)) } func (f *Files) OpenWrite(filename string) (io.WriteCloser, error) { return openFileWrite(f.join(filename)) } func (f *Files) Write(pathname string, content *buffer.Buffer) error { filename := pathNameToFileName(pathname) err := f.Mkdirp(filepath.Dir(filename)) if err != nil { return errors.WithMessage(err, "could not create directory") } fd, err := openFileWrite(f.join(filename)) if err != nil { return errors.WithMessagef(err, "could not open output file") } defer fd.Close() _, err = fd.Write(content.Bytes()) if err != nil { return errors.WithMessage(err, "could not write output file") } return nil } type MultiWriteCloser struct { writers []io.WriteCloser multiWriter io.Writer } func (mw *MultiWriteCloser) Write(p []byte) (n int, err error) { return mw.multiWriter.Write(p) } func (mw *MultiWriteCloser) Close() error { var lastErr error for _, w := range mw.writers { err := w.Close() if err != nil { lastErr = err } } return lastErr } func openFileWrite(filename string) (*os.File, error) { return os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) } func openFileGz(filename string) (*gzip.Writer, error) { filenameGz := filename + ".gz" f, err := openFileWrite(filenameGz) if err != nil { return nil, err } return gzip.NewWriterLevel(f, gzipLevel) } func openFileBrotli(filename string) (*brotli.Writer, error) { filenameBrotli := filename + ".br" f, err := openFileWrite(filenameBrotli) if err != nil { return nil, err } return brotli.NewWriterLevel(f, brotliLevel), nil } func multiOpenFile(filename string) (*MultiWriteCloser, error) { r, err := openFileWrite(filename) if err != nil { return nil, err } gz, err := openFileGz(filename) if err != nil { return nil, err } br, err := openFileBrotli(filename) if err != nil { return nil, err } return &MultiWriteCloser{ writers: []io.WriteCloser{r, gz, br}, multiWriter: io.MultiWriter(r, gz, br), }, nil } func (f *Files) OpenFileAndVariants(filename string) (io.WriteCloser, error) { if f.options.Compress { return multiOpenFile(f.join(filename)) } return openFileWrite(f.join(filename)) } func (f *Files) Mkdirp(dir string) error { f.log.Debug("creating directory", "dir", dir) err := os.MkdirAll(f.join(dir), 0755) return errors.WithMessage(err, "could not create directory") } func (f *Files) join(filename string) string { return filepath.Join(f.outputDirectory, pathNameToFileName(filename)) } |