Compare commits

...

5 Commits

5 changed files with 134 additions and 30 deletions

View File

@ -1,16 +1,68 @@
package cid package cid
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"errors"
"github.com/multiformats/go-multibase" "github.com/multiformats/go-multibase"
) )
func EncodeHashSimple(hash [32]byte, size uint64) (string, error) { var MAGIC_BYTES = []byte{0x26, 0x1f}
type CID struct {
Hash [32]byte
Size uint64
}
func Encode(hash [32]byte, size uint64) (string, error) {
sizeBytes := make([]byte, 8) sizeBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(sizeBytes, size) binary.LittleEndian.PutUint64(sizeBytes, size)
prefixedHash := append([]byte{0x26, 0x1f}, hash[:]...) prefixedHash := append(MAGIC_BYTES, hash[:]...)
prefixedHash = append(prefixedHash, sizeBytes...) prefixedHash = append(prefixedHash, sizeBytes...)
return multibase.Encode(multibase.Base58BTC, prefixedHash) return multibase.Encode(multibase.Base58BTC, prefixedHash)
} }
func Valid(cid string) (bool, error) {
_, err := maybeDecode(cid)
if err != nil {
return false, err
}
return true, nil
}
func Decode(cid string) (*CID, error) {
data, err := maybeDecode(cid)
if err != nil {
return &CID{}, err
}
data = data[len(MAGIC_BYTES):]
var hash [32]byte
copy(hash[:], data[:])
size := binary.LittleEndian.Uint64(data[32:])
return &CID{Hash: hash, Size: size}, nil
}
func maybeDecode(cid string) ([]byte, error) {
_, data, err := multibase.Decode(cid)
if err != nil {
return nil, err
}
if bytes.Compare(data[0:len(MAGIC_BYTES)], MAGIC_BYTES) != 0 {
return nil, errors.New("CID magic bytes missing or invalid")
}
size := binary.LittleEndian.Uint64(data[len(MAGIC_BYTES)+32:])
if size == 0 {
return nil, errors.New("missing or empty size")
}
return data, nil
}

17
go.sum
View File

