Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: CDN emulation #10

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 99 additions & 3 deletions api/file_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

Expand Down Expand Up @@ -124,7 +125,71 @@ func PostFileHandler(fio *file_system.FileSystem, prover *proofs.Prover, wl *wal
}
}

func DownloadFileHandler(f *file_system.FileSystem) func(http.ResponseWriter, *http.Request) {
func ForwardDownload(merkle []byte, wallet *wallet.Wallet, w http.ResponseWriter) {
cl := storageTypes.NewQueryClient(wallet.Client.GRPCConn)
fReq := storageTypes.QueryFindFile{Merkle: merkle}

fileRes, err := cl.FindFile(context.Background(), &fReq)
if err != nil {
v := types.ErrorResponse{
Error: err.Error(),
}
w.WriteHeader(http.StatusInternalServerError)
_ = json.NewEncoder(w).Encode(v)
return
}

ips := fileRes.ProviderIps
var ipStrings []string
err = json.Unmarshal([]byte(ips), ipStrings)
if err != nil {
v := types.ErrorResponse{
Error: err.Error(),
}
w.WriteHeader(http.StatusInternalServerError)
_ = json.NewEncoder(w).Encode(v)
return
}

for _, ipString := range ipStrings {
u, err := url.Parse(ipString)
if err != nil {
continue
}

merkleString := hex.EncodeToString(merkle)

hasU := u.JoinPath("has", merkleString)

hcl := http.DefaultClient

r, err := http.NewRequest("GET", hasU.String(), nil)
if err != nil {
continue
}

hRes, err := hcl.Do(r)
if err != nil {
continue
}

if hRes.StatusCode != 200 {
continue
}

mU := u.JoinPath("download", merkleString)
http.Redirect(w, r, mU.String(), http.StatusFound)
return
}

v := types.ErrorResponse{
Error: "cannot find file on network",
}
w.WriteHeader(http.StatusInternalServerError)
_ = json.NewEncoder(w).Encode(v)
}

func DownloadFileHandler(f *file_system.FileSystem, wallet *wallet.Wallet) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)

Expand All @@ -136,19 +201,50 @@ func DownloadFileHandler(f *file_system.FileSystem) func(http.ResponseWriter, *h
}
w.WriteHeader(http.StatusInternalServerError)
_ = json.NewEncoder(w).Encode(v)

return
}

file, err := f.GetFileDataByMerkle(merkle)
if err != nil {
ForwardDownload(merkle, wallet, w)
return
}

_, _ = w.Write(file)
}
}

func HasFileHandler(f *file_system.FileSystem) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)

merkleString := vars["merkle"]
merkle, err := hex.DecodeString(merkleString)
if err != nil {
v := types.ErrorResponse{
Error: err.Error(),
}
w.WriteHeader(http.StatusInternalServerError)
_ = json.NewEncoder(w).Encode(v)
return

}

_, _ = w.Write(file)
found, err := f.HasFile(merkle)
if err != nil {
v := types.ErrorResponse{
Error: err.Error(),
}
w.WriteHeader(http.StatusInternalServerError)
_ = json.NewEncoder(w).Encode(v)
return
}

if found {
w.WriteHeader(200)
return
}

w.WriteHeader(http.StatusNotFound)
}
}
3 changes: 2 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func (a *API) Serve(f *file_system.FileSystem, p *proofs.Prover, wallet *wallet.
r := mux.NewRouter()
r.HandleFunc("/", IndexHandler(wallet.AccAddress()))
r.HandleFunc("/upload", PostFileHandler(f, p, wallet, chunkSize))
r.HandleFunc("/download/{fid}", DownloadFileHandler(f))
r.HandleFunc("/download/{fid}", DownloadFileHandler(f, wallet))
r.HandleFunc("/has/{fid}", HasFileHandler(f))

r.HandleFunc("/list", ListFilesHandler(f))
r.HandleFunc("/api/data/fids", LegacyListFilesHandler(f))
Expand Down
16 changes: 16 additions & 0 deletions file_system/file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,19 @@ func (f *FileSystem) GetFileDataByMerkle(merkle []byte) ([]byte, error) {

return fileData, err
}

func (f *FileSystem) HasFile(merkle []byte) (found bool, err error) {
found = false
err = f.db.View(func(txn *badger.Txn) error {
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
prefix := majorChunkMerkleKey(merkle)
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
found = true
return nil
}

return nil
})
return found, err
}
1 change: 0 additions & 1 deletion file_system/file_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,4 @@ func TestWriteAndProveFiles(t *testing.T) {
require.Equal(t, true, verified)

}

}
1 change: 1 addition & 0 deletions file_system/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func majorChunkKey(merkle []byte, owner string, start int64) []byte {
return []byte(fmt.Sprintf("chunks/%x/%s/%d/", merkle, owner, start))
}

// majorChunkMerkleKey returns a byte array of `chunks/{merkle}`
func majorChunkMerkleKey(merkle []byte) []byte {
return []byte(fmt.Sprintf("chunks/%x", merkle))
}
Expand Down
Loading