A continuation of HomeBox the inventory and organization system built for the Home User
feat: add OpenFoodFacts as barcode lookup provider
palini762cc6527f6fbcb
diff --git a/backend/app/api/handlers/v1/v1_ctrl_product_search.go b/backend/app/api/handlers/v1/v1_ctrl_product_search.goindex 10c180a5..9a33bece 100644--- a/backend/app/api/handlers/v1/v1_ctrl_product_search.go+++ b/backend/app/api/handlers/v1/v1_ctrl_product_search.go@@ -19,6 +19,8 @@ import ("github.com/sysadminsmedia/homebox/backend/internal/web/adapters")+const barcodeHTTPTimeoutSec = 10+type UPCITEMDBResponse struct {Code string `json:"code"`Total int `json:"total"`@@ -56,6 +58,19 @@ type UPCITEMDBResponse struct {} `json:"items"`}+type OpenFoodFactsResponse struct {+ Code string `json:"code"`+ Status int `json:"status"`+ Product struct {+ ProductName string `json:"product_name"`+ Brands string `json:"brands"`+ Categories string `json:"categories"`+ ImageFrontURL string `json:"image_front_url"`+ Quantity string `json:"quantity"`+ GenericName string `json:"generic_name"`+ } `json:"product"`+}+type BARCODESPIDER_COMResponse struct {ItemResponse struct {Code int `json:"code"`@@ -92,167 +107,274 @@ type BARCODESPIDER_COMResponse struct {} `json:"Stores"`}-// HandleGenerateQRCode godoc-//-// @Summary Search EAN from Barcode-// @Tags Items-// @Produce json-// @Param data query string false "barcode to be searched"-// @Success 200 {object} []repo.BarcodeProduct-// @Router /v1/products/search-from-barcode [GET]-// @Security Bearer-func (ctrl *V1Controller) HandleProductSearchFromBarcode(conf config.BarcodeAPIConf) errchain.HandlerFunc {- type query struct {- // 80 characters is the longest non-2D barcode length (GS1-128)- EAN string `schema:"productEAN" validate:"required,max=80"`+func lookupUPCItemDB(iEan string) ([]repo.BarcodeProduct, error) {+ client := &http.Client{Timeout: barcodeHTTPTimeoutSec * time.Second}+ resp, err := client.Get("https://api.upcitemdb.com/prod/trial/lookup?upc=" + url.QueryEscape(iEan))+ if err != nil {+ return nil, err}- return func(w http.ResponseWriter, r *http.Request) error {- q, err := adapters.DecodeQuery[query](r)- if err != nil {- return err- }+ defer func() {+ err = errors.Join(err, resp.Body.Close())+ }()- const TIMEOUT_SEC = 10+ if resp.StatusCode != http.StatusOK {+ return nil, fmt.Errorf("API returned status code: %d", resp.StatusCode)+ }- log.Info().Msg("Processing barcode lookup request on: " + q.EAN)+ body, err := io.ReadAll(resp.Body)+ if err != nil {+ return nil, err+ }- // Search on UPCITEMDB- var products []repo.BarcodeProduct+ var result UPCITEMDBResponse+ if err := json.Unmarshal(body, &result); err != nil {+ log.Error().Msg("Can not unmarshal JSON from upcitemdb.com")+ return nil, err+ }- // www.ean-search.org/: not free+ var res []repo.BarcodeProduct- // Example code: dewalt 5035048748428+ for _, it := range result.Items {+ var p repo.BarcodeProduct+ p.SearchEngineName = "upcitemdb.com"+ p.Barcode = iEan- upcitemdb := func(iEan string) ([]repo.BarcodeProduct, error) {- client := &http.Client{Timeout: TIMEOUT_SEC * time.Second}- resp, err := client.Get("https://api.upcitemdb.com/prod/trial/lookup?upc=" + iEan)- if err != nil {- return nil, err- }+ p.Item.Description = it.Description+ p.Item.Name = it.Title+ p.Manufacturer = it.Brand+ p.ModelNumber = it.Model+ if len(it.Images) != 0 {+ p.ImageURL = it.Images[0]+ }- defer func() {- err = errors.Join(err, resp.Body.Close())- }()+ res = append(res, p)+ }- if resp.StatusCode != http.StatusOK {- return nil, fmt.Errorf("API returned status code: %d", resp.StatusCode)- }+ return res, nil+}- // We Read the response body on the line below.- body, err := io.ReadAll(resp.Body)- if err != nil {- return nil, err- }+func lookupBarcodespider(tokenAPI string, iEan string) ([]repo.BarcodeProduct, error) {+ if len(tokenAPI) == 0 {+ return nil, errors.New("no api token configured for barcodespider. " ++ "Please define the api token in environment variable HBOX_BARCODE_TOKEN_BARCODESPIDER")+ }- // Uncomment the following string for debug- // sb := string(body)- // log.Debug().Msg("Response: " + sb)+ req, err := http.NewRequest(+ "GET", "https://api.barcodespider.com/v1/lookup?upc="+url.QueryEscape(iEan), nil)- var result UPCITEMDBResponse- if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer- log.Error().Msg("Can not unmarshal JSON")- }+ if err != nil {+ return nil, err+ }- var res []repo.BarcodeProduct+ req.Header.Add("token", tokenAPI)- for _, it := range result.Items {- var p repo.BarcodeProduct- p.SearchEngineName = "upcitemdb.com"- p.Barcode = iEan+ client := &http.Client{Timeout: barcodeHTTPTimeoutSec * time.Second}- p.Item.Description = it.Description- p.Item.Name = it.Title- p.Manufacturer = it.Brand- p.ModelNumber = it.Model- if len(it.Images) != 0 {- p.ImageURL = it.Images[0]- }+ resp, err := client.Do(req)+ if err != nil {+ return nil, err+ }- res = append(res, p)- }+ defer func() {+ err = errors.Join(err, resp.Body.Close())+ }()++ if resp.StatusCode != http.StatusOK {+ return nil, fmt.Errorf("barcodespider API returned status code: %d", resp.StatusCode)+ }- return res, nil+ body, err := io.ReadAll(resp.Body)+ if err != nil {+ return nil, err+ }++ var result BARCODESPIDER_COMResponse+ if err := json.Unmarshal(body, &result); err != nil {+ log.Error().Msg("Can not unmarshal JSON from barcodespider.com")+ return nil, err+ }++ var p repo.BarcodeProduct+ p.Barcode = iEan+ p.SearchEngineName = "barcodespider.com"+ p.Item.Name = result.ItemAttributes.Title+ p.Item.Description = result.ItemAttributes.Description+ p.Manufacturer = result.ItemAttributes.Brand+ p.ModelNumber = result.ItemAttributes.Model+ p.ImageURL = result.ItemAttributes.Image++ return []repo.BarcodeProduct{p}, nil+}++// sanitizeHeader removes control characters that could cause HTTP header injection.+func sanitizeHeader(s string) string {+ return strings.Map(func(r rune) rune {+ if r < 0x20 || r == 0x7F {+ return -1}+ return r+ }, s)+}- ps, err := upcitemdb(q.EAN)- if err != nil {- log.Error().Msg("Can not retrieve product from upcitemdb.com" + err.Error())+func lookupOpenFoodFacts(contact string, iEan string) ([]repo.BarcodeProduct, error) {+ client := &http.Client{Timeout: barcodeHTTPTimeoutSec * time.Second}+ req, err := http.NewRequest(+ "GET", "https://world.openfoodfacts.org/api/v2/product/"+url.PathEscape(iEan)+".json", nil)+ if err != nil {+ return nil, err+ }+ userAgent := "Homebox/1.0 (https://github.com/sysadminsmedia/homebox)"+ safeContact := sanitizeHeader(strings.TrimSpace(contact))+ if len(safeContact) > 0 {+ userAgent = "Homebox/1.0 (contact: " + safeContact + ")"+ }+ req.Header.Set("User-Agent", userAgent)++ resp, err := client.Do(req)+ if err != nil {+ return nil, err+ }++ defer func() {+ err = errors.Join(err, resp.Body.Close())+ }()++ if resp.StatusCode != http.StatusOK {+ return nil, fmt.Errorf("OpenFoodFacts API returned status code: %d", resp.StatusCode)+ }++ body, err := io.ReadAll(resp.Body)+ if err != nil {+ return nil, err+ }++ var result OpenFoodFactsResponse+ if err := json.Unmarshal(body, &result); err != nil {+ log.Error().Msg("Can not unmarshal OpenFoodFacts JSON")+ return nil, err+ }++ if result.Status == 0 {+ return nil, nil+ }++ var p repo.BarcodeProduct+ p.Barcode = iEan+ p.SearchEngineName = "openfoodfacts.org"+ p.Item.Name = result.Product.ProductName+ p.Manufacturer = result.Product.Brands++ description := result.Product.GenericName+ if len(result.Product.Categories) > 0 {+ if len(description) > 0 {+ description += " | "}+ description += result.Product.Categories+ }+ if len(result.Product.Quantity) > 0 {+ if len(description) > 0 {+ description += " | "+ }+ description += result.Product.Quantity+ }+ p.Item.Description = description- // Barcode spider implementation- barcodespider := func(tokenAPI string, iEan string) ([]repo.BarcodeProduct, error) {- if len(tokenAPI) == 0 {- return nil, errors.New("no api token configured for barcodespider. " +- "Please define the api token in environment variable HBOX_BARCODE_TOKEN_BARCODESPIDER")- }+ if len(result.Product.ImageFrontURL) > 0 {+ p.ImageURL = result.Product.ImageFrontURL+ }- req, err := http.NewRequest(- "GET", "https://api.barcodespider.com/v1/lookup?upc="+iEan, nil)+ return []repo.BarcodeProduct{p}, nil+}- if err != nil {- return nil, err- }+// fetchImageBase64 fetches an image from the given HTTPS URL and returns it as a base64-encoded data URI.+func fetchImageBase64(imageURL string) (string, error) {+ client := &http.Client{Timeout: barcodeHTTPTimeoutSec * time.Second}+ res, err := client.Get(imageURL)+ if err != nil {+ return "", err+ }+ defer func() {+ _ = res.Body.Close()+ }()- req.Header.Add("token", tokenAPI)+ if res.StatusCode != http.StatusOK {+ return "", fmt.Errorf("image fetch returned status %d", res.StatusCode)+ }- client := &http.Client{Timeout: TIMEOUT_SEC * time.Second}+ contentType := res.Header.Get("Content-Type")+ if !strings.HasPrefix(contentType, "image/") {+ return "", fmt.Errorf("non-image content type: %s", contentType)+ }- resp, err := client.Do(req)- if err != nil {- return nil, err- }+ limitedReader := io.LimitReader(res.Body, 8*1024*1024)+ bytes, err := io.ReadAll(limitedReader)+ if err != nil {+ return "", err+ }- // defer the call to Body.Close(). We also check the error code, and merge- // it with the other error in this code to avoid error overiding.- defer func() {- err = errors.Join(err, resp.Body.Close())- }()+ mimeType := http.DetectContentType(bytes)+ var base64Encoding string+ switch mimeType {+ case "image/jpeg":+ base64Encoding = "data:image/jpeg;base64,"+ case "image/png":+ base64Encoding = "data:image/png;base64,"+ default:+ return "", fmt.Errorf("unsupported image type: %s", mimeType)+ }- if resp.StatusCode != http.StatusOK {- return nil, fmt.Errorf("barcodespider API returned status code: %d", resp.StatusCode)- }+ return base64Encoding + base64.StdEncoding.EncodeToString(bytes), nil+}- // We Read the response body on the line below.- body, err := io.ReadAll(resp.Body)- if err != nil {- return nil, err- }+// HandleProductSearchFromBarcode godoc+//+// @Summary Search EAN from Barcode+// @Tags Items+// @Produce json+// @Param data query string false "barcode to be searched"+// @Success 200 {object} []repo.BarcodeProduct+// @Router /v1/products/search-from-barcode [GET]+// @Security Bearer+func (ctrl *V1Controller) HandleProductSearchFromBarcode(conf config.BarcodeAPIConf) errchain.HandlerFunc {+ type query struct {+ // 80 characters is the longest non-2D barcode length (GS1-128)+ EAN string `schema:"productEAN" validate:"required,max=80"`+ }++ return func(w http.ResponseWriter, r *http.Request) error {+ q, err := adapters.DecodeQuery[query](r)+ if err != nil {+ return err+ }- // Uncomment the following string for debug- // sb := string(body)- // log.Debug().Msg("Response: " + sb)+ log.Info().Msg("Processing barcode lookup request on: " + q.EAN)- var result BARCODESPIDER_COMResponse- if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer- log.Error().Msg("Can not unmarshal JSON")- }+ var products []repo.BarcodeProduct- // TODO: check 200 code on HTTP response.- var p repo.BarcodeProduct- p.Barcode = iEan- p.SearchEngineName = "barcodespider.com"- p.Item.Name = result.ItemAttributes.Title- p.Item.Description = result.ItemAttributes.Description- p.Manufacturer = result.ItemAttributes.Brand- p.ModelNumber = result.ItemAttributes.Model- p.ImageURL = result.ItemAttributes.Image+ // www.ean-search.org/: not free- var res []repo.BarcodeProduct- res = append(res, p)+ // Example code: dewalt 5035048748428- return res, nil+ ps, err := lookupUPCItemDB(q.EAN)+ if err != nil {+ log.Error().Msg("Can not retrieve product from upcitemdb.com: " + err.Error())}- ps2, err := barcodespider(conf.TokenBarcodespider, q.EAN)+ ps2, err := lookupBarcodespider(conf.TokenBarcodespider, q.EAN)if err != nil {log.Error().Msg("Can not retrieve product from barcodespider.com: " + err.Error())}+ ps3, err := lookupOpenFoodFacts(conf.OpenFoodFactsContact, q.EAN)+ if err != nil {+ log.Error().Msg("Can not retrieve product from openfoodfacts.org: " + err.Error())+ }+// Merge everything.products = append(products, ps...)-products = append(products, ps2...)+ products = append(products, ps3...)// Retrieve images if possiblefor i := range products {@@ -269,62 +391,12 @@ func (ctrl *V1Controller) HandleProductSearchFromBarcode(conf config.BarcodeAPICcontinue}- client := &http.Client{Timeout: TIMEOUT_SEC * time.Second}- res, err := client.Get(p.ImageURL)+ base64Img, err := fetchImageBase64(p.ImageURL)if err != nil {log.Warn().Msg("Cannot fetch image for URL: " + p.ImageURL + ": " + err.Error())- // On error, res may be nil; skip to next product to avoid nil dereferencecontinue}-- // Ensure body is closed for successful responses- defer func() {- // res is non-nil here; merge any close error- err = errors.Join(err, res.Body.Close())- }()-- // Validate response- if res.StatusCode != http.StatusOK {- continue- }-- // Check content type- contentType := res.Header.Get("Content-Type")- if !strings.HasPrefix(contentType, "image/") {- continue- }-- // Limit image size to 8MB- limitedReader := io.LimitReader(res.Body, 8*1024*1024)-- // Read data of image- bytes, err := io.ReadAll(limitedReader)- if err != nil {- log.Warn().Msg(err.Error())- continue- }-- // Convert to Base64- var base64Encoding string-- // Determine the content type of the image file- mimeType := http.DetectContentType(bytes)-- // Prepend the appropriate URI scheme header depending- // on the MIME type- switch mimeType {- case "image/jpeg":- base64Encoding += "data:image/jpeg;base64,"- case "image/png":- base64Encoding += "data:image/png;base64,"- default:- continue- }-- // Append the base64 encoded output- base64Encoding += base64.StdEncoding.EncodeToString(bytes)-- p.ImageBase64 = base64Encoding+ p.ImageBase64 = base64Img}if len(products) != 0 {diff --git a/backend/internal/sys/config/conf.go b/backend/internal/sys/config/conf.goindex afe50623..4750c8d1 100644--- a/backend/internal/sys/config/conf.go+++ b/backend/internal/sys/config/conf.go@@ -101,7 +101,8 @@ type OIDCConf struct {}type BarcodeAPIConf struct {- TokenBarcodespider string `yaml:"token_barcodespider"`+ TokenBarcodespider string `yaml:"token_barcodespider"`+ OpenFoodFactsContact string `yaml:"openfoodfacts_contact"`}type AuthConfig struct {