Browse Source

remove magicmime dependency

Matthieu Jacquot 4 years ago
parent
commit
5f384be6e4
6 changed files with 45 additions and 57 deletions
  1. 12 9
      Gopkg.lock
  2. 0 4
      Gopkg.toml
  3. 11 18
      graph.go
  4. 14 16
      mime.go
  5. 0 2
      server.go
  6. 8 8
      webid.go

+ 12 - 9
Gopkg.lock

@@ -25,6 +25,17 @@
   revision = "947c36da3153ff334e74d9d980de341d25f358ba"
   version = "v1.1"
 
+[[projects]]
+  branch = "master"
+  digest = "1:d4d1295925d5dfab49954082388dd3556860fa2daa70df9775a6d5bfec6c5054"
+  name = "github.com/gabriel-vasile/mimetype"
+  packages = [
+    ".",
+    "matchers",
+  ]
+  pruneopts = "UT"
+  revision = "d4f1602c882009c84583421e8f6e124d3eca176e"
+
 [[projects]]
   digest = "1:e72d1ebb8d395cf9f346fd9cbc652e5ae222dd85e0ac842dc57f175abed6d195"
   name = "github.com/gorilla/securecookie"
@@ -57,14 +68,6 @@
   pruneopts = "UT"
   revision = "d14aff371f65d19049be51de3b604f22a2ca853e"
 
-[[projects]]
-  digest = "1:1090c43442963716e8f440ca37bd9e7804bc656e669662ab30995fad2f8dbd33"
-  name = "github.com/rakyll/magicmime"
-  packages = ["."]
-  pruneopts = "UT"
-  revision = "9b99294d6b2216897632b9734769d8ce4568681e"
-  version = "v0.1.0"
-
 [[projects]]
   digest = "1:18752d0b95816a1b777505a97f71c7467a8445b8ffb55631a7bf779f6ba4fa83"
   name = "github.com/stretchr/testify"
@@ -100,10 +103,10 @@
   input-imports = [
     "github.com/boltdb/bolt",
     "github.com/elazarl/goproxy",
+    "github.com/gabriel-vasile/mimetype",
     "github.com/gorilla/securecookie",
     "github.com/linkeddata/gojsonld",
     "github.com/presbrey/goraptor",
-    "github.com/rakyll/magicmime",
     "github.com/stretchr/testify/assert",
     "golang.org/x/net/webdav",
     "golang.org/x/net/websocket",

+ 0 - 4
Gopkg.toml

@@ -45,10 +45,6 @@
   branch = "master"
   name = "github.com/presbrey/goraptor"
 
-[[constraint]]
-  name = "github.com/rakyll/magicmime"
-  version = "0.1.0"
-
 [[constraint]]
   name = "github.com/stretchr/testify"
   version = "1.2.2"

+ 11 - 18
graph.go

@@ -107,26 +107,10 @@ func jterm2term(term jsonld.Term) Term {
 	return nil
 }
 
-// helper function returns true if term is nil, otherwise checks with triple
-func skipOnNilEqual(c *Triple, t Term, part string) bool {
-	if t == nil {
-		return true
-	}
-
-	switch part {
-	case "subject":
-		return c.Subject.Equal(t)
-	case "predicate":
-		return c.Predicate.Equal(t)
-	default:
-		return c.Object.Equal(t)
-	}
-}
-
 // One returns one triple based on a triple pattern of S, P, O objects
 func (g *Graph) One(s Term, p Term, o Term) *Triple {
 	for triple := range g.IterTriples() {
-		if skipOnNilEqual(triple, s, "subject") && skipOnNilEqual(triple, p, "predicate") && skipOnNilEqual(triple, o, "object") {
+		if isNilOrEquals(s, triple.Subject) && isNilOrEquals(p, triple.Predicate) && isNilOrEquals(o, triple.Object) {
 			return triple
 		}
 	}
@@ -168,7 +152,7 @@ func (g *Graph) All(s Term, p Term, o Term) []*Triple {
 			continue
 		}
 
-		if skipOnNilEqual(triple, s, "subject") && skipOnNilEqual(triple, p, "predicate") && skipOnNilEqual(triple, o, "object") {
+		if isNilOrEquals(s, triple.Subject) && isNilOrEquals(p, triple.Predicate) && isNilOrEquals(o, triple.Object) {
 			triples = append(triples, triple)
 		}
 	}
@@ -454,3 +438,12 @@ func (g *Graph) JSONPatch(r io.Reader) error {
 	}
 	return nil
 }
+
+// isNilOrEquals is a helper function returns true if first term is nil, otherwise checks equality
+func isNilOrEquals(t1 Term, t2 Term) bool {
+	if t1 == nil {
+		return true
+	}
+
+	return t2.Equal(t1)
+}

+ 14 - 16
mime.go

@@ -2,11 +2,13 @@ package gold
 
 import (
 	"errors"
-	crdf "github.com/presbrey/goraptor"
+	"fmt"
 	"mime"
 	"path/filepath"
 
-	"github.com/rakyll/magicmime"
+	"github.com/gabriel-vasile/mimetype"
+	crdf "github.com/presbrey/goraptor"
+
 	"regexp"
 	"sync"
 )
@@ -76,25 +78,21 @@ func init() {
 		}
 		serializerMimes = append(serializerMimes, mime)
 	}
-
-	magicmime.Open(magicmime.MAGIC_MIME_TYPE)
 }
 
