From 05636d7d45b0331c2594cff2a2aab2eb7f3a4924 Mon Sep 17 00:00:00 2001 From: Ajitha Prasad Y <59395177+kelpikz@users.noreply.github.com> Date: Sat, 3 Apr 2021 15:12:45 +0530 Subject: [PATCH] [Feat] : remove downloading image from url, instead save the url to the db (#350) * Feat : remove downloading image from url, and save the url to the database instead * Fix: update marketevent test for the updated model * fix: remove comments * feat: update frontend submodule --- frontend | 2 +- models/MarketEvent.go | 46 ++------------------------------------ models/MarketEvent_test.go | 8 +++---- 3 files changed, 7 insertions(+), 49 deletions(-) diff --git a/frontend b/frontend index 97d1b89f..ea0d20f9 160000 --- a/frontend +++ b/frontend @@ -1 +1 @@ -Subproject commit 97d1b89f05531ec783a20ffa214b0287090a9822 +Subproject commit ea0d20f9e531e6a120c786f5b2dce776e9be2ead diff --git a/models/MarketEvent.go b/models/MarketEvent.go index cab9f1dc..b30bc6bd 100644 --- a/models/MarketEvent.go +++ b/models/MarketEvent.go @@ -1,12 +1,7 @@ package models import ( - "errors" "fmt" - "io" - "net/http" - "os" - "strings" models_pb "github.com/delta/dalal-street-server/proto_build/models" "github.com/delta/dalal-street-server/utils" @@ -88,43 +83,6 @@ func AddMarketEvent(stockId uint32, headline, text string, isGlobal bool, imageU "param_imageURL": imageURL, }) - l.Infof("Attempting") - - // Try downloading image first - response, err := http.Get(imageURL) - if err != nil || response.StatusCode != http.StatusOK { - l.Errorf("Error : %v, StatusCode : %d", err, response.StatusCode) - if err != nil { - return err - } - return errors.New("NOT OK status code") - } - defer response.Body.Close() - - // Extract filename - var basename = imageURL[strings.LastIndex(imageURL, "/")+1:] - var getParamStartIndex = strings.Index(basename, "?") - if getParamStartIndex != -1 { - basename = basename[:getParamStartIndex] - } - l.Debugf("ImageURL : %s Basename : %s", imageURL, basename) - - // open file for saving image - file, err := os.Create(utils.GetImageBasePath() + basename) - - if err != nil { - l.Error(err) - return err - } - defer file.Close() - - // copy image to file - _, err = io.Copy(file, response.Body) - if err != nil { - l.Error(err) - return err - } - db := getDB() me := &MarketEvent{ @@ -132,7 +90,7 @@ func AddMarketEvent(stockId uint32, headline, text string, isGlobal bool, imageU Headline: headline, Text: text, IsGlobal: isGlobal, - ImagePath: basename, + ImagePath: imageURL, CreatedAt: utils.GetCurrentTimeISO8601(), } @@ -143,7 +101,7 @@ func AddMarketEvent(stockId uint32, headline, text string, isGlobal bool, imageU ImageUrl: imageURL, }) - if err = db.Save(me).Error; err != nil { + if err := db.Save(me).Error; err != nil { l.Error(err) return err } diff --git a/models/MarketEvent_test.go b/models/MarketEvent_test.go index 14701080..c615532a 100644 --- a/models/MarketEvent_test.go +++ b/models/MarketEvent_test.go @@ -3,7 +3,7 @@ package models import ( "testing" - "github.com/delta/dalal-street-server/utils/test" + testutils "github.com/delta/dalal-street-server/utils/test" ) func TestMarketEventToProto(t *testing.T) { @@ -14,7 +14,7 @@ func TestMarketEventToProto(t *testing.T) { Text: "Hello World", IsGlobal: true, EmotionScore: -54, - ImagePath: "bitcoin_1516197589.jpg", + ImagePath: "http://www.valuewalk.com/wp-content/uploads/2018/01/bitcoin_1516197589.jpg", CreatedAt: "2017-02-09T00:00:00", } @@ -33,7 +33,7 @@ func Test_GetMarketEvents(t *testing.T) { Text: "Hello World", IsGlobal: true, EmotionScore: -54, - ImagePath: "bitcoin_1516197589.jpg", + ImagePath: "http://www.valuewalk.com/wp-content/uploads/2018/01/bitcoin_1516197589.jpg", CreatedAt: "2017-02-09T00:00:00", } db := getDB() @@ -78,7 +78,7 @@ func Test_AddMarketEvent(t *testing.T) { Headline: "Hello", Text: "Hello World", IsGlobal: true, - ImagePath: "bitcoin_1516197589.jpg", + ImagePath: "http://www.valuewalk.com/wp-content/uploads/2018/01/bitcoin_1516197589.jpg", } db := getDB() defer func() {