From ed2bb81b8d0951fe8433d39cb8ff9d433b3a90b2 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Tue, 26 Jun 2018 15:55:36 -0400 Subject: [PATCH] Code cleanups to make code climate happy. License: MIT Signed-off-by: Kevin Atkinson --- core/commands/root.go | 2 +- core/commands/urlstore.go | 2 +- filestore/fsrefstore.go | 19 ++++++++++--------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/core/commands/root.go b/core/commands/root.go index 1f62b7205..648a2a886 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -136,7 +136,7 @@ var rootSubcommands = map[string]*cmds.Command{ "tar": lgc.NewCommand(TarCmd), "file": lgc.NewCommand(unixfs.UnixFSCmd), "update": lgc.NewCommand(ExternalBinary()), - "urlstore": lgc.NewCommand(UrlStoreCmd), + "urlstore": lgc.NewCommand(urlStoreCmd), "version": lgc.NewCommand(VersionCmd), "shutdown": lgc.NewCommand(daemonShutdownCmd), } diff --git a/core/commands/urlstore.go b/core/commands/urlstore.go index 58f9d6aa5..8818e33b7 100644 --- a/core/commands/urlstore.go +++ b/core/commands/urlstore.go @@ -16,7 +16,7 @@ import ( cmdkit "gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit" ) -var UrlStoreCmd = &cmds.Command{ +var urlStoreCmd = &cmds.Command{ Subcommands: map[string]*cmds.Command{ "add": urlAdd, diff --git a/filestore/fsrefstore.go b/filestore/fsrefstore.go index 710deac03..be0be92c7 100644 --- a/filestore/fsrefstore.go +++ b/filestore/fsrefstore.go @@ -123,11 +123,10 @@ func (f *FileManager) Get(c *cid.Cid) (blocks.Block, error) { } func (f *FileManager) readDataObj(c *cid.Cid, d *pb.DataObj) ([]byte, error) { - if !IsURL(d.GetFilePath()) { - return f.readFileDataObj(c, d) - } else { + if IsURL(d.GetFilePath()) { return f.readURLDataObj(c, d) } + return f.readFileDataObj(c, d) } func (f *FileManager) getDataObj(c *cid.Cid) (*pb.DataObj, error) { @@ -266,7 +265,12 @@ func (f *FileManager) Put(b *posinfo.FilestoreNode) error { func (f *FileManager) putTo(b *posinfo.FilestoreNode, to putter) error { var dobj pb.DataObj - if !IsURL(b.PosInfo.FullPath) { + if IsURL(b.PosInfo.FullPath) { + if !f.AllowUrls { + return fmt.Errorf("urlstore not enabled") + } + dobj.FilePath = proto.String(b.PosInfo.FullPath) + } else { if !f.AllowFiles { return fmt.Errorf("filestore not enabled") } @@ -280,11 +284,6 @@ func (f *FileManager) putTo(b *posinfo.FilestoreNode, to putter) error { } dobj.FilePath = proto.String(filepath.ToSlash(p)) - } else { - if !f.AllowUrls { - return fmt.Errorf("urlstore not enabled") - } - dobj.FilePath = proto.String(b.PosInfo.FullPath) } dobj.Offset = proto.Uint64(b.PosInfo.Offset) dobj.Size_ = proto.Uint64(uint64(len(b.RawData()))) @@ -314,6 +313,8 @@ func (f *FileManager) PutMany(bs []*posinfo.FilestoreNode) error { return batch.Commit() } +// IsURL returns true if the string represents a valid URL that the +// urlstore can handle. func IsURL(str string) bool { return (len(str) > 7 && str[0] == 'h' && str[1] == 't' && str[2] == 't' && str[3] == 'p') && ((len(str) > 8 && str[4] == 's' && str[5] == ':' && str[6] == '/' && str[7] == '/') ||