mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
Reworked the way metadata/tags for files work. Simplified it at lot.
This commit is contained in:
154
Block Records.go
154
Block Records.go
@@ -32,7 +32,6 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -52,62 +51,23 @@ const (
|
||||
|
||||
// BlockRecordFile is the metadata of a file published on the blockchain
|
||||
type BlockRecordFile struct {
|
||||
Hash []byte // Hash of the file data
|
||||
ID uuid.UUID // ID
|
||||
Type uint8 // File Type
|
||||
Format uint16 // File Format
|
||||
Size uint64 // Size of the file data
|
||||
TagsRaw []BlockRecordFileTag // Tags to provide additional metadata
|
||||
TagsDecoded []interface{} // Decoded tags. See FileTagX structures.
|
||||
Hash []byte // Hash of the file data
|
||||
ID uuid.UUID // ID
|
||||
Type uint8 // File Type
|
||||
Format uint16 // File Format
|
||||
Size uint64 // Size of the file data
|
||||
Tags []BlockRecordFileTag // Tags provide additional metadata
|
||||
}
|
||||
|
||||
// ---- Tag structures ----
|
||||
|
||||
// BlockRecordFileTag provides additional metadata about the file.
|
||||
// New tags can be defines in the future without breaking support.
|
||||
// BlockRecordFileTag provides metadata about the file.
|
||||
type BlockRecordFileTag struct {
|
||||
Type uint16 // See TagTypeX constants.
|
||||
Data []byte // Actual data of the tag, decoded into FileTagX structures.
|
||||
Type uint16 // See TagX constants.
|
||||
Data []byte // Data
|
||||
|
||||
// If top bit of Type is set, then Data must be 2, 4, or 8 bytes representing the distance number (positive or negative) of raw record in the block that will be used as data.
|
||||
// This is an embedded basic compression algorithm for repetitive tag. For example directory tags or album tags might be heavily repetitive among files.
|
||||
}
|
||||
|
||||
// TagTypeName defines the type of the tag
|
||||
const (
|
||||
TagTypeName = 0 // Name of file
|
||||
TagTypeFolder = 1 // Folder
|
||||
TagTypeDateCreated = 2 // Date when the file was originally created. This may differ from the date in the block record, which indicates when the file was shared.
|
||||
TagTypeDescription = 3 // Arbitrary description of the file. May contain hashtags.
|
||||
TagTypeDateShared = 4 // When the file was published on the blockchain. Cannot be set manually (virtual read-only tag).
|
||||
)
|
||||
|
||||
// FileTagFolder specifies in which folder the file is stored.
|
||||
// A corresponding TypeFolder file record matching the name may exist to provide additional details about the folder.
|
||||
type FileTagFolder struct {
|
||||
Name string // Name of the folder
|
||||
}
|
||||
|
||||
// FileTagName specifies the file name. Empty names are allowed, but not recommended!
|
||||
type FileTagName struct {
|
||||
Name string // Name of the file
|
||||
}
|
||||
|
||||
// FileTagDescription is an arbitrary of the description of the file. It may contain hashtags to help tagging and searching.
|
||||
type FileTagDescription struct {
|
||||
Description string
|
||||
}
|
||||
|
||||
// FileTagDateCreated is the date when the file was originally created
|
||||
type FileTagDateCreated struct {
|
||||
Date time.Time // Created time
|
||||
}
|
||||
|
||||
// FileTagDateShared is the date when the file was shared on the blockchain
|
||||
type FileTagDateShared struct {
|
||||
Date time.Time // Shared time
|
||||
}
|
||||
|
||||
// ---- low-level encoding ----
|
||||
|
||||
// decodeBlockRecordFiles decodes only file records. Other records are ignored.
|
||||
@@ -169,18 +129,12 @@ func decodeBlockRecordFiles(recordsRaw []BlockRecordRaw) (files []BlockRecordFil
|
||||
tag.Data = record.Data[index+6 : index+6+int(tagSize)]
|
||||
}
|
||||
|
||||
file.TagsRaw = append(file.TagsRaw, tag)
|
||||
|
||||
if decoded, err := decodeFileTag(tag); err != nil {
|
||||
return nil, err
|
||||
} else if decoded != nil {
|
||||
file.TagsDecoded = append(file.TagsDecoded, decoded)
|
||||
}
|
||||
file.Tags = append(file.Tags, tag)
|
||||
|
||||
index += 6 + int(tagSize)
|
||||
}
|
||||
|
||||
file.TagsDecoded = append(file.TagsDecoded, FileTagDateShared{Date: record.Date})
|
||||
file.Tags = append(file.Tags, TagFromDate(TagDateShared, record.Date))
|
||||
|
||||
files = append(files, file)
|
||||
}
|
||||
@@ -189,53 +143,6 @@ func decodeBlockRecordFiles(recordsRaw []BlockRecordRaw) (files []BlockRecordFil
|
||||
return files, err
|
||||
}
|
||||
|
||||
// decodeFileTag decodes a file tag. If the tag type is not known, it returns nil.
|
||||
func decodeFileTag(tag BlockRecordFileTag) (decoded interface{}, err error) {
|
||||
switch tag.Type {
|
||||
case TagTypeFolder:
|
||||
return FileTagFolder{Name: string(tag.Data)}, nil
|
||||
|
||||
case TagTypeName:
|
||||
return FileTagName{Name: string(tag.Data)}, nil
|
||||
|
||||
case TagTypeDescription:
|
||||
return FileTagDescription{Description: string(tag.Data)}, nil
|
||||
|
||||
case TagTypeDateCreated:
|
||||
if len(tag.Data) != 8 {
|
||||
return nil, errors.New("file tag date invalid size")
|
||||
}
|
||||
|
||||
timeB := int64(binary.LittleEndian.Uint64(tag.Data[0:8]))
|
||||
return FileTagDateCreated{Date: time.Unix(timeB, 0)}, nil
|
||||
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// encodeFileTag encodes a file tag. If the tag type is not known, it returns nil.
|
||||
func encodeFileTag(decoded interface{}) (tag BlockRecordFileTag, err error) {
|
||||
switch v := decoded.(type) {
|
||||
case FileTagFolder:
|
||||
return BlockRecordFileTag{Type: TagTypeFolder, Data: []byte(v.Name)}, nil
|
||||
|
||||
case FileTagName:
|
||||
return BlockRecordFileTag{Type: TagTypeName, Data: []byte(v.Name)}, nil
|
||||
|
||||
case FileTagDescription:
|
||||
return BlockRecordFileTag{Type: TagTypeDescription, Data: []byte(v.Description)}, nil
|
||||
|
||||
case FileTagDateCreated:
|
||||
var tempDate [8]byte
|
||||
binary.LittleEndian.PutUint64(tempDate[0:8], uint64(v.Date.UTC().Unix()))
|
||||
return BlockRecordFileTag{Type: TagTypeDateCreated, Data: tempDate[:]}, nil
|
||||
|
||||
}
|
||||
|
||||
return tag, errors.New("encodeFileTag unknown tag type")
|
||||
}
|
||||
|
||||
// encodeBlockRecordFiles encodes files into the block record data
|
||||
// This function should be called grouped with all files in the same folder. The folder name is deduplicated; only unique folder records will be returned.
|
||||
// Note that this function only stores the folder names as tags; it does not create separate TypeFolder file records.
|
||||
@@ -245,13 +152,7 @@ func encodeBlockRecordFiles(files []BlockRecordFile) (recordsRaw []BlockRecordRa
|
||||
|
||||
// loop through all tags to encode them and create list of duplicates that will be replaced by references
|
||||
for n := range files {
|
||||
for m := range files[n].TagsDecoded {
|
||||
tag, err := encodeFileTag(files[n].TagsDecoded[m])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files[n].TagsRaw = append(files[n].TagsRaw, tag)
|
||||
|
||||
for _, tag := range files[n].Tags {
|
||||
if len(tag.Data) > 4 {
|
||||
if _, ok := uniqueTagDataMap[string(tag.Data)]; !ok {
|
||||
uniqueTagDataMap[string(tag.Data)] = struct{}{}
|
||||
@@ -277,29 +178,29 @@ func encodeBlockRecordFiles(files []BlockRecordFile) (recordsRaw []BlockRecordRa
|
||||
data[48] = files[n].Type
|
||||
binary.LittleEndian.PutUint16(data[49:49+2], files[n].Format)
|
||||
binary.LittleEndian.PutUint64(data[51:51+8], files[n].Size)
|
||||
binary.LittleEndian.PutUint16(data[59:59+2], uint16(len(files[n].TagsRaw)))
|
||||
binary.LittleEndian.PutUint16(data[59:59+2], uint16(len(files[n].Tags)))
|
||||
|
||||
for _, tagRaw := range files[n].TagsRaw {
|
||||
for _, tag := range files[n].Tags {
|
||||
// Some tags are virtual and never stored on the blockchain. If attempted to write, ignore.
|
||||
if tagRaw.Type == TagTypeDateShared {
|
||||
if tag.IsVirtual() {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(tagRaw.Data) > 4 {
|
||||
if refNumber, ok := duplicateTagDataMap[string(tagRaw.Data)]; ok {
|
||||
if len(tag.Data) > 4 {
|
||||
if refNumber, ok := duplicateTagDataMap[string(tag.Data)]; ok {
|
||||
// In case the data is duplicated, use reference to the RecordTypeTagData instead
|
||||
tagRaw.Type |= 0x8000
|
||||
tagRaw.Data = intToBytes(-(len(recordsRaw) - refNumber))
|
||||
tag.Type |= 0x8000
|
||||
tag.Data = intToBytes(-(len(recordsRaw) - refNumber))
|
||||
}
|
||||
}
|
||||
|
||||
var tempTag [6]byte
|
||||
|
||||
binary.LittleEndian.PutUint16(tempTag[0:2], tagRaw.Type)
|
||||
binary.LittleEndian.PutUint32(tempTag[2:2+4], uint32(len(tagRaw.Data)))
|
||||
binary.LittleEndian.PutUint16(tempTag[0:2], tag.Type)
|
||||
binary.LittleEndian.PutUint32(tempTag[2:2+4], uint32(len(tag.Data)))
|
||||
|
||||
data = append(data, tempTag[:]...)
|
||||
data = append(data, tagRaw.Data...)
|
||||
data = append(data, tag.Data...)
|
||||
}
|
||||
|
||||
recordsRaw = append(recordsRaw, BlockRecordRaw{Type: RecordTypeFile, Data: data})
|
||||
@@ -324,17 +225,6 @@ func intToBytes(number int) (buffer []byte) {
|
||||
return buffer[0:8]
|
||||
}
|
||||
|
||||
// TagRawToText returns the tag text of the given tag, if available. Empty if not.
|
||||
func (file *BlockRecordFile) TagRawToText(tagType uint16) string {
|
||||
for m := range file.TagsRaw {
|
||||
if file.TagsRaw[m].Type == tagType {
|
||||
return string(file.TagsRaw[m].Data)
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// ---- high-level decoding ----
|
||||
|
||||
// BlockDecoded contains the decoded records from a block
|
||||
|
||||
@@ -47,5 +47,3 @@ const (
|
||||
FormatAPK // APK
|
||||
FormatISO // ISO
|
||||
)
|
||||
|
||||
// Future tags to be defined for audio/video: Artist, Album, Title, Length, Bitrate, Codec
|
||||
|
||||
62
File Metadata.go
Normal file
62
File Metadata.go
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
File Name: File Metadata.go
|
||||
Copyright: 2021 Peernet s.r.o.
|
||||
Author: Peter Kleissner
|
||||
|
||||
Metadata tags provide meta information about files.
|
||||
*/
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// List of defined file tags.
|
||||
const (
|
||||
TagName = 0 // Name of file.
|
||||
TagFolder = 1 // Folder name.
|
||||
TagDescription = 2 // Arbitrary description of the file. May contain hashtags.
|
||||
TagDateShared = 3 // When the file was published on the blockchain. Cannot be set manually (virtual read-only tag).
|
||||
TagDateCreated = 4 // Date when the file was originally created. This may differ from the date in the block record, which indicates when the file was shared.
|
||||
)
|
||||
|
||||
// Future tags to be defined for audio/video: Artist, Album, Title, Length, Bitrate, Codec
|
||||
// Windows list: https://docs.microsoft.com/en-us/windows/win32/wmdm/metadata-constants
|
||||
|
||||
// ---- encoding ----
|
||||
|
||||
// Date returns the tags data as date encoded
|
||||
func (tag *BlockRecordFileTag) Date() (time.Time, error) {
|
||||
if len(tag.Data) != 8 {
|
||||
return time.Time{}, errors.New("file tag date invalid size")
|
||||
}
|
||||
|
||||
timeB := int64(binary.LittleEndian.Uint64(tag.Data[0:8]))
|
||||
return time.Unix(timeB, 0).UTC(), nil
|
||||
}
|
||||
|
||||
// Text returns the tags data as text encoded
|
||||
func (tag *BlockRecordFileTag) Text() string {
|
||||
return string(tag.Data)
|
||||
}
|
||||
|
||||
// IsVirtual checks if the tag is virtual.
|
||||
func (tag *BlockRecordFileTag) IsVirtual() bool {
|
||||
return tag.Type == TagDateShared
|
||||
}
|
||||
|
||||
// TagFromDate returns a tag from date
|
||||
func TagFromDate(Type uint16, Date time.Time) BlockRecordFileTag {
|
||||
var tempDate [8]byte
|
||||
binary.LittleEndian.PutUint64(tempDate[0:8], uint64(Date.UTC().Unix()))
|
||||
|
||||
return BlockRecordFileTag{Type: Type, Data: tempDate[:]}
|
||||
}
|
||||
|
||||
// TagFromText returns a tag from text
|
||||
func TagFromText(Type uint16, Text string) BlockRecordFileTag {
|
||||
return BlockRecordFileTag{Type: Type, Data: []byte(Text)}
|
||||
}
|
||||
28
Test_test.go
28
Test_test.go
@@ -94,12 +94,12 @@ func TestBlockEncoding(t *testing.T) {
|
||||
encoded1, _ := encodeBlockRecordProfile(BlockRecordProfile{Fields: []BlockRecordProfileField{{Type: ProfileFieldName, Text: "Test User 1"}}})
|
||||
|
||||
file1 := BlockRecordFile{Hash: hashData([]byte("Test data")), Type: TypeText, Format: FormatText, Size: 9, ID: uuid.New()}
|
||||
file1.TagsDecoded = append(file1.TagsDecoded, FileTagName{Name: "Filename 1.txt"})
|
||||
file1.TagsDecoded = append(file1.TagsDecoded, FileTagFolder{Name: "documents\\sub folder"})
|
||||
file1.Tags = append(file1.Tags, TagFromText(TagName, "Filename 1.txt"))
|
||||
file1.Tags = append(file1.Tags, TagFromText(TagFolder, "documents\\sub folder"))
|
||||
|
||||
file2 := BlockRecordFile{Hash: hashData([]byte("Test data 2")), Type: TypeText, Format: FormatText, Size: 9, ID: uuid.New()}
|
||||
file2.TagsDecoded = append(file2.TagsDecoded, FileTagName{Name: "Filename 2.txt"})
|
||||
file2.TagsDecoded = append(file2.TagsDecoded, FileTagFolder{Name: "documents\\sub folder"})
|
||||
file2.Tags = append(file2.Tags, TagFromText(TagName, "Filename 2.txt"))
|
||||
file2.Tags = append(file2.Tags, TagFromText(TagFolder, "documents\\sub folder"))
|
||||
|
||||
encodedFiles, _ := encodeBlockRecordFiles([]BlockRecordFile{file1, file2})
|
||||
|
||||
@@ -160,8 +160,8 @@ func TestBlockchainAdd(t *testing.T) {
|
||||
initUserBlockchain()
|
||||
|
||||
file1 := BlockRecordFile{Hash: hashData([]byte("Test data")), Type: TypeText, Format: FormatText, Size: 9, ID: uuid.New()}
|
||||
file1.TagsDecoded = append(file1.TagsDecoded, FileTagName{Name: "Filename 1.txt"})
|
||||
file1.TagsDecoded = append(file1.TagsDecoded, FileTagFolder{Name: "documents\\sub folder"})
|
||||
file1.Tags = append(file1.Tags, TagFromText(TagName, "Filename 1.txt"))
|
||||
file1.Tags = append(file1.Tags, TagFromText(TagFolder, "documents\\sub folder"))
|
||||
|
||||
newHeight, newVersion, status := UserBlockchainAddFiles([]BlockRecordFile{file1})
|
||||
|
||||
@@ -222,12 +222,12 @@ func printFile(file BlockRecordFile) {
|
||||
fmt.Printf(" Format %d\n", file.Format)
|
||||
fmt.Printf(" Hash %s\n", hex.EncodeToString(file.Hash))
|
||||
|
||||
for _, decodedT := range file.TagsDecoded {
|
||||
switch v := decodedT.(type) {
|
||||
case FileTagName:
|
||||
fmt.Printf(" Name %s\n", v.Name)
|
||||
case FileTagFolder:
|
||||
fmt.Printf(" Folder %s\n", v.Name)
|
||||
for _, tag := range file.Tags {
|
||||
switch tag.Type {
|
||||
case TagName:
|
||||
fmt.Printf(" Name %s\n", tag.Text())
|
||||
case TagFolder:
|
||||
fmt.Printf(" Folder %s\n", tag.Text())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -239,8 +239,8 @@ func TestBlockchainDelete(t *testing.T) {
|
||||
|
||||
// test add file
|
||||
file1 := BlockRecordFile{Hash: hashData([]byte("Test data")), Type: TypeText, Format: FormatText, Size: 9, ID: uuid.New()}
|
||||
file1.TagsDecoded = append(file1.TagsDecoded, FileTagName{Name: "Test file to be deleted.txt"})
|
||||
file1.TagsDecoded = append(file1.TagsDecoded, FileTagFolder{Name: "documents\\sub folder"})
|
||||
file1.Tags = append(file1.Tags, TagFromText(TagName, "Test file to be deleted.txt"))
|
||||
file1.Tags = append(file1.Tags, TagFromText(TagFolder, "documents\\sub folder"))
|
||||
|
||||
newHeight, newVersion, status := UserBlockchainAddFiles([]BlockRecordFile{file1})
|
||||
fmt.Printf("Added file: Status %d height %d version %d\n", status, newHeight, newVersion)
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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": []
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user