b6306a6e61
This straigthens some mistakes with the outputs of the /add endpoints. Currently, we had exactly the same output format which: * was not exactly the ipfs API output format but was sort of similar * made some weird concessions to be compatible (like having a string-type "size") * was not aligned with Cluster API conventions (lowercase keys) This corrects all this: * The Cluster API /add output format now uses the right types and lowercase keys. * `Hash` is now `Cid`, because the field carries a Cid. * We copy error handling with request trailers from IPFS, and avoid carrying the errors in the output objects. * The proxy now returns exactly the types as ipfs would * We add the X-Chunked-Output: 1 header, which is custom and redundant, but otherwise breaks js-ipfs-api integrations with the /add endpoint. License: MIT Signed-off-by: Hector Sanjuan <code@hector.link>
202 lines
4.6 KiB
Go
202 lines
4.6 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
// DefaultShardSize is the shard size for params objects created with DefaultParams().
|
|
var DefaultShardSize = uint64(100 * 1024 * 1024) // 100 MB
|
|
|
|
// AddedOutput carries information for displaying the standard ipfs output
|
|
// indicating a node of a file has been added.
|
|
type AddedOutput struct {
|
|
Name string `json:"name"`
|
|
Cid string `json:"cid,omitempty"`
|
|
Bytes uint64 `json:"bytes,omitempty"`
|
|
Size uint64 `json:"size,omitempty"`
|
|
}
|
|
|
|
// AddParams contains all of the configurable parameters needed to specify the
|
|
// importing process of a file being added to an ipfs-cluster
|
|
type AddParams struct {
|
|
PinOptions
|
|
|
|
Recursive bool
|
|
Layout string
|
|
Chunker string
|
|
RawLeaves bool
|
|
Hidden bool
|
|
Wrap bool
|
|
Shard bool
|
|
Progress bool
|
|
CidVersion int
|
|
HashFun string
|
|
}
|
|
|
|
// DefaultAddParams returns a AddParams object with standard defaults
|
|
func DefaultAddParams() *AddParams {
|
|
return &AddParams{
|
|
Recursive: false,
|
|
Layout: "", // corresponds to balanced layout
|
|
Chunker: "size-262144",
|
|
RawLeaves: false,
|
|
Hidden: false,
|
|
Wrap: false,
|
|
Shard: false,
|
|
Progress: false,
|
|
CidVersion: 0,
|
|
HashFun: "sha2-256",
|
|
PinOptions: PinOptions{
|
|
ReplicationFactorMin: 0,
|
|
ReplicationFactorMax: 0,
|
|
Name: "",
|
|
ShardSize: DefaultShardSize,
|
|
},
|
|
}
|
|
}
|
|
|
|
func parseBoolParam(q url.Values, name string, dest *bool) error {
|
|
if v := q.Get(name); v != "" {
|
|
b, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
return fmt.Errorf("parameter %s invalid", name)
|
|
}
|
|
*dest = b
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func parseIntParam(q url.Values, name string, dest *int) error {
|
|
if v := q.Get(name); v != "" {
|
|
i, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
return fmt.Errorf("parameter %s invalid", name)
|
|
}
|
|
*dest = i
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// AddParamsFromQuery parses the AddParams object from
|
|
// a URL.Query().
|
|
func AddParamsFromQuery(query url.Values) (*AddParams, error) {
|
|
params := DefaultAddParams()
|
|
|
|
layout := query.Get("layout")
|
|
switch layout {
|
|
case "trickle", "balanced", "":
|
|
// nothing
|
|
default:
|
|
return nil, errors.New("parameter trickle invalid")
|
|
}
|
|
params.Layout = layout
|
|
|
|
chunker := query.Get("chunker")
|
|
params.Chunker = chunker
|
|
name := query.Get("name")
|
|
params.Name = name
|
|
|
|
hashF := query.Get("hash")
|
|
if hashF != "" {
|
|
params.HashFun = hashF
|
|
}
|
|
|
|
err := parseBoolParam(query, "recursive", ¶ms.Recursive)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = parseBoolParam(query, "raw-leaves", ¶ms.RawLeaves)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = parseBoolParam(query, "hidden", ¶ms.Hidden)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = parseBoolParam(query, "wrap-with-directory", ¶ms.Wrap)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = parseBoolParam(query, "shard", ¶ms.Shard)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = parseBoolParam(query, "progress", ¶ms.Progress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = parseIntParam(query, "replication-min", ¶ms.ReplicationFactorMin)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = parseIntParam(query, "replication-max", ¶ms.ReplicationFactorMax)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = parseIntParam(query, "cid-version", ¶ms.CidVersion)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if v := query.Get("shard-size"); v != "" {
|
|
shardSize, err := strconv.ParseUint(v, 10, 64)
|
|
if err != nil {
|
|
return nil, errors.New("parameter shard_size is invalid")
|
|
}
|
|
params.ShardSize = shardSize
|
|
}
|
|
|
|
return params, nil
|
|
}
|
|
|
|
// ToQueryString returns a url query string (key=value&key2=value2&...)
|
|
func (p *AddParams) ToQueryString() string {
|
|
fmtStr := "replication-min=%d&replication-max=%d&name=%s&"
|
|
fmtStr += "shard=%t&shard-size=%d&recursive=%t&"
|
|
fmtStr += "layout=%s&chunker=%s&raw-leaves=%t&hidden=%t&"
|
|
fmtStr += "wrap-with-directory=%t&progress=%t&"
|
|
fmtStr += "cid-version=%d&hash=%s"
|
|
query := fmt.Sprintf(
|
|
fmtStr,
|
|
p.ReplicationFactorMin,
|
|
p.ReplicationFactorMax,
|
|
p.Name,
|
|
p.Shard,
|
|
p.ShardSize,
|
|
p.Recursive,
|
|
p.Layout,
|
|
p.Chunker,
|
|
p.RawLeaves,
|
|
p.Hidden,
|
|
p.Wrap,
|
|
p.Progress,
|
|
p.CidVersion,
|
|
p.HashFun,
|
|
)
|
|
return query
|
|
}
|
|
|
|
// Equals checks if p equals p2.
|
|
func (p *AddParams) Equals(p2 *AddParams) bool {
|
|
return p.ReplicationFactorMin == p2.ReplicationFactorMin &&
|
|
p.ReplicationFactorMax == p2.ReplicationFactorMax &&
|
|
p.Name == p2.Name &&
|
|
p.Recursive == p2.Recursive &&
|
|
p.Shard == p2.Shard &&
|
|
p.ShardSize == p2.ShardSize &&
|
|
p.Layout == p2.Layout &&
|
|
p.Chunker == p2.Chunker &&
|
|
p.RawLeaves == p2.RawLeaves &&
|
|
p.Hidden == p2.Hidden &&
|
|
p.Wrap == p2.Wrap &&
|
|
p.CidVersion == p2.CidVersion &&
|
|
p.HashFun == p2.HashFun
|
|
}
|