Skip to content

Commit

Permalink
Detect MIME types on missing SQL entries
Browse files Browse the repository at this point in the history
Using magic number signatures to detect file types if the metadata entry is missing.

Closes #10.
  • Loading branch information
xeals committed May 2, 2018
1 parent c42606d commit 08b64ad
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
17 changes: 16 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@
[[constraint]]
name = "github.com/urfave/cli"
version = "1.20.0"

[[constraint]]
name = "github.com/h2non/filetype"
version = "1.0.5"
33 changes: 31 additions & 2 deletions cmd/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cmd

import (
"fmt"
"io/ioutil"
"log"
"os"

"github.com/h2non/filetype"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/xeals/signal-back/types"
Expand Down Expand Up @@ -71,12 +73,19 @@ func ExtractAttachments(bf *types.BackupFile) error {
ps := f.GetStatement().GetParameters()
if len(ps) == 25 { // Contains blob information
aEncs[*ps[19].IntegerParameter] = *ps[3].StringParamter
log.Printf("found attachment metadata %v: `%v`\n", *ps[19].IntegerParameter, ps)
}

if a := f.GetAttachment(); a != nil {
ext := getExt(aEncs[*a.AttachmentId], *a.AttachmentId)
fileName := fmt.Sprintf("%v%s", *a.AttachmentId, ext)
log.Printf("found attachment binary %v\n\n", *a.AttachmentId)
id := *a.AttachmentId

mime, hasMime := aEncs[id]
ext := getExt(mime, id)

fileName := fmt.Sprintf("%v%s", id, ext)
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, os.ModePerm)

if err != nil {
return errors.Wrap(err, "failed to open output file")
}
Expand All @@ -86,6 +95,22 @@ func ExtractAttachments(bf *types.BackupFile) error {
if err = file.Close(); err != nil {
return errors.Wrap(err, "failed to close output file")
}

if !hasMime { // Time to look into the file itself and guess.
buf, err := ioutil.ReadFile(fileName)
if err != nil {
return errors.Wrap(err, "failed to read output file for MIME detection")
}
kind, err := filetype.Match(buf)
if err != nil {
log.Printf("unable to detect file type: %s\n", err.Error())
}
if err = os.Rename(fileName, fileName+"."+kind.Extension); err != nil {
log.Println("unknown file type")
return errors.Wrap(err, "unable to rename output file")
}
log.Println("found file type:", kind.MIME)
}
}
}
}
Expand Down Expand Up @@ -223,6 +248,10 @@ func getExt(mime string, file uint64) string {
warnExt(file, "otf")
return ".ttf"

case "":
log.Printf("file `%v` has no associated SQL entry; going to have to guess at its encoding", file)
return ""

default:
log.Printf("encoding `%s` not recognised. create a PR or issue if you think it should be\n", mime)
log.Printf("if you can provide details on the file `%v` as well, it would be appreciated", file)
Expand Down

0 comments on commit 08b64ad

Please sign in to comment.