Skip to content
Open
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
5 changes: 0 additions & 5 deletions config.yaml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ Dirs:
# DontHash: false
## If true, will pin the root directory
# Pin: false
## If true, and EstuaryAPIKey is set, will attempt to pin the CID via Estuary as well
# Estuary: false
# - ID: Example2
# Dir: /home/user/Pictures/
# Nocopy: false
# DontHash: false
# Pin: false

# API key for Estuary (optional, find out more at https://estuary.tech)
EstuaryAPIKey:

# Relative MFS directory path (default "/ipfs-sync/")
BasePath: /ipfs-sync/

Expand Down
4 changes: 0 additions & 4 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var (
VersionFlag = flag.Bool("version", false, "display version and exit")
VerboseFlag = flag.Bool("v", false, "display verbose output")
Verbose bool
EstuaryAPIKey string // don't make this a flag
VerifyFilestoreFlag = flag.Bool("verify", false, "verify filestore on startup (not recommended unless you're having issues)")
VerifyFilestore bool

Expand Down Expand Up @@ -64,7 +63,6 @@ type DirKey struct {
Nocopy bool `yaml:"Nocopy"`
DontHash bool `yaml:"DontHash"`
Pin bool `yaml:"Pin"`
Estuary bool `yaml:"Estuary"`

// probably best to let this be managed automatically
CID string
Expand Down Expand Up @@ -117,7 +115,6 @@ type ConfigFileStruct struct {
DB string `yaml:"DB"`
IgnoreHidden bool `yaml:"IgnoreHidden"`
Timeout string `yaml:"Timeout"`
EstuaryAPIKey string `yaml:"EstuaryAPIKey"`
VerifyFilestore bool `yaml:"VerifyFilestore"`
}

Expand Down Expand Up @@ -179,7 +176,6 @@ func loadConfig(path string) {
DBPath = cfg.DB
}
IgnoreHidden = cfg.IgnoreHidden
EstuaryAPIKey = cfg.EstuaryAPIKey
VerifyFilestore = cfg.VerifyFilestore
}

Expand Down
110 changes: 2 additions & 108 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -122,7 +121,7 @@ func filePathWalkDir(root string) ([]string, error) {
}

// AddDir adds a directory, and returns CID.
func AddDir(path string, nocopy bool, pin bool, estuary bool) (string, error) {
func AddDir(path string, nocopy bool, pin bool) (string, error) {
pathSplit := strings.Split(path, string(os.PathSeparator))
dirName := pathSplit[len(pathSplit)-2]
files, err := filePathWalkDir(path)
Expand Down Expand Up @@ -158,11 +157,6 @@ func AddDir(path string, nocopy bool, pin bool, estuary bool) (string, error) {
err := Pin(cid)
log.Println("Error pinning", dirName, ":", err)
}
if estuary {
if err := PinEstuary(cid, dirName); err != nil {
log.Println("Error pinning to Estuary:", err)
}
}
return cid, err
}

Expand Down Expand Up @@ -567,11 +561,6 @@ func Publish(cid, key string) error {
return err
}

type EstuaryFile struct {
Cid string
Name string
}

type IPFSRemotePinningResponse struct {
Count int
Results []*IPFSRemotePinResult
Expand All @@ -586,98 +575,6 @@ type IPFSRemotePin struct {
Cid string
}

func doEstuaryRequest(reqType, cmd string, jsonData []byte) (string, error) {
if EstuaryAPIKey == "" {
return "", errors.New("Estuary API key is blank.")
}
var cancel context.CancelFunc
ctx := context.Background()
if TimeoutTime > 0 {
ctx, cancel = context.WithTimeout(ctx, TimeoutTime)
defer cancel()
}
c := &http.Client{}

var (
req *http.Request
err error
)
if jsonData != nil {
req, err = http.NewRequestWithContext(ctx, reqType, "https://api.estuary.tech/"+cmd, bytes.NewBuffer(jsonData))
} else {
req, err = http.NewRequestWithContext(ctx, reqType, "https://api.estuary.tech/"+cmd, nil)
}
if err != nil {
return "", err
}

req.Header.Add("Authorization", "Bearer "+EstuaryAPIKey)
req.Header.Add("Content-Type", "application/json")
resp, err := c.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}

errStruct := new(ErrorStruct)
err = json.Unmarshal(body, errStruct)
if err == nil {
if errStruct.Error() != "" {
return string(body), errStruct
}
}

return string(body), nil
}

func PinEstuary(cid, name string) error {
jsonData, _ := json.Marshal(&EstuaryFile{Cid: cid, Name: name})
_, err := doEstuaryRequest("POST", "pinning/pins", jsonData)
return err
}

func UpdatePinEstuary(oldcid, newcid, name string) {
resp, err := doEstuaryRequest("GET", "pinning/pins?cid="+oldcid, nil)
if err != nil {
log.Println("Error getting Estuary pin:", err)
return
}
pinResp := new(IPFSRemotePinningResponse)
err = json.Unmarshal([]byte(resp), pinResp)
if err != nil {
log.Println("Error decoding Estuary pin list:", err)
return
}
// FIXME Estuary doesn't seem to support `cid` GET field yet, this code can be removed when it does:
var reqId string
pinResp.Count = 0
for _, pinResult := range pinResp.Results {
if pinResult.Pin.Cid == oldcid {
reqId = pinResult.RequestId
pinResp.Count = 1
break
}
}
// END OF FIXME
jsonData, _ := json.Marshal(&EstuaryFile{Cid: newcid, Name: name})
if pinResp.Count > 0 {
_, err := doEstuaryRequest("POST", "pinning/pins/"+reqId, jsonData)
if err != nil {
log.Println("Error updating Estuary pin:", err)
} else {
return
}
}
err = PinEstuary(newcid, name)
if err != nil {
log.Println("Error pinning to Estuary:", err)
}
}

// WatchDog watches for directory updates, periodically updates IPNS records, and updates recursive pins.
func WatchDog() {
// Init WatchDog
Expand Down Expand Up @@ -754,7 +651,7 @@ func WatchDog() {
log.Println(dk.ID, "not found, generating...")
ik := GenerateKey(dk.ID)
var err error
dk.CID, err = AddDir(dk.Dir, dk.Nocopy, dk.Pin, dk.Estuary)
dk.CID, err = AddDir(dk.Dir, dk.Nocopy, dk.Pin)
if err != nil {
log.Panicln("[ERROR] Failed to add directory:", err)
}
Expand All @@ -772,9 +669,6 @@ func WatchDog() {
if dk.Pin {
UpdatePin(dk.CID, fCID, dk.Nocopy)
}
if dk.Estuary {
UpdatePinEstuary(dk.CID, fCID, strings.Split(dk.MFSPath, "/")[0])
}
Publish(fCID, dk.ID)
dk.CID = fCID
log.Println(dk.MFSPath, "updated...")
Expand Down