2021-10-30 12:01:05 +00:00
|
|
|
package martilq
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"os"
|
|
|
|
|
"time"
|
|
|
|
|
"log"
|
|
|
|
|
"errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Resource struct {
|
|
|
|
|
Title string `json:"title"`
|
2021-10-31 11:53:58 +00:00
|
|
|
Uid string `json:"uid"`
|
|
|
|
|
DocumentName string `json:"documentName"`
|
2021-10-30 12:01:05 +00:00
|
|
|
IssueDate time.Time `json:"issueDate"`
|
|
|
|
|
Modified time.Time `json:"modified"`
|
|
|
|
|
Expires time.Time `json:"expires"`
|
|
|
|
|
State string `json:"state"`
|
|
|
|
|
Author string `json:"author"`
|
|
|
|
|
Length int64 `json:"length"`
|
2021-10-31 11:53:58 +00:00
|
|
|
Hash hash `json:"hash"`
|
2021-10-30 12:01:05 +00:00
|
|
|
|
|
|
|
|
Description string `json:"description"`
|
|
|
|
|
Url string `json:"url"`
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
Content_Type string `json:"content-type"`
|
|
|
|
|
Encoding string `json:"encoding"`
|
|
|
|
|
Compression string `json:"compression"`
|
|
|
|
|
Encryption string `json:"encryption"`
|
2021-10-31 11:53:58 +00:00
|
|
|
DescribedBy string `json:"describedBy"`
|
2021-10-30 12:01:05 +00:00
|
|
|
|
|
|
|
|
Attributes []Attribute `json:"attributes"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewResource(config configuration) Resource {
|
|
|
|
|
|
|
|
|
|
r := Resource {}
|
|
|
|
|
u := uuid.New()
|
|
|
|
|
r.Uid = u.String()
|
|
|
|
|
|
|
|
|
|
r.IssueDate = time.Now()
|
|
|
|
|
r.State = config.state
|
|
|
|
|
r.Author = config.author
|
|
|
|
|
r.Expires = config.ExpireDate()
|
|
|
|
|
r.Encoding = config.encoding
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewMartiLQResource(config configuration, sourcePath string, urlPath string, excludeHash bool, extendAttributes bool) (Resource, error) {
|
|
|
|
|
|
|
|
|
|
r := Resource {}
|
|
|
|
|
|
|
|
|
|
stats, err := os.Stat(sourcePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
log.Printf("'" + sourcePath + "' file does not exist.")
|
|
|
|
|
return r, errors.New("'" + sourcePath + "' file does not exist.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 11:53:58 +00:00
|
|
|
|
|
|
|
|
if config.dataPath != "" {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-30 12:01:05 +00:00
|
|
|
u := uuid.New()
|
|
|
|
|
r.Uid = u.String()
|
|
|
|
|
|
|
|
|
|
r.State = config.state
|
|
|
|
|
r.Author = config.author
|
|
|
|
|
r.Expires = config.ExpireDate()
|
|
|
|
|
r.Encoding = config.encoding
|
|
|
|
|
|
|
|
|
|
r.DocumentName = stats.Name()
|
2021-10-31 11:53:58 +00:00
|
|
|
if config.title == "{{documentName}}" {
|
2021-10-30 12:01:05 +00:00
|
|
|
r.Title = r.DocumentName
|
|
|
|
|
}
|
|
|
|
|
r.IssueDate = time.Now()
|
|
|
|
|
r.Modified = stats.ModTime()
|
|
|
|
|
r.Url = urlPath
|
|
|
|
|
r.Length = stats.Size()
|
|
|
|
|
if !excludeHash {
|
|
|
|
|
h := NewMartiLQHash(config.hashAlgorithm, sourcePath, "", config.signKey_File)
|
|
|
|
|
r.Hash = h
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.Attributes = NewDefaultExtensionAttributes(sourcePath, extendAttributes)
|
|
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
|
}
|