Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
fix(cmd): some cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderpick committed Oct 29, 2018
1 parent 14e927b commit 5b1d371
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 97 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Expand Up @@ -49,8 +49,7 @@ _testmain.go
*.iml
.gx/
.ipfs
.textile
dist/textile-go-*
.textile*

# Development environment files
.ackrc
Expand Down
17 changes: 11 additions & 6 deletions cmd/add.go
Expand Up @@ -137,27 +137,32 @@ func (x *imageCmd) Execute(args []string) error {
}
defer file.Close()

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile("file", filepath.Base(path))
if err != nil {
return err
}
if _, err = io.Copy(part, file); err != nil {
return err
}
writer.Close()

var added *core.AddDataResult
var added *struct {
Items []core.AddDataResult `json:"items"`
}
if err := executeJsonCmd(POST, "add/"+x.Name(), params{
args: args,
payload: body,
payload: &body,
ctype: writer.FormDataContentType(),
}, &added); err != nil {
return err
}

fmt.Println("id :" + added.Id)
fmt.Println("key :" + added.Key)
for _, item := range added.Items {
fmt.Println("id: " + item.Id)
fmt.Println("key: " + item.Key)
}
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions core/api.go
Expand Up @@ -153,7 +153,7 @@ func (a *api) addImage(g *gin.Context) {
g.String(http.StatusBadRequest, err.Error())
return
}
fileHeaders := form.File["files"]
fileHeaders := form.File["file"]
var adds []*AddDataResult
for _, header := range fileHeaders {
file, err := header.Open()
Expand All @@ -166,9 +166,10 @@ func (a *api) addImage(g *gin.Context) {
g.String(http.StatusBadRequest, err.Error())
return
}
file.Close()
adds = append(adds, added)
}
g.JSON(http.StatusCreated, adds)
g.JSON(http.StatusCreated, gin.H{"items": adds})
}

