press: use json for metadata

This commit is contained in:
buengese 2020-09-28 21:53:41 +02:00
parent f3779ae427
commit 2d77521cd3

View File

@ -4,12 +4,11 @@ package compress
import (
"bufio"
"bytes"
"compress/gzip"
"context"
"crypto/md5"
"encoding/binary"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
@ -352,7 +351,7 @@ func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
return nil, errors.New("error decoding metadata")
}
// Create our Object
o, err := f.Fs.NewObject(ctx, makeDataName(remote, meta.Size, meta.Mode))
o, err := f.Fs.NewObject(ctx, makeDataName(remote, meta.CompressionMetadata.Size, meta.Mode))
return f.newObject(o, mo, meta), err
}
@ -495,7 +494,7 @@ func (f *Fs) putCompress(ctx context.Context, in io.Reader, src fs.ObjectInfo, o
}
// Generate metadata
meta := newMetadata(result.meta.Size, f.mode, result.meta, metaHasher.Sum(nil), mimeType)
meta := newMetadata(result.meta.Size, f.mode, result.meta, hex.EncodeToString(metaHasher.Sum(nil)), mimeType)
// Check the hashes of the compressed data if we were comparing them
if ht != hash.None && hasher != nil {
@ -549,27 +548,20 @@ func (f *Fs) putUncompress(ctx context.Context, in io.Reader, src fs.ObjectInfo,
if err != nil {
return nil, nil, err
}
return o, newMetadata(o.Size(), Uncompressed, sgzip.GzipMetadata{}, sum, mimeType), nil
return o, newMetadata(o.Size(), Uncompressed, sgzip.GzipMetadata{}, hex.EncodeToString(sum), mimeType), nil
}
// This function will write a metadata struct to a metadata Object for an src. Returns a wrappable metadata object.
func (f *Fs) putMetadata(ctx context.Context, meta *ObjectMetadata, src fs.ObjectInfo, options []fs.OpenOption, put putFn) (mo fs.Object, err error) {
// Generate the metadata contents
var b bytes.Buffer
gzipWriter := gzip.NewWriter(&b)
metadataEncoder := gob.NewEncoder(gzipWriter)
err = metadataEncoder.Encode(meta)
data, err := json.Marshal(meta)
if err != nil {
return nil, err
}
err = gzipWriter.Close()
if err != nil {
return nil, err
}
metaReader := bytes.NewReader(b.Bytes())
metaReader := bytes.NewReader(data)
// Put the data
mo, err = put(ctx, metaReader, f.wrapInfo(src, makeMetadataName(src.Remote()), int64(b.Len())), options...)
mo, err = put(ctx, metaReader, f.wrapInfo(src, makeMetadataName(src.Remote()), int64(len(data))), options...)
if err != nil {
removeErr := mo.Remove(ctx)
if removeErr != nil {
@ -943,7 +935,7 @@ func (f *Fs) PublicLink(ctx context.Context, remote string, duration fs.Duration
// ObjectMetadata describes the metadata for an Object.
type ObjectMetadata struct {
Mode int // Compression mode of the file.
Hash []byte // MD5 hash of the file.
MD5 string // MD5 hash of the file.
MimeType string // Mime type of the file
CompressionMetadata sgzip.GzipMetadata
}
@ -959,12 +951,11 @@ type Object struct {
}
// This function generates a metadata object
func newMetadata(size int64, mode int, cmeta sgzip.GzipMetadata, hash []byte, mimeType string) *ObjectMetadata {
func newMetadata(size int64, mode int, cmeta sgzip.GzipMetadata, md5 string, mimeType string) *ObjectMetadata {
meta := new(ObjectMetadata)
meta.Size = size
meta.Mode = mode
meta.CompressionMetadata = cmeta
meta.Hash = hash
meta.MD5 = md5
meta.MimeType = mimeType
return meta
}
@ -982,18 +973,9 @@ func readMetadata(ctx context.Context, mo fs.Object) (meta *ObjectMetadata) {
fs.Errorf(mo, "Error closing object: %v", err)
}
}()
// Read gzipped compressed data from it
gzipReader, err := gzip.NewReader(rc)
if err != nil {
return nil
}
// Decode the gob from that
jr := json.NewDecoder(rc)
meta = new(ObjectMetadata)
metadataDecoder := gob.NewDecoder(gzipReader)
err = metadataDecoder.Decode(meta)
// Cleanup and return
_ = gzipReader.Close() // We don't really care if this is closed properly
if err != nil {
if err = jr.Decode(meta); err != nil {
return nil
}
return meta
@ -1071,7 +1053,7 @@ func (f *Fs) newObject(o fs.Object, mo fs.Object, meta *ObjectMetadata) *Object
f: f,
mo: mo,
moName: mo.Remote(),
size: meta.Size,
size: meta.CompressionMetadata.Size,
meta: meta,
}
}
@ -1136,7 +1118,7 @@ func (o *Object) Size() int64 {
if o.meta == nil {
return o.size
}
return o.meta.Size
return o.meta.CompressionMetadata.Size
}
// MimeType returns the MIME type of the file
@ -1158,7 +1140,7 @@ func (o *Object) Hash(ctx context.Context, ht hash.Type) (string, error) {
if err != nil {
return "", err
}
return hex.EncodeToString(o.meta.Hash), nil
return o.meta.MD5, nil
}
// SetTier performs changing storage tier of the Object if