@ -53,12 +53,6 @@ github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6Xge
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 h1:KkH3I3sJuOLP3TjA/dfr4NAY8bghDwnXiU7cTKxQqo0= github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 h1:KkH3I3sJuOLP3TjA/dfr4NAY8bghDwnXiU7cTKxQqo0=
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06/go.mod h1:7erjKLwalezA0k99cWs5L11HWOAPNjdUZ6RxH1BXbbM= github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06/go.mod h1:7erjKLwalezA0k99cWs5L11HWOAPNjdUZ6RxH1BXbbM=
github.com/SiaFoundation/renterd v0.3.0-beta.0.20230503134537-9a6bd1c83cef h1:qdI3zlSddo7emGvd7xqAVY5hFK4C1PSn9D4nhqzOmkM=
github.com/SiaFoundation/renterd v0.3.0-beta.0.20230503134537-9a6bd1c83cef/go.mod h1:xHn0AS2pidylCob13qoiKHIz7UlGITujRk4TbP3XU1U=
github.com/SiaFoundation/renterd v0.3.0-beta.0.20230504125557-1900afd53571 h1:GeUPWCYIh2GH0KGnOM4EXAOax0xvKbJI77zigXsIBW0=
github.com/SiaFoundation/renterd v0.3.0-beta.0.20230504125557-1900afd53571/go.mod h1:xHn0AS2pidylCob13qoiKHIz7UlGITujRk4TbP3XU1U=
github.com/SiaFoundation/renterd v0.3.0-beta.0.20230505083543-7925301194b0 h1:k8tZqSzowXL/YC/smxutG67hw5xqf/pSYUpC4q1GKII=
github.com/SiaFoundation/renterd v0.3.0-beta.0.20230505083543-7925301194b0/go.mod h1:xHn0AS2pidylCob13qoiKHIz7UlGITujRk4TbP3XU1U=
github.com/SiaFoundation/renterd v0.3.0-beta.0.20230505211942-7b55caed4740 h1:isbxOoZXejYswvQRA1mfXmwponqawHJwhoVkAOX0cFo= github.com/SiaFoundation/renterd v0.3.0-beta.0.20230505211942-7b55caed4740 h1:isbxOoZXejYswvQRA1mfXmwponqawHJwhoVkAOX0cFo=
github.com/SiaFoundation/renterd v0.3.0-beta.0.20230505211942-7b55caed4740/go.mod h1:jIZ+xQnbfWhbDtpkgZSccnNxb0Ks3zcnnqPzkB6y1YM= github.com/SiaFoundation/renterd v0.3.0-beta.0.20230505211942-7b55caed4740/go.mod h1:jIZ+xQnbfWhbDtpkgZSccnNxb0Ks3zcnnqPzkB6y1YM=
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
@ -180,7 +174,6 @@ github.com/gobwas/ws v1.2.0/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/K
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4= github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE= github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@ -547,8 +540,6 @@ go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyK
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8= go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
go.opentelemetry.io/proto/otlp v0.19.0 h1:IVN6GR+mhC4s5yfcTbmzHYODqvWAp3ZedA2SJPI1Nnw= go.opentelemetry.io/proto/otlp v0.19.0 h1:IVN6GR+mhC4s5yfcTbmzHYODqvWAp3ZedA2SJPI1Nnw=
go.opentelemetry.io/proto/otlp v0.19.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U= go.opentelemetry.io/proto/otlp v0.19.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U=
go.sia.tech/core v0.1.12-0.20230428165632-55bb40d9006f h1:Jkn3WeNRTJ6R+SChrGvv0J+b5Kwd7Q4lIuwqJ7vkanA=
go.sia.tech/core v0.1.12-0.20230428165632-55bb40d9006f/go.mod h1:D17UWSn99SEfQnEaR9G9n6Kz9+BwqMoUgZ6Cl424LsQ=
go.sia.tech/core v0.1.12-0.20230503202148-581dd00ac1d2 h1:a1UTWnPoH6Nf8Y4MrIf74jqCeMoPVmEJPoICOHqCA4o= go.sia.tech/core v0.1.12-0.20230503202148-581dd00ac1d2 h1:a1UTWnPoH6Nf8Y4MrIf74jqCeMoPVmEJPoICOHqCA4o=
go.sia.tech/core v0.1.12-0.20230503202148-581dd00ac1d2/go.mod h1:D17UWSn99SEfQnEaR9G9n6Kz9+BwqMoUgZ6Cl424LsQ= go.sia.tech/core v0.1.12-0.20230503202148-581dd00ac1d2/go.mod h1:D17UWSn99SEfQnEaR9G9n6Kz9+BwqMoUgZ6Cl424LsQ=
go.sia.tech/jape v0.9.0 h1:kWgMFqALYhLMJYOwWBgJda5ko/fi4iZzRxHRP7pp8NY= go.sia.tech/jape v0.9.0 h1:kWgMFqALYhLMJYOwWBgJda5ko/fi4iZzRxHRP7pp8NY=
@ -557,8 +548,6 @@ go.sia.tech/mux v1.2.0 h1:ofa1Us9mdymBbGMY2XH/lSpY8itFsKIo/Aq8zwe+GHU=
go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo= go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo=
go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca h1:aZMg2AKevn7jKx+wlusWQfwSM5pNU9aGtRZme29q3O4= go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca h1:aZMg2AKevn7jKx+wlusWQfwSM5pNU9aGtRZme29q3O4=
go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca/go.mod h1:h/1afFwpxzff6/gG5i1XdAgPK7dEY6FaibhK7N5F86Y= go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca/go.mod h1:h/1afFwpxzff6/gG5i1XdAgPK7dEY6FaibhK7N5F86Y=
go.sia.tech/web/renterd v0.13.0 h1:wG02yiwrS5PPCELHlPMqZ5sMO0+11V6T73941mJrfqY=
go.sia.tech/web/renterd v0.13.0/go.mod h1:jr4PVQW1KU8JpAzmJRfFecDeJ5SPIRrKM3OKZ+FvGvE=
go.sia.tech/web/renterd v0.14.0 h1:74WDPNYXk71d8uT86rkQAa7AlDp8+VDRsQ2oyhwPplg= go.sia.tech/web/renterd v0.14.0 h1:74WDPNYXk71d8uT86rkQAa7AlDp8+VDRsQ2oyhwPplg=
go.sia.tech/web/renterd v0.14.0/go.mod h1:jr4PVQW1KU8JpAzmJRfFecDeJ5SPIRrKM3OKZ+FvGvE= go.sia.tech/web/renterd v0.14.0/go.mod h1:jr4PVQW1KU8JpAzmJRfFecDeJ5SPIRrKM3OKZ+FvGvE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
@ -735,14 +724,10 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210421210424-b80969c67360/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210421210424-b80969c67360/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols= golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@ -903,8 +888,6 @@ google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAG
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34=
google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU= google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU=
google.golang.org/grpc v1.54.0 h1:EhTqbhiYeixwWQtAEZAxmV9MGqcjEU2mFx52xCzNyag=
google.golang.org/grpc v1.54.0/go.mod h1:PUSEXI6iWghWaB6lXM4knEgpJNu2qUcKfDtNci3EC2g=
google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag= google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag=
google.golang.org/grpc v1.55.0/go.mod h1:iYEXKGkEBhg1PjZQvoYEVPTDkHo1/bjTnfwTeGONTY8= google.golang.org/grpc v1.55.0/go.mod h1:iYEXKGkEBhg1PjZQvoYEVPTDkHo1/bjTnfwTeGONTY8=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=

