alpha version
This commit is contained in:
116
app/processor.go
Normal file
116
app/processor.go
Normal file
@ -0,0 +1,116 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/alex-savin/go-receipt-tracker/models"
|
||||
tele "gopkg.in/telebot.v4"
|
||||
)
|
||||
|
||||
func Parse(c *tele.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseReceipt .
|
||||
func parseReceipt(j string) (*models.Receipt, error) {
|
||||
var receipt *models.Receipt
|
||||
if err := json.Unmarshal([]byte(j), &receipt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return receipt, nil
|
||||
}
|
||||
|
||||
func imgType(i string) string {
|
||||
if doesFileExist(i) {
|
||||
bytes, err := os.ReadFile(i)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Determine the content type of the image file
|
||||
mimeType := http.DetectContentType(bytes)
|
||||
|
||||
switch mimeType {
|
||||
case "image/jpeg":
|
||||
return "image/jpeg"
|
||||
case "image/png":
|
||||
return "image/png"
|
||||
case "image/webp":
|
||||
return "image/webp"
|
||||
case "image/svg+xml":
|
||||
return "image/svg+xml"
|
||||
case "image/gif":
|
||||
return "image/gif"
|
||||
case "image/avif":
|
||||
return "image/avif"
|
||||
case "image/apng":
|
||||
return "image/apng"
|
||||
}
|
||||
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// readImage .
|
||||
func readImage(i string) string {
|
||||
if doesFileExist(i) {
|
||||
bytes, err := os.ReadFile(i)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Prepend the appropriate URI scheme header depending
|
||||
// on the MIME type
|
||||
var base64Encoding string
|
||||
|
||||
// Append the base64 encoded output
|
||||
base64Encoding += toBase64(bytes)
|
||||
|
||||
return base64Encoding
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// toBase64 .
|
||||
func toBase64(b []byte) string {
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
// doesFileExist function to check if file exists
|
||||
func doesFileExist(fileName string) bool {
|
||||
_, error := os.Stat(fileName)
|
||||
|
||||
// check if error is "file not exists"
|
||||
if os.IsNotExist(error) {
|
||||
log.Printf("%v file does not exist\n", fileName)
|
||||
return false
|
||||
} else {
|
||||
log.Printf("%v file exist\n", fileName)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// imgType .
|
||||
func (a App) imgType(fileName string) (string, error) {
|
||||
f, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
a.Log.Error("")
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
buff := make([]byte, 512) // docs tell that it take only first 512 bytes into consideration
|
||||
if _, err = f.Read(buff); err != nil {
|
||||
fmt.Println(err) // do something with that error
|
||||
return "", err
|
||||
}
|
||||
|
||||
return http.DetectContentType(buff), nil
|
||||
}
|
Reference in New Issue
Block a user