File: metadata.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20200923.4646bfe-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,324 kB
  • sloc: makefile: 37
file content (120 lines) | stat: -rw-r--r-- 2,738 bytes parent folder | download | duplicates (3)
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
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package filestorage

import (
	"time"

	"github.com/juju/errors"
)

// RawDoc is a basic, uniquely identifiable document.
type RawDoc struct {
	// ID is the unique identifier for the document.
	ID string
}

// Doc wraps a document in the Document interface.
type Doc struct {
	Raw RawDoc
}

// ID returns the document's unique identifier.
func (d *Doc) ID() string {
	return d.Raw.ID
}

// SetID sets the document's unique identifier.  If the ID is already
// set, SetID() returns true (false otherwise).
func (d *Doc) SetID(id string) bool {
	if d.Raw.ID != "" {
		return true
	}
	d.Raw.ID = id
	return false
}

// RawFileMetadata holds info specific to stored files.
type RawFileMetadata struct {
	// Size is the size (in bytes) of the stored file.
	Size int64
	// Checksum is the checksum of the stored file.
	Checksum string
	// ChecksumFormat describes the kind of the checksum.
	ChecksumFormat string
	// Stored records the timestamp of when the file was last stored.
	Stored *time.Time
}

// FileMetadata contains the metadata for a single stored file.
type FileMetadata struct {
	Doc
	Raw RawFileMetadata
}

// NewMetadata returns a new Metadata for a stored file.
func NewMetadata() *FileMetadata {
	meta := FileMetadata{}
	return &meta
}

func (m *FileMetadata) Size() int64 {
	return m.Raw.Size
}

func (m *FileMetadata) Checksum() string {
	return m.Raw.Checksum
}

func (m *FileMetadata) ChecksumFormat() string {
	return m.Raw.ChecksumFormat
}

func (m *FileMetadata) Stored() *time.Time {
	return m.Raw.Stored
}

func (m *FileMetadata) SetFileInfo(size int64, checksum, format string) error {
	// Fall back to existing values.
	if size == 0 {
		size = m.Raw.Size
	}
	if checksum == "" {
		checksum = m.Raw.Checksum
	}
	if format == "" {
		format = m.Raw.ChecksumFormat
	}
	if checksum != "" {
		if format == "" {
			return errors.Errorf("missing checksum format")
		}
	} else if format != "" {
		return errors.Errorf("missing checksum")
	}
	// Only allow setting once.
	if m.Raw.Size != 0 && size != m.Raw.Size {
		return errors.Errorf("file information (size) already set")
	}
	if m.Raw.Checksum != "" && checksum != m.Raw.Checksum {
		return errors.Errorf("file information (checksum) already set")
	}
	if m.Raw.ChecksumFormat != "" && format != m.Raw.ChecksumFormat {
		return errors.Errorf("file information (checksum format) already set")
	}
	// Set the values.
	m.Raw.Size = size
	m.Raw.Checksum = checksum
	m.Raw.ChecksumFormat = format
	return nil
}

func (m *FileMetadata) SetStored(timestamp *time.Time) {
	if timestamp == nil {
		now := time.Now().UTC()
		m.Raw.Stored = &now
	} else {
		m.Raw.Stored = timestamp
	}
}