10
main.go
View File

@ -90,10 +90,16 @@ func main() {
app.Get("/swagger/{any:path}", swaggerUI) app.Get("/swagger/{any:path}", swaggerUI)
// Start the Iris app and listen for incoming requests on port 80 // Start the Iris app and listen for incoming requests on port 80
log.Fatal(app.Listen(":8080", func(app *iris.Application) { err := app.Listen(":8080", func(app *iris.Application) {
routes := app.GetRoutes() routes := app.GetRoutes()
for _, route := range routes { for _, route := range routes {
log.Println(route) log.Println(route)
} }
})) })
if err != nil {
log.Fatal(err)
}
renterd.ShutdownComplete()
} }

View File

@ -51,6 +51,8 @@ var (
seed *types.PrivateKey seed *types.PrivateKey
ready = make(chan bool) ready = make(chan bool)
readyFired = false readyFired = false
shutdown = make(chan bool)
shutdownFired = false
) )
func check(context string, err error) { func check(context string, err error) {
@ -367,6 +369,8 @@ func Main() {
log.Fatalf("Shutdown function %v failed: %v", i+1, err) log.Fatalf("Shutdown function %v failed: %v", i+1, err)
} }
} }
shutdown <- true
} }
func GetApiAddr() string { func GetApiAddr() string {
@ -382,3 +386,13 @@ func Ready() bool {
return true return true
} }
func ShutdownComplete() bool {
if shutdownFired {
return true
}
shutdownFired = <-shutdown
return true
}

View File

@ -52,7 +52,7 @@ func (f *FilesService) PostUpload() {
hashBytes := blake3.Sum256(buf) hashBytes := blake3.Sum256(buf)
hashHex := hex.EncodeToString(hashBytes[:]) hashHex := hex.EncodeToString(hashBytes[:])
fileCid, err := cid.EncodeHashSimple(hashBytes, uint64(meta.Size)) fileCid, err := cid.Encode(hashBytes, uint64(meta.Size))
if internalError(ctx, err) { if internalError(ctx, err) {
return return
@ -123,17 +123,66 @@ func (f *FilesService) PostUpload() {
ctx.JSON(&UploadResponse{Cid: fileCid}) ctx.JSON(&UploadResponse{Cid: fileCid})
} }
func internalErrorCustom(ctx iris.Context, err error, customError error) bool { func (f *FilesService) GetDownload() {
ctx := f.Ctx
cidString := ctx.URLParam("cid")
_, err := cid.Valid(cidString)
if sendError(ctx, err, iris.StatusBadRequest) {
return
}
cidObject, _ := cid.Decode(cidString)
hashHex := hex.EncodeToString(cidObject.Hash[:])
result := db.Get().Table("uploads").Where("hash = ?", hashHex).Row()
if result.Err() != nil {
sendError(ctx, result.Err(), iris.StatusNotFound)
return
}
fetch, err := client.R().SetDoNotParseResponse(true).Get(fmt.Sprintf("/worker/objects/%s", hashHex))
if err != nil {
if fetch.StatusCode() == 404 {
sendError(ctx, err, iris.StatusNotFound)
return
}
internalError(ctx, err)
return
}
ctx.Header("Transfer-Encoding", "chunked")
internalError(ctx, err)
err = ctx.StreamWriter(func(w io.Writer) error {
_, err = io.Copy(w, fetch.RawBody())
_ = fetch.RawBody().Close()
return err
})
internalError(ctx, err)
}
func sendErrorCustom(ctx iris.Context, err error, customError error, irisError int) bool {
if err != nil { if err != nil {
if customError != nil { if customError != nil {
err = customError err = customError
} }
ctx.StopWithError(iris.StatusInternalServerError, err) ctx.StopWithError(irisError, err)
return true return true
} }
return false return false
} }
func internalError(ctx iris.Context, err error) bool { func internalError(ctx iris.Context, err error) bool {
return internalErrorCustom(ctx, err, nil) return sendErrorCustom(ctx, err, nil, iris.StatusInternalServerError)
}
func internalErrorCustom(ctx iris.Context, err error, customError error) bool {
return sendErrorCustom(ctx, err, customError, iris.StatusInternalServerError)
}
func sendError(ctx iris.Context, err error, irisError int) bool {
return sendErrorCustom(ctx, err, nil, irisError)
} }