-
Notifications
You must be signed in to change notification settings - Fork 1
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
Phillip/test fileupload #55
Open
phiphi282
wants to merge
21
commits into
master
Choose a base branch
from
phillip/test_fileupload
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7172fc1
added basic fileupload
4b69c06
starting file handling
6436164
removed old uploads
873e72e
First working prototype
43d2858
refactor tests
phiphi282 ad75706
added basic fileupload
fcc1e69
starting file handling
4bb0843
First working prototype
5e1baf5
merging in new master
phiphi282 fdd9f16
Started mocking service, tests in service package still needed
phiphi282 b98dab3
Added http tests
phiphi282 c05a890
Added test for entryservice
phiphi282 4c3d1e5
formatting and final testing
phiphi282 9b154ea
added basic fileupload
de28758
starting file handling
3bb7fde
First working prototype
8d06af3
remove unwanted commands
phiphi282 a407051
removed old folder for force push
phiphi282 8e22691
switched to switch
phiphi282 b6f699d
Only allowing images for now
phiphi282 4a9df44
Make changes according to CR (not finished yet)
phiphi282 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mock | ||
|
||
type Uploader struct { | ||
UploadFileFn func(file []byte, course string, filename string) (string, error) | ||
UploadFileFnInvoked bool | ||
} | ||
|
||
func (u *Uploader) UploadFile(file []byte, course string, filename string) (string, error) { | ||
u.UploadFileFnInvoked = true | ||
return u.UploadFileFn(file, course, filename) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,27 @@ package courseEntryService | |
|
||
import ( | ||
"github.com/eduboard/backend" | ||
"github.com/eduboard/backend/upload" | ||
"github.com/pkg/errors" | ||
"gopkg.in/mgo.v2/bson" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func New(repository eduboard.CourseEntryRepository) CourseEntryService { | ||
return CourseEntryService{ | ||
ER: repository, | ||
u: &upload.Uploader{}, | ||
} | ||
} | ||
|
||
type Uploader interface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this Interface used for? Wasn't the file uploaded to this server? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interface is used for mocking the upload function in our tests |
||
UploadFile(file []byte, course string, filename string) (string, error) | ||
} | ||
|
||
type CourseEntryService struct { | ||
ER eduboard.CourseEntryRepository | ||
u Uploader | ||
} | ||
|
||
func (cES CourseEntryService) StoreCourseEntry(entry *eduboard.CourseEntry, cfu eduboard.CourseFindUpdater) (error, *eduboard.CourseEntry) { | ||
|
@@ -39,6 +48,18 @@ func (cES CourseEntryService) StoreCourseEntry(entry *eduboard.CourseEntry, cfu | |
return nil, entry | ||
} | ||
|
||
func (cES CourseEntryService) StoreCourseEntryFiles(files [][]byte, id string, date time.Time) ([]string, error) { | ||
paths := []string{} | ||
for key, file := range files { | ||
url, err := cES.u.UploadFile(file, id, date.String()+"_"+strconv.Itoa(key)) | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
paths = append(paths, url) | ||
} | ||
return paths, nil | ||
} | ||
|
||
func (cES CourseEntryService) UpdateCourseEntry(*eduboard.CourseEntry) (*eduboard.CourseEntry, error) { | ||
return &eduboard.CourseEntry{}, nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package upload | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type Uploader struct{} | ||
|
||
func (u *Uploader) UploadFile(file []byte, course string, filename string) (string, error) { | ||
dir := string("./files/" + course + "/") | ||
path := string(dir + filename) | ||
serverFile := string("/files/" + course + "/" + filename) | ||
|
||
contentType := http.DetectContentType(file) | ||
|
||
if strings.Split(contentType, "/")[0] != "image" { | ||
return "", errors.New("filetype not supported") | ||
} | ||
|
||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
os.MkdirAll(dir, os.ModePerm) | ||
} | ||
|
||
err := ioutil.WriteFile(path, file, 0644) | ||
if err != nil { | ||
panic(err) | ||
return "", err | ||
} | ||
|
||
return serverFile, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be
Chain(http.FileServer(http.Dir(a.Files))), http.StripPrefix("/files")...
? The first argument ofChain
is the final handler.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http.StripPrefix takes in a string that will be removed from the beginning from the handlers path
So it needs to take in both arguments