-func GuessMimeType(path string) (mimeType string, err error) {
-	// Get the mime type of the file. In some cases, MagicMime
-	// returns an empty string, and in rare cases (about 1 in 10000),
-	// it returns unprintable characters. These are not valid mime
-	// types and cause ingest to fail. So we default to the safe
-	// text/plain and then set the MimeType only if
-	// MagicMime returned something that looks legit.
-	// Open the Mime Magic DB only once.
-	mimeType = "text/plain"
-	mutex.Lock()
-	guessedType, _ := magicmime.TypeByFile(path)
-	mutex.Unlock()
+func GuessMimeType(path string) (string, error) {
+	mimeType := "text/plain"
+
+	guessedType, _, err := mimetype.DetectFile(path)
+	if err != nil {
+		fmt.Printf("Unknown mimeType from file: %s ==> %s", path, err)
+		return mimeType, err
+	}
+
 	if guessedType != "" && validMimeType.MatchString(guessedType) {
 		mimeType = guessedType
 	}
+
 	return mimeType, nil
 }
 

+ 0 - 2
server.go

@@ -45,8 +45,6 @@ var (
 	debugFlags  = log.Flags() | log.Lshortfile
 	debugPrefix = "[debug] "
 
-	// magic *magicmime.Magic
-
 	methodsAll = []string{
 		"OPTIONS", "HEAD", "GET",
 		"PATCH", "POST", "PUT", "MKCOL", "DELETE",

+ 8 - 8
webid.go

@@ -491,15 +491,15 @@ func (req *httpRequest) AddWorkspaces(account webidAccount, g *Graph) error {
 		}
 
 		// Append workspace URL to the preferencesFile
-		if ws.Name != "Inbox" || ws.Name != "Timeline" {
-			pref.AddTriple(wsTerm, ns.rdf.Get("type"), ns.space.Get("Workspace"))
-			if len(ws.Type) > 0 {
-				pref.AddTriple(wsTerm, ns.rdf.Get("type"), ns.space.Get(ws.Type))
-			}
-			pref.AddTriple(wsTerm, ns.dct.Get("title"), NewLiteral(ws.Label))
-
-			pref.AddTriple(NewResource(account.WebID), ns.space.Get("workspace"), wsTerm)
+		//if ws.Name != "Inbox" || ws.Name != "Timeline" { <- this assertion is always true ...
+		pref.AddTriple(wsTerm, ns.rdf.Get("type"), ns.space.Get("Workspace"))
+		if len(ws.Type) > 0 {
+			pref.AddTriple(wsTerm, ns.rdf.Get("type"), ns.space.Get(ws.Type))
 		}
+		pref.AddTriple(wsTerm, ns.dct.Get("title"), NewLiteral(ws.Label))
+
+		pref.AddTriple(NewResource(account.WebID), ns.space.Get("workspace"), wsTerm)
+		//}
 	}
 
 	resource, _ := req.pathInfo(account.PrefURI)