Reworked the way metadata/tags for files work. Simplified it at lot.

This commit is contained in:
Kleissner
2021-09-28 02:59:47 +02:00
parent 24df8c1eb0
commit 9ef6a88512
7 changed files with 177 additions and 240 deletions

View File

@@ -107,17 +107,14 @@ type apiBlockRecordProfileBlob struct {
Data []byte `json:"data"` // The data
}
// apiFileMetadata describes recognized metadata that is decoded into text.
// apiFileMetadata contains metadata information.
type apiFileMetadata struct {
Type uint16 `json:"type"` // See core.TagTypeX constants.
Name string `json:"name"` // User friendly name of the tag. Use the Type fields to identify the metadata as this name may change.
Value string `json:"value"` // Text value of the tag.
}
// apiFileTagRaw describes a raw tag. This allows to support future metadata that is not yet defined in the core library.
type apiFileTagRaw struct {
Type uint16 `json:"type"` // See core.TagTypeX constants.
Data []byte `json:"data"` // Data
Type uint16 `json:"type"` // See core.TagX constants.
Name string `json:"name"` // User friendly name of the metadata type. Use the Type fields to identify the metadata as this name may change.
// Depending on the exact type, one of the below fields is used for proper encoding:
Text string `json:"text"` // Text value. UTF-8 encoding.
Blob []byte `json:"blob"` // Binary data
Date time.Time `json:"date"` // Date
}
// apiBlockRecordFile is the metadata of a file published on the blockchain
@@ -131,12 +128,7 @@ type apiBlockRecordFile struct {
Name string `json:"name"` // Name of the file
Description string `json:"description"` // Description. This is expected to be multiline and contain hashtags!
Date time.Time `json:"date"` // Date of the virtual file
Metadata []apiFileMetadata `json:"metadata"` // Metadata. These are decoded tags.
TagsRaw []apiFileTagRaw `json:"tagsraw"` // All tags encoded that were not recognized as metadata.
// The following known tags from the core library are decoded into metadata or other fields in above structure; everything else is a raw tag:
// TagTypeName, TagTypeFolder, TagTypeDescription, TagTypeDateCreated
// The caller can specify its own metadata fields and fill the TagsRaw structure when creating a new file. It will be returned when reading the files' data.
Metadata []apiFileMetadata `json:"metadata"` // Additional metadata.
}
/*
@@ -256,44 +248,29 @@ func apiBlockchainSelfDeleteFile(w http.ResponseWriter, r *http.Request) {
// --- conversion from core to API data ---
func isFileTagKnownMetadata(tagType uint16) bool {
switch tagType {
case core.TagTypeName, core.TagTypeFolder, core.TagTypeDescription, core.TagTypeDateCreated, core.TagTypeDateShared:
return true
default:
return false
}
}
func blockRecordFileToAPI(input core.BlockRecordFile) (output apiBlockRecordFile) {
output = apiBlockRecordFile{ID: input.ID, Hash: input.Hash, Type: input.Type, Format: input.Format, Size: input.Size, TagsRaw: []apiFileTagRaw{}, Metadata: []apiFileMetadata{}}
output = apiBlockRecordFile{ID: input.ID, Hash: input.Hash, Type: input.Type, Format: input.Format, Size: input.Size, Metadata: []apiFileMetadata{}}
// Copy all raw tags. This allows the API caller to decode any future tags that are not defined yet.
for n := range input.TagsRaw {
if !isFileTagKnownMetadata(input.TagsRaw[n].Type) {
output.TagsRaw = append(output.TagsRaw, apiFileTagRaw{Type: input.TagsRaw[n].Type, Data: input.TagsRaw[n].Data})
}
}
for _, tag := range input.Tags {
switch tag.Type {
case core.TagName:
output.Name = tag.Text()
// Try to decode tags into known metadata.
for _, tagDecoded := range input.TagsDecoded {
switch v := tagDecoded.(type) {
case core.FileTagName:
output.Name = v.Name
case core.TagFolder:
output.Folder = tag.Text()
case core.FileTagFolder:
output.Folder = v.Name
case core.TagDescription:
output.Description = tag.Text()
case core.FileTagDescription:
output.Description = v.Description
case core.TagDateShared:
output.Date, _ = tag.Date()
case core.FileTagDateCreated:
output.Metadata = append(output.Metadata, apiFileMetadata{Type: core.TagTypeDateCreated, Name: "Date Created", Value: v.Date.Format(dateFormat)})
case core.FileTagDateShared:
output.Date = v.Date
case core.TagDateCreated:
date, _ := tag.Date()
output.Metadata = append(output.Metadata, apiFileMetadata{Type: tag.Type, Name: "Date Created", Date: date})
default:
output.Metadata = append(output.Metadata, apiFileMetadata{Type: tag.Type, Blob: tag.Data})
}
}
@@ -304,27 +281,24 @@ func blockRecordFileFromAPI(input apiBlockRecordFile) (output core.BlockRecordFi
output = core.BlockRecordFile{ID: input.ID, Hash: input.Hash, Type: input.Type, Format: input.Format, Size: input.Size}
if input.Name != "" {
output.TagsDecoded = append(output.TagsDecoded, core.FileTagName{Name: input.Name})
output.Tags = append(output.Tags, core.TagFromText(core.TagName, input.Name))
}
if input.Folder != "" {
output.TagsDecoded = append(output.TagsDecoded, core.FileTagFolder{Name: input.Folder})
output.Tags = append(output.Tags, core.TagFromText(core.TagFolder, input.Folder))
}
if input.Description != "" {
output.TagsDecoded = append(output.TagsDecoded, core.FileTagDescription{Description: input.Description})
output.Tags = append(output.Tags, core.TagFromText(core.TagDescription, input.Description))
}
for _, tag := range input.Metadata {
switch tag.Type {
case core.TagTypeDateCreated:
if dateF, err := time.Parse(dateFormat, tag.Value); err == nil {
output.TagsDecoded = append(output.TagsDecoded, core.FileTagDateCreated{Date: dateF})
}
}
}
for _, meta := range input.Metadata {
switch meta.Type {
case core.TagName, core.TagFolder, core.TagDescription, core.TagDateShared:
for n := range input.TagsRaw {
if !isFileTagKnownMetadata(input.TagsRaw[n].Type) {
output.TagsRaw = append(output.TagsRaw, core.BlockRecordFileTag{Type: input.TagsRaw[n].Type, Data: input.TagsRaw[n].Data})
case core.TagDateCreated:
output.Tags = append(output.Tags, core.TagFromDate(meta.Type, meta.Date))
default:
output.Tags = append(output.Tags, core.BlockRecordFileTag{Type: meta.Type, Data: meta.Blob})
}
}

View File

@@ -281,9 +281,9 @@ func createTestResult(fileType int) (file core.BlockRecordFile) {
extension = ".bin"
}
file.TagsDecoded = append(file.TagsDecoded, core.FileTagName{Name: TempFileName("", extension)})
//file.TagsDecoded = append(file.TagsDecoded, core.FileTagFolder{Name: "not set"})
file.TagsDecoded = append(file.TagsDecoded, core.FileTagDateShared{Date: time.Now().UTC()})
file.Tags = append(file.Tags, core.TagFromText(core.TagName, TempFileName("", extension)))
//file.Tags = append(file.Tags, core.TagFromText(core.TagFolder, "not set"))
file.Tags = append(file.Tags, core.TagFromDate(core.TagDateShared, time.Now().UTC()))
return
}

View File

@@ -184,6 +184,10 @@ type apiBlockRecordProfileBlob struct {
}
```
## File Functions
These functions allow adding, deleting, and listing files stored on the users blockchain. Only metadata is actually stored on the blockchain.
```go
type apiBlockRecordFile struct {
ID uuid.UUID `json:"id"` // Unique ID.
@@ -195,29 +199,28 @@ type apiBlockRecordFile struct {
Name string `json:"name"` // Name of the file
Description string `json:"description"` // Description. This is expected to be multiline and contain hashtags!
Date time.Time `json:"date"` // Date of the virtual file
Metadata []apiFileMetadata `json:"metadata"` // Metadata. These are decoded tags.
TagsRaw []apiFileTagRaw `json:"tagsraw"` // All tags encoded that were not recognized as metadata.
// The following known tags from the core library are decoded into metadata or other fields in above structure; everything else is a raw tag:
// TagTypeName, TagTypeFolder, TagTypeDescription, TagTypeDateCreated
// The caller can specify its own metadata fields and fill the TagsRaw structure when creating a new file. It will be returned when reading the files' data.
Metadata []apiFileMetadata `json:"metadata"` // Additional metadata.
}
type apiFileMetadata struct {
Type uint16 `json:"type"` // See core.TagTypeX constants.
Name string `json:"name"` // User friendly name of the tag. Use the Type fields to identify the metadata as this name may change.
Value string `json:"value"` // Text value of the tag.
}
type apiFileTagRaw struct {
Type uint16 `json:"type"` // See core.TagTypeX constants.
Data []byte `json:"data"` // Data
Type uint16 `json:"type"` // See core.TagX constants.
Name string `json:"name"` // User friendly name of the metadata type. Use the Type fields to identify the metadata as this name may change.
// Depending on the exact type, one of the below fields is used for proper encoding:
Text string `json:"text"` // Text value. UTF-8 encoding.
Blob []byte `json:"blob"` // Binary data
Date time.Time `json:"date"` // Date
}
```
## File Functions
Below is the list of defined metadata types. Undefined types may be used by clients, but are always mapped into the `blob` field.
These functions allow adding, deleting, and listing files stored on the users blockchain. Only metadata is actually stored on the blockchain.
| Type | Constant | Encoding | Info |
|------|----------------|----------|------------------------------------------------------------------------------------------------------------------------|
| 0 | TagName | Text | Mapped into Name field. Name of file. |
| 1 | TagFolder | Text | Mapped into Folder field. Folder name. |
| 2 | TagDescription | Text | Mapped into Description field. Arbitrary description of the file. May contain hashtags. |
| 3 | TagDateShared | Date | Mapped into Date field. When the file was published on the blockchain. Cannot be set manually (virtual read-only tag). |
| 4 | TagDateCreated | Date | Date when the file was originally created. |
### Add File
@@ -249,8 +252,7 @@ Example POST request to `http://127.0.0.1:112/blockchain/self/add/file`:
"name": "Test.txt",
"folder": "sample directory/sub folder",
"description": "",
"metadata": [],
"tagsraw": []
"metadata": []
}]
}
```
@@ -270,11 +272,7 @@ Another payload example to create a new file but with a new arbitrary tag with t
"description": "Example description\nThis can be any text #newfile #2021.",
"metadata": [{
"type": 2,
"value": "2021-08-28 00:00:00"
}],
"tagsraw": [{
"type": 100,
"data": "dGVzdA=="
"date": "2021-08-28T00:00:00Z"
}]
}]
}
@@ -302,9 +300,25 @@ Example response:
"folder": "sample directory/sub folder",
"name": "Test.txt",
"description": "",
"date": "2021-08-27T16:59:13+02:00",
"metadata": [],
"tagsraw": []
"date": "2021-08-27T14:59:13Z",
"metadata": []
}, {
"id": "bc32cbae-011d-4f0b-80a8-281ca9369211",
"hash": "aFad3zRACbk44dsOw5sVGxYmz+Rqh8ORDcGJNqIz+Ss=",
"type": 1,
"format": 10,
"size": 4,
"folder": "sample directory/sub folder",
"name": "Test 2.txt",
"description": "Example description\nThis can be any text #newfile #2021.",
"date": "2021-09-27T23:33:37Z",
"metadata": [{
"type": 2,
"name": "Date Created",
"text": "",
"blob": null,
"date": "2021-08-28T00:00:00Z"
}]
}],
"status": 0
}
@@ -567,8 +581,7 @@ Example response with dummy data:
"name": "88d8cc57d5c2a5fea881ceea09503ee4.txt",
"description": "",
"date": "2021-09-23T00:00:00Z",
"metadata": [],
"tagsraw": []
"metadata": []
}]
}
```