mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
backend/mongo: use GridFS to stream large zips (#365)
* backend/mongo: use GridFS to stream large zips * fix tests
This commit is contained in:
@@ -3,28 +3,43 @@ package mongo
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/gomods/athens/pkg/errors"
|
||||
"github.com/gomods/athens/pkg/storage"
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
)
|
||||
|
||||
// Save stores a module in mongo storage.
|
||||
func (s *ModuleStore) Save(ctx context.Context, module, version string, mod []byte, zip io.Reader, info []byte) error {
|
||||
const op errors.Op = "mongo.Save"
|
||||
sp, ctx := opentracing.StartSpanFromContext(ctx, "storage.mongo.Save")
|
||||
defer sp.Finish()
|
||||
zipBytes, err := ioutil.ReadAll(zip)
|
||||
|
||||
zipName := s.gridFileName(module, version)
|
||||
fs := s.s.DB(s.d).GridFS("fs")
|
||||
f, err := fs.Create(zipName)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.E(op, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = io.Copy(f, zip) // check number of bytes written?
|
||||
if err != nil {
|
||||
return errors.E(op, err)
|
||||
}
|
||||
|
||||
m := &storage.Module{
|
||||
Module: module,
|
||||
Version: version,
|
||||
Mod: mod,
|
||||
Zip: zipBytes,
|
||||
Info: info,
|
||||
}
|
||||
|
||||
c := s.s.DB(s.d).C(s.c)
|
||||
return c.Insert(m)
|
||||
err = c.Insert(m)
|
||||
if err != nil {
|
||||
return errors.E(op, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user