-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a4965a
commit d4dc63b
Showing
4 changed files
with
77 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
uploads |
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,62 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
const MAX_UPLOAD_SIZE = 5 * 1024 * 1024 // 5MB | ||
|
||
func upload(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPost { | ||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE) | ||
if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil { | ||
http.Error(w, "file too big. must be under 5MB", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
file, fileHeader, err := r.FormFile("file") | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
// make buffer used to detect content type | ||
buff := make([]byte, 512) | ||
if _, err := file.Read(buff); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
filetype := http.DetectContentType(buff) | ||
if filetype != "image/jpeg" && filetype != "image/png" { | ||
http.Error(w, "invalid format. only jpeg and png are accepted", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// seek back to beginning to get whole file | ||
if _, err := file.Seek(0, io.SeekStart); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
dst, err := os.Create(filepath.Join(".", "uploads", fileHeader.Filename)) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
defer dst.Close() | ||
|
||
_, err = io.Copy(dst, file) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
run: | ||
go run ./cmd/uploadr | ||
go run . |
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