// -- HELPERS -- //
Expand Down
3 changes: 3 additions & 0 deletions core/cafe.go
Expand Up @@ -51,6 +51,9 @@ func (t *Textile) RefreshCafeSession(cafeId string) (*repo.CafeSession, error) {

// CheckCafeMessages fetches new messages from registered cafes
func (t *Textile) CheckCafeMessages() error {
if err := t.touchDatastore(); err != nil {
return err
}
if !t.Online() {
return ErrOffline
}
Expand Down
21 changes: 14 additions & 7 deletions core/cafe_api_test.go
Expand Up @@ -23,22 +23,22 @@ var session *repo.CafeSession
var blockHash = "QmbQ4K3vXNJ3DjCNdG2urCXs7BuHqWQG1iSjZ8fbnF8NMs"
var photoHash = "QmSUnsZi9rGvPZLWy2v5N7fNxUWVNnA5nmppoM96FbLqLp"

func TestPin_Setup(t *testing.T) {
func TestCafeApi_Setup(t *testing.T) {
// start one node
os.RemoveAll(repoPath1)
accnt1 := keypair.Random()
if err := InitRepo(InitConfig{
Account: *accnt1,
RepoPath: repoPath1,
LogLevel: logger.DEBUG,
LogLevel: logger.ERROR,
}); err != nil {
t.Errorf("init node1 failed: %s", err)
return
}
var err error
node1, err = NewTextile(RunConfig{
RepoPath: repoPath1,
LogLevel: logger.DEBUG,
LogLevel: logger.ERROR,
})
if err != nil {
t.Errorf("create node1 failed: %s", err)
Expand All @@ -52,14 +52,14 @@ func TestPin_Setup(t *testing.T) {
if err := InitRepo(InitConfig{
Account: *accnt2,
RepoPath: repoPath2,
LogLevel: logger.DEBUG,
LogLevel: logger.ERROR,
}); err != nil {
t.Errorf("init node2 failed: %s", err)
return
}
node2, err = NewTextile(RunConfig{
RepoPath: repoPath2,
LogLevel: logger.DEBUG,
LogLevel: logger.ERROR,
CafeOpen: true,
CafeBindAddr: "127.0.0.1:5000",
})
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestPin_Setup(t *testing.T) {
}
}

func TestPin_Pin(t *testing.T) {
func TestCafeApi_Pin(t *testing.T) {
block, err := os.Open("testdata/" + blockHash)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestPin_Pin(t *testing.T) {
}
}

func TestPin_PinArchive(t *testing.T) {
func TestCafeApi_PinArchive(t *testing.T) {
archive, err := os.Open("testdata/" + photoHash + ".tar.gz")
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -161,6 +161,13 @@ func TestPin_PinArchive(t *testing.T) {
}
}

func TestCafeApi_Teardown(t *testing.T) {
node1.Stop()
node2.Stop()
node1 = nil
node2 = nil
}

func pin(reader io.Reader, cType string, token string, addr string) (*http.Response, error) {
url := fmt.Sprintf("%s/cafe/v0/pin", addr)
req, err := http.NewRequest("POST", url, reader)
Expand Down
11 changes: 7 additions & 4 deletions core/main.go
Expand Up @@ -597,20 +597,23 @@ func (t *Textile) runQueues() {
}
tick := time.NewTicker(freq)
defer tick.Stop()
go func() {
time.Sleep(time.Second)
t.flushQueues()
}()
t.flushQueues()
for {
select {
case <-tick.C:
t.flushQueues()
case <-t.done:
return
}
}
}

// flushQueues flushes each message queue
func (t *Textile) flushQueues() {
if err := t.touchDatastore(); err != nil {
log.Error(err)
return
}
go t.threadsOutbox.Flush()
go t.cafeOutbox.Flush()
go t.cafeInbox.CheckMessages()
Expand Down
23 changes: 10 additions & 13 deletions core/main_test.go
Expand Up @@ -19,7 +19,7 @@ func TestInitRepo(t *testing.T) {
if err := InitRepo(InitConfig{
Account: *accnt,
RepoPath: repoPath,
LogLevel: logger.DEBUG,
LogLevel: logger.ERROR,
}); err != nil {
t.Errorf("init node failed: %s", err)
}
Expand All @@ -29,7 +29,7 @@ func TestNewTextile(t *testing.T) {
var err error
node, err = NewTextile(RunConfig{
RepoPath: repoPath,
LogLevel: logger.DEBUG,
LogLevel: logger.ERROR,
})
if err != nil {
t.Errorf("create node failed: %s", err)
Expand Down Expand Up @@ -74,19 +74,19 @@ func TestCore_AddThread(t *testing.T) {
}
}

func TestCore_AddPhoto(t *testing.T) {
added, err := node.AddPhoto("../photo/testdata/image.jpg")
func TestCore_AddImage(t *testing.T) {
added, err := node.AddImageByPath("../images/testdata/image.jpg")
if err != nil {
t.Errorf("add photo failed: %s", err)
t.Errorf("add image failed: %s", err)
return
}
if len(added.Id) == 0 {
t.Errorf("add photo got bad id")
t.Errorf("add image got bad id")
}
// test adding an image w/o the orientation tag
added2, err := node.AddPhoto("../photo/testdata/image-no-orientation.jpg")
added2, err := node.AddImageByPath("../images/testdata/image-no-orientation.jpg")
if err != nil {
t.Errorf("add photo w/o orientation tag failed: %s", err)
t.Errorf("add image w/o orientation tag failed: %s", err)
return
}
if len(added2.Id) == 0 {
Expand All @@ -95,8 +95,7 @@ func TestCore_AddPhoto(t *testing.T) {
}

func TestCore_Stop(t *testing.T) {
err := node.Stop()
if err != nil {
if err := node.Stop(); err != nil {
t.Errorf("stop node failed: %s", err)
}
}
Expand All @@ -114,7 +113,5 @@ func TestCore_OnlineAgain(t *testing.T) {
}

func TestCore_Teardown(t *testing.T) {
os.RemoveAll(node.GetRepoPath())
os.RemoveAll(node1.GetRepoPath())
os.RemoveAll(node2.GetRepoPath())
node = nil
}
Binary file modified dist/textile
Binary file not shown.
4 changes: 3 additions & 1 deletion gateway/main.go
Expand Up @@ -28,7 +28,9 @@ type Gateway struct {
// Start creates a gateway server
func (g *Gateway) Start(addr string) {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = core.Node.Writer()
if core.Node != nil {
gin.DefaultWriter = core.Node.Writer()
}

// setup router
router := gin.Default()
Expand Down
75 changes: 42 additions & 33 deletions images/main.go
Expand Up @@ -78,7 +78,7 @@ func ImagePathForSize(size ImageSize) ImagePath {
}
}

// Metadata (mostly exif data) stripped from images pre-encoding.
// Metadata (mostly exif data) stripped from images, pre-encoding.
// NOTE: Metadata is encrypted and stored alongside encoded images (in the photo set DAG).
type Metadata struct {
Version string `json:"version"`
Expand All @@ -94,6 +94,47 @@ type Metadata struct {
Longitude float64 `json:"longitude,omitempty"`
}

// NewMetadata returns a new image meta data object
func NewMetadata(
reader io.Reader,
path string,
ext string,
format Format,
encodingFormat Format,
width int,
height int,
version string,
) (Metadata, error) {
var created time.Time
var lat, lon float64
x, err := exif.Decode(reader)
if err == nil {
// time taken
createdTmp, err := x.DateTime()
if err == nil {
created = createdTmp
}
// coords taken
latTmp, lonTmp, err := x.LatLong()
if err == nil {
lat, lon = latTmp, lonTmp
}
}
return Metadata{
Version: version,
Created: created,
Added: time.Now(),
Name: strings.TrimSuffix(filepath.Base(path), ext),
Ext: ext,
OriginalFormat: string(format),
EncodingFormat: string(encodingFormat),
Width: width,
Height: height,
Latitude: lat,
Longitude: lon,
}, nil
}

// DecodeImage returns a cleaned reader from an image file
func DecodeImage(file multipart.File) (*bytes.Reader, *Format, *image.Point, error) {
img, formatStr, err := image.Decode(file)
Expand Down Expand Up @@ -188,38 +229,6 @@ func DecodeExif(reader io.Reader) *exif.Exif {
return exf
}

// NewMetadata reads any available meta/exif data from a photo
func NewMetadata(reader io.Reader, path string, ext string, format Format, encodingFormat Format, width int, height int, version string) (Metadata, error) {
var created time.Time
var lat, lon float64
x, err := exif.Decode(reader)
if err == nil {
// time taken
createdTmp, err := x.DateTime()
if err == nil {
created = createdTmp
}
// coords taken
latTmp, lonTmp, err := x.LatLong()
if err == nil {
lat, lon = latTmp, lonTmp
}
}
return Metadata{
Version: version,
Created: created,
Added: time.Now(),
Name: strings.TrimSuffix(filepath.Base(path), ext),
Ext: ext,
OriginalFormat: string(format),
EncodingFormat: string(encodingFormat),
Width: width,
Height: height,
Latitude: lat,
Longitude: lon,
}, nil
}

// correctOrientation returns a copy of an image (jpg|png|gif) with exif removed
func correctOrientation(img image.Image, exf *exif.Exif) (image.Image, error) {
if exf == nil {
Expand Down

0 comments on commit 5b1d371

Please sign in to comment.