Skip to content

Commit

Permalink
[Feat] : remove downloading image from url, instead save the url to t…
Browse files Browse the repository at this point in the history
…he 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
  • Loading branch information
kelpikz authored Apr 3, 2021
1 parent 5e12d96 commit 05636d7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 49 deletions.
2 changes: 1 addition & 1 deletion frontend
46 changes: 2 additions & 44 deletions models/MarketEvent.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -88,51 +83,14 @@ 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{
StockId: stockId,
Headline: headline,
Text: text,
IsGlobal: isGlobal,
ImagePath: basename,
ImagePath: imageURL,
CreatedAt: utils.GetCurrentTimeISO8601(),
}

Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions models/MarketEvent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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",
}

Expand All @@ -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()
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 05636d7

Please sign in to comment.