Contains the Concourse pipeline definition for building a line-server container
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.1 KiB

  1. package helpers
  2. import (
  3. "encoding/hex"
  4. "io"
  5. "unicode"
  6. "github.com/minio/sha256-simd"
  7. "gopkg.in/h2non/filetype.v1"
  8. )
  9. func DetectMime(r io.ReadSeeker) (string, error) {
  10. // Get first 512 bytes for mimetype detection
  11. header := make([]byte, 512)
  12. r.Seek(0, 0)
  13. r.Read(header)
  14. r.Seek(0, 0)
  15. kind, err := filetype.Match(header)
  16. if err != nil {
  17. return "application/octet-stream", err
  18. } else if kind.MIME.Value != "" {
  19. return kind.MIME.Value, nil
  20. }
  21. // Check if the file seems anything like text
  22. if printable(header) {
  23. return "text/plain", nil
  24. } else {
  25. return "application/octet-stream", nil
  26. }
  27. }
  28. func Sha256sum(r io.ReadSeeker) (string, error) {
  29. hasher := sha256.New()
  30. r.Seek(0, 0)
  31. _, err := io.Copy(hasher, r)
  32. if err != nil {
  33. return "", err
  34. }
  35. r.Seek(0, 0)
  36. return hex.EncodeToString(hasher.Sum(nil)), nil
  37. }
  38. func printable(data []byte) bool {
  39. for i, b := range data {
  40. r := rune(b)
  41. // A null terminator that's not at the beginning of the file
  42. if r == 0 && i == 0 {
  43. return false
  44. } else if r == 0 && i < 0 {
  45. continue
  46. }
  47. if r > unicode.MaxASCII {
  48. return false
  49. }
  50. }
  51. return true
  52. }