Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:clear file serving #256

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,290 changes: 643 additions & 647 deletions server/api/tumdev/campus_backend.pb.go

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions server/api/tumdev/campus_backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,12 @@ message News {
string title = 2;
string text = 3;
string link = 4;
// where a news thumbnail is stored. empty string means no image is available
string image_url = 5;
string source = 6;
// when the news item was created in OUR database
google.protobuf.Timestamp created = 7;
// the date of the news item
google.protobuf.Timestamp date = 8;
}

Expand All @@ -265,7 +268,8 @@ message ListNewsSourcesReply {
message NewsSource {
string source = 1;
string title = 2;
string icon = 3;
// where the icon can be found
string icon_url = 3;
}

message ListNewsAlertsRequest {
Expand Down Expand Up @@ -530,8 +534,8 @@ message ListMoviesReply {
}

message Movie {
string cover_name = 1;
string cover_path = 2;
reserved /*string cover_name = */ 1;
reserved /*string cover_path =*/ 2;
int64 movie_id = 3;
google.protobuf.Timestamp date = 4;
google.protobuf.Timestamp created = 5;
Expand All @@ -546,10 +550,11 @@ message Movie {
string imdb_rating = 12;
string description = 13;
int64 cover_id = 14;
// was previously the trailer
reserved 15;
reserved /*string trailer*/ 15;
// Where to find additional information about this movie
string link = 16;
// Where to find a cover image for this movie
string cover_url = 17;
}

message CreateFeedbackRequest {
Expand Down
24 changes: 13 additions & 11 deletions server/api/tumdev/campus_backend.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1551,12 +1551,6 @@
"apiMovie": {
"type": "object",
"properties": {
"coverName": {
"type": "string"
},
"coverPath": {
"type": "string"
},
"movieId": {
"type": "string",
"format": "int64"
Expand Down Expand Up @@ -1602,6 +1596,10 @@
"link": {
"type": "string",
"title": "Where to find additional information about this movie"
},
"coverUrl": {
"type": "string",
"title": "Where to find a cover image for this movie"
}
}
},
Expand All @@ -1622,18 +1620,21 @@
"type": "string"
},
"imageUrl": {
"type": "string"
"type": "string",
"title": "where a news thumbnail is stored. empty string means no image is available"
},
"source": {
"type": "string"
},
"created": {
"type": "string",
"format": "date-time"
"format": "date-time",
"title": "when the news item was created in OUR database"
},
"date": {
"type": "string",
"format": "date-time"
"format": "date-time",
"title": "the date of the news item"
}
}
},
Expand Down Expand Up @@ -1669,8 +1670,9 @@
"title": {
"type": "string"
},
"icon": {
"type": "string"
"iconUrl": {
"type": "string",
"title": "where the icon can be found"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions server/backend/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"context"
"fmt"

pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"github.com/TUM-Dev/Campus-Backend/server/model"
Expand Down Expand Up @@ -35,8 +36,7 @@ func (s *CampusServer) ListMovies(ctx context.Context, req *pb.ListMoviesRequest
Actors: movie.Actors,
ImdbRating: movie.ImdbRating,
Description: movie.Description,
CoverName: movie.File.Name,
CoverPath: movie.File.Path,
CoverUrl: fmt.Sprintf("https://api.tum.app/files/%s%s", movie.File.Path, movie.File.Name),
CoverId: movie.File.File,
Link: movie.Link,
})
Expand Down
12 changes: 5 additions & 7 deletions server/backend/movie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ var (
Actors: "Tom Cruise, Jeremy Renner, Simon Pegg, Paula Patton",
ImdbRating: "7.4",
Description: "The IMF is shut down when it's implicated in the bombing of the Kremlin, causing Ethan Hunt and his new team to go rogue to clear their organization's name.",
CoverName: "mission_impossible_4.jpg",
CoverPath: "movie/mission_impossible_4.jpg",
CoverUrl: "https://api.tum.app/files/movie/mission_impossible_4.jpg",
CoverId: 1,
Link: "https://www.imdb.com/title/tt1229238/",
}
Expand All @@ -70,8 +69,7 @@ var (
Actors: "Tom Cruise, Jeremy Renner, Simon Pegg, Rebecca Ferguson",
ImdbRating: "7.4",
Description: "Ethan and his team take on their most impossible mission yet when they have to eradicate an international rogue organization as highly skilled as they are and committed to destroying the IMF.",
CoverName: "mission_impossible_5.jpg",
CoverPath: "movie/mission_impossible_5.jpg",
CoverUrl: "https://api.tum.app/files/movie/mission_impossible_5.jpg",
CoverId: 2,
Link: "https://www.imdb.com/title/tt2381249/",
}
Expand All @@ -82,8 +80,8 @@ func (s *MovieSuite) Test_ListMoviesAll() {
s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `kino` LEFT JOIN `files` `File` ON `kino`.`cover` = `File`.`file` WHERE kino > ?").
WithArgs(-1).
WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}).
AddRow(movie2.MovieId, movie2.Date.AsTime(), movie2.Created.AsTime(), movie2.Title, movie2.ReleaseYear, movie2.Runtime, movie2.Genre, movie2.Director, movie2.Actors, movie2.ImdbRating, movie2.Description, nil, movie2.CoverId, movie2.Link, movie2.CoverId, movie2.CoverName, movie2.CoverPath, 1, "", 1).
AddRow(movie1.MovieId, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.ReleaseYear, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, nil, movie1.CoverId, movie1.Link, movie1.CoverId, movie1.CoverName, movie1.CoverPath, 1, "", 1))
AddRow(movie2.MovieId, movie2.Date.AsTime(), movie2.Created.AsTime(), movie2.Title, movie2.ReleaseYear, movie2.Runtime, movie2.Genre, movie2.Director, movie2.Actors, movie2.ImdbRating, movie2.Description, nil, movie2.CoverId, movie2.Link, movie2.CoverId, "mission_impossible_5.jpg", "movie/", 1, "", 1).
AddRow(movie1.MovieId, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.ReleaseYear, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, nil, movie1.CoverId, movie1.Link, movie1.CoverId, "mission_impossible_4.jpg", "movie/", 1, "", 1))
response, err := server.ListMovies(context.Background(), &pb.ListMoviesRequest{LastId: -1})
require.NoError(s.T(), err)
require.Equal(s.T(), &pb.ListMoviesReply{Movies: []*pb.Movie{&movie2, &movie1}}, response)
Expand All @@ -94,7 +92,7 @@ func (s *MovieSuite) Test_ListMoviesOne() {
s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `kino` LEFT JOIN `files` `File` ON `kino`.`cover` = `File`.`file` WHERE kino > ?").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}).
AddRow(movie1.MovieId, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.ReleaseYear, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, nil, movie1.CoverId, movie1.Link, movie1.CoverId, movie1.CoverName, movie1.CoverPath, 1, "", 1))
AddRow(movie1.MovieId, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.ReleaseYear, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, nil, movie1.CoverId, movie1.Link, movie1.CoverId, "mission_impossible_4.jpg", "movie/", 1, "", 1))
response, err := server.ListMovies(context.Background(), &pb.ListMoviesRequest{LastId: 1})
require.NoError(s.T(), err)
require.Equal(s.T(), &pb.ListMoviesReply{Movies: []*pb.Movie{&movie1}}, response)
Expand Down
14 changes: 9 additions & 5 deletions server/backend/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSource
for _, source := range sources {
log.WithField("title", source.Title).Trace("sending news source")
resp = append(resp, &pb.NewsSource{
Source: fmt.Sprintf("%d", source.Source),
Title: source.Title,
Icon: source.File.URL.String,
Source: fmt.Sprintf("%d", source.Source),
Title: source.Title,
IconUrl: fmt.Sprintf("https://api.tum.app/files/%s%s", source.File.Path, source.File.Name),
})
}
return &pb.ListNewsSourcesReply{Sources: resp}, nil
Expand Down Expand Up @@ -63,12 +63,16 @@ func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (*
resp := make([]*pb.News, len(newsEntries))
for i, item := range newsEntries {
log.WithField("title", item.Title).Trace("sending news")
imgUrl := ""
if item.File != nil {
imgUrl = fmt.Sprintf("https://api.tum.app/files/%s%s", item.File.Path, item.File.Name)
}
resp[i] = &pb.News{
Id: item.News,
Title: item.Title,
Text: item.Description,
Link: item.Link,
ImageUrl: item.Image.String,
ImageUrl: imgUrl,
Source: fmt.Sprintf("%d", item.Src),
Created: timestamppb.New(item.Created),
Date: timestamppb.New(item.Date),
Expand Down Expand Up @@ -97,7 +101,7 @@ func (s *CampusServer) ListNewsAlerts(ctx context.Context, req *pb.ListNewsAlert
var alerts []*pb.NewsAlert
for _, alert := range res {
alerts = append(alerts, &pb.NewsAlert{
ImageUrl: alert.File.URL.String,
ImageUrl: fmt.Sprintf("https://api.tum.app/files/%s%s", alert.File.Path, alert.File.Name),
Link: alert.Link.String,
Created: timestamppb.New(alert.Created),
From: timestamppb.New(alert.From),
Expand Down
24 changes: 12 additions & 12 deletions server/backend/news_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func newsFile(id int64) *model.File {
return &model.File{
File: id,
Name: fmt.Sprintf("src_%d.png", id),
Path: "news/sources",
Path: "news/sources/",
Downloads: 1,
URL: null.String{},
Downloaded: null.BoolFrom(true),
Expand All @@ -68,8 +68,8 @@ func source1() *model.NewsSource {
Source: 1,
Title: "Amazing News 1",
URL: null.StringFrom("https://example.com/amazing1"),
FileID: newsFile(2).File,
File: *newsFile(2),
FileID: newsFile(1).File,
File: *newsFile(1),
Hook: null.StringFrom(""),
}
}
Expand Down Expand Up @@ -99,8 +99,8 @@ func (s *NewsSuite) Test_ListNewsSourcesMultiple() {
require.NoError(s.T(), err)
expectedResp := &pb.ListNewsSourcesReply{
Sources: []*pb.NewsSource{
{Source: fmt.Sprintf("%d", source1().Source), Title: source1().Title, Icon: source1().File.URL.String},
{Source: fmt.Sprintf("%d", source2().Source), Title: source2().Title, Icon: source2().File.URL.String},
{Source: fmt.Sprintf("%d", source1().Source), Title: source1().Title, IconUrl: "https://api.tum.app/files/news/sources/src_1.png"},
{Source: fmt.Sprintf("%d", source2().Source), Title: source2().Title, IconUrl: "https://api.tum.app/files/news/sources/src_2.png"},
},
}
require.Equal(s.T(), expectedResp, response)
Expand Down Expand Up @@ -183,8 +183,8 @@ func (s *NewsSuite) Test_ListNewsMultiple() {
require.NoError(s.T(), err)
expectedResp := &pb.ListNewsReply{
News: []*pb.News{
{Id: n1.News, Title: n1.Title, Text: n1.Description, Link: n1.Link, ImageUrl: n1.Image.String, Source: fmt.Sprintf("%d", n1.Src), Created: timestamppb.New(n1.Created), Date: timestamppb.New(n1.Date)},
{Id: n2.News, Title: n2.Title, Text: n2.Description, Link: n2.Link, ImageUrl: n2.Image.String, Source: fmt.Sprintf("%d", n2.Src), Created: timestamppb.New(n2.Created), Date: timestamppb.New(n2.Date)},
{Id: n1.News, Title: n1.Title, Text: n1.Description, Link: n1.Link, ImageUrl: "https://api.tum.app/files/news/sources/src_1.png", Source: fmt.Sprintf("%d", n1.Src), Created: timestamppb.New(n1.Created), Date: timestamppb.New(n1.Date)},
{Id: n2.News, Title: n2.Title, Text: n2.Description, Link: n2.Link, ImageUrl: "", Source: fmt.Sprintf("%d", n2.Src), Created: timestamppb.New(n2.Created), Date: timestamppb.New(n2.Date)},
},
}
require.Equal(s.T(), expectedResp, response)
Expand All @@ -194,7 +194,7 @@ func newsAlertFile(id int64) *model.File {
return &model.File{
File: id,
Name: fmt.Sprintf("src_%d.png", id),
Path: "news/sources",
Path: "news/sources/",
Downloads: 1,
URL: null.String{},
Downloaded: null.BoolFrom(true),
Expand All @@ -216,8 +216,8 @@ func alert1() *model.NewsAlert {
func alert2() *model.NewsAlert {
return &model.NewsAlert{
NewsAlert: 2,
FileID: newsAlertFile(1).File,
File: *newsAlertFile(1),
FileID: newsAlertFile(2).File,
File: *newsAlertFile(2),
Name: null.String{},
Link: null.String{},
Created: time.Time.Add(time.Now(), time.Hour),
Expand Down Expand Up @@ -266,8 +266,8 @@ func (s *NewsSuite) Test_ListNewsAlertsMultiple() {
require.NoError(s.T(), err)
expectedResp := &pb.ListNewsAlertsReply{
Alerts: []*pb.NewsAlert{
{ImageUrl: a1.File.URL.String, Link: a1.Link.String, Created: timestamppb.New(a1.Created), From: timestamppb.New(a1.From), To: timestamppb.New(a1.To)},
{ImageUrl: a2.File.URL.String, Link: a2.Link.String, Created: timestamppb.New(a2.Created), From: timestamppb.New(a2.From), To: timestamppb.New(a2.To)},
{ImageUrl: "https://api.tum.app/files/news/sources/src_1.png", Link: a1.Link.String, Created: timestamppb.New(a1.Created), From: timestamppb.New(a1.From), To: timestamppb.New(a1.To)},
{ImageUrl: "https://api.tum.app/files/news/sources/src_2.png", Link: a2.Link.String, Created: timestamppb.New(a2.Created), From: timestamppb.New(a2.From), To: timestamppb.New(a2.To)},
}}
require.Equal(s.T(), expectedResp, response)
}
Expand Down
6 changes: 3 additions & 3 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"embed"
"encoding/json"
"io/fs"
"net"
"net/http"
"net/textproto"
Expand Down Expand Up @@ -77,8 +76,9 @@ func main() {
})
httpMux.Handle("/metrics", promhttp.Handler())

static, _ := fs.Sub(swagfs, "swagger")
httpMux.Handle("/", http.FileServer(http.FS(static)))
httpMux.Handle("/", http.RedirectHandler("/swagger/", http.StatusTemporaryRedirect))
httpMux.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir("/Storage"))))
httpMux.Handle("/swagger/", http.FileServer(http.FS(swagfs)))

// Main GRPC Server
grpcServer := grpc.NewServer()
Expand Down
Loading