Skip to content

Commit ca82a85

Browse files
committed
Files' digest is calculated when they're added
1 parent e8935d5 commit ca82a85

File tree

6 files changed

+102
-34
lines changed

6 files changed

+102
-34
lines changed

crypto/metadata.pb.go

+8-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crypto/metadata.proto

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

1818
/*
19-
//Build with:
19+
Build with:
2020
2121
```sh
2222
protoc \
@@ -38,7 +38,10 @@ option go_package = ".;crypto";
3838

3939
// Metadata message
4040
message Metadata {
41+
// File's original name
4142
string name = 1 [json_name="n"];
43+
// File's content type
4244
string content_type = 2 [json_name="ct"];
45+
// File size
4346
int64 size = 3 [json_name="sz"];
4447
}

index/index.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type FolderList struct {
3939
FileId string `json:"fileId,omitempty"`
4040
Date *time.Time `json:"date,omitempty"`
4141
MimeType string `json:"mimeType,omitempty"`
42+
Digest []byte `json:"digest,omitempty"`
43+
Size int64 `json:"size,omitempty"`
4244
}
4345

4446
// IndexStats contains the result of the
@@ -173,7 +175,7 @@ func (i *Index) save(obj *pb.IndexFile) error {
173175
}
174176

175177
// AddFile adds a file to the index
176-
func (i *Index) AddFile(path string, fileId []byte, mimeType string) error {
178+
func (i *Index) AddFile(path string, fileId []byte, mimeType string, size int64, digest []byte) error {
177179
// path must be at least 2 characters (with / being one)
178180
if len(path) < 2 {
179181
return errors.New("path name is too short")
@@ -186,6 +188,14 @@ func (i *Index) AddFile(path string, fileId []byte, mimeType string) error {
186188
if strings.HasSuffix(path, "/") {
187189
return errors.New("path must not end with /")
188190
}
191+
// File size must not be negative (but can be empty)
192+
if size < 0 {
193+
return errors.New("invalid file size")
194+
}
195+
// If the digest is empty, ensure it's null
196+
if len(digest) < 1 {
197+
digest = nil
198+
}
189199

190200
// Force a refresh of the index
191201
if err := i.Refresh(true); err != nil {
@@ -210,6 +220,8 @@ func (i *Index) AddFile(path string, fileId []byte, mimeType string) error {
210220
Seconds: time.Now().Unix(),
211221
},
212222
MimeType: mimeType,
223+
Size: size,
224+
Digest: digest,
213225
}
214226
elements := append(i.cache.Elements, fileEl)
215227

@@ -317,6 +329,8 @@ func (i *Index) GetFileById(fileId string) (*FolderList, error) {
317329
FileId: fileId,
318330
Date: date,
319331
MimeType: el.MimeType,
332+
Size: el.Size,
333+
Digest: el.Digest,
320334
}, nil
321335
}
322336

@@ -447,6 +461,8 @@ func (i *Index) ListFolder(path string) ([]FolderList, error) {
447461
FileId: fileId.String(),
448462
Date: date,
449463
MimeType: el.File.MimeType,
464+
Size: el.File.Size,
465+
Digest: el.File.Digest,
450466
}
451467
y++
452468
} else {

index/proto/index.pb.go

+49-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index/proto/index.proto

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

1818
/*
19-
//Build with:
19+
Build with:
2020
2121
```sh
2222
protoc \
@@ -40,10 +40,18 @@ option go_package = "github.com/ItalyPaleAle/index/proto";
4040

4141
// IndexElement message
4242
message IndexElement {
43+
// File's full path (directory + name)
4344
string path = 1 [json_name="p"];
45+
// File ID (UUID), encoded as raw bytes
4446
bytes file_id = 2;
47+
// Date when the file was added to the repository
4548
google.protobuf.Timestamp date = 3 [json_name="d"];
49+
// File's content type
4650
string mime_type = 4 [json_name="m"];
51+
// File size
52+
int64 size = 5;
53+
// File digest as SHA-256
54+
bytes digest = 10;
4755

4856
// This is added for compatibility with the version 1 JSON format
4957
string file_id_string = 102 [json_name="n",deprecated=true];

repository/add.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package repository
1919

2020
import (
2121
"context"
22+
"crypto/sha256"
2223
"errors"
2324
"io"
2425
"os"
@@ -31,6 +32,9 @@ import (
3132
"github.com/gofrs/uuid"
3233
)
3334

35+
// Flag that controls whether SHA-256 digests are calculated for files
36+
var CalculateDigest = true
37+
3438
// AddStream adds a document to the repository by reading it from a stream
3539
func (repo *Repository) AddStream(ctx context.Context, in io.ReadCloser, filename, destinationFolder, mimeType string, size int64) (fileIdStr string, status int, err error) {
3640
// Generate a file id
@@ -56,19 +60,28 @@ func (repo *Repository) AddStream(ctx context.Context, in io.ReadCloser, filenam
5660
return "", RepositoryStatusExisting, nil
5761
}
5862

63+
// Create the hash
64+
hash := sha256.New()
65+
66+
// Tee the in stream into the hash
67+
tee := io.TeeReader(in, hash)
68+
5969
// Write the data to an encrypted file
6070
metadata := &crypto.Metadata{
6171
Name: sanitizedFilename,
6272
ContentType: mimeType,
6373
Size: size,
6474
}
65-
_, err = repo.Store.Set(ctx, fileId.String(), in, nil, metadata)
75+
_, err = repo.Store.Set(ctx, fileId.String(), tee, nil, metadata)
6676
if err != nil {
6777
return "", RepositoryStatusInternalError, err
6878
}
6979

80+
// Complete the file's digest
81+
digest := hash.Sum(nil)
82+
7083
// Add to the index
71-
err = repo.Index.AddFile(sanitizedPath, fileId.Bytes(), mimeType)
84+
err = repo.Index.AddFile(sanitizedPath, fileId.Bytes(), mimeType, size, digest)
7285
if err != nil {
7386
return "", RepositoryStatusInternalError, err
7487
}

0 commit comments

Comments
 (0)