-
Notifications
You must be signed in to change notification settings - Fork 0
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
373dd8e
commit 1199097
Showing
18 changed files
with
292 additions
and
66 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
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
package media | ||
|
||
type Storage interface { | ||
// Store | ||
// Stores the image in the storage and returns the URL | ||
Store(path string, data []byte) error | ||
|
||
Delete(path string) error | ||
|
||
GetHost() string | ||
GetUrl(path string) string | ||
} |
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 |
---|---|---|
@@ -1,16 +1,17 @@ | ||
CREATE TABLE TmpImage( | ||
imgId uuid PRIMARY KEY, | ||
url varchar(100) NOT NULL , | ||
path varchar(100) NOT NULL , | ||
expected_usage int8 NOT NULL , | ||
uploader uuid references user_identity(user_id) NOT NULL, | ||
uploaded_at timestamp NOT NULL | ||
); | ||
|
||
CREATE TABLE ConfirmedImage( | ||
imgId uuid PRIMARY KEY, | ||
url varchar(100) NOT NULL , | ||
path varchar(100) NOT NULL , | ||
usage int8 NOT NULL , | ||
uploader uuid references user_identity(user_id) NOT NULL, | ||
uploaded_at timestamp NOT NULL, | ||
confirmed_at timestamp NOT NULL | ||
) | ||
) | ||
|
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,9 @@ | ||
# MediaService | ||
|
||
This service has two main functions | ||
|
||
1. User can upload temporary images, temp images will be cleaned up after some duration. | ||
2. Other services confirm the usage of the temporary images and make them temporary. | ||
|
||
Upload images with port 8080 using multipart form file with key "image" | ||
Confirm usages with port 8081 using gprc. |
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,7 +1,111 @@ | ||
package media | ||
|
||
import "testing" | ||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"mime/multipart" | ||
"monify/lib/utils" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestW(t *testing.T) { | ||
type TestServerState struct { | ||
server Server | ||
infra Infra | ||
started bool | ||
mutex sync.Mutex | ||
} | ||
|
||
var state TestServerState | ||
|
||
func SetupTestServer() { | ||
state.mutex.Lock() | ||
if state.started { | ||
state.mutex.Unlock() | ||
return | ||
} | ||
|
||
secrets, err := utils.LoadSecrets(utils.LoadEnv()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
infra, err := Setup(NewConfig("dev", secrets)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
state.infra = infra | ||
state.server = NewServer(infra) | ||
state.started = true | ||
state.mutex.Unlock() | ||
} | ||
|
||
func getTestFilePath() string { | ||
abs, err := filepath.Abs("test.png") | ||
if err != nil { | ||
panic(err) | ||
} | ||
return abs | ||
} | ||
|
||
func TestS3Storage(t *testing.T) { | ||
SetupTestServer() | ||
fp := getTestFilePath() | ||
file, err := os.ReadFile(fp) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = state.infra.objStorage.Delete("test.png") | ||
assert.NoError(t, err) | ||
err = state.infra.objStorage.Store("test.png", file) | ||
assert.NoError(t, err) | ||
|
||
response, err := http.Get(state.infra.objStorage.GetUrl("test.png")) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, response.StatusCode) | ||
|
||
err = state.infra.objStorage.Delete("test.png") | ||
assert.NoError(t, err) | ||
|
||
response, err = http.Get(state.infra.objStorage.GetUrl("test.png")) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusForbidden, response.StatusCode) | ||
} | ||
|
||
func TestUpload(t *testing.T) { | ||
SetupTestServer() | ||
fp := getTestFilePath() | ||
file, err := os.Open(fp) | ||
assert.NoError(t, err) | ||
defer file.Close() | ||
|
||
var requestBody bytes.Buffer | ||
writer := multipart.NewWriter(&requestBody) | ||
// Add the file to the form | ||
part, err := writer.CreateFormFile("image", filepath.Base("test.png")) | ||
assert.NoError(t, err) | ||
_, err = io.Copy(part, file) | ||
assert.NoError(t, err) | ||
err = writer.Close() | ||
assert.NoError(t, err) | ||
req, err := http.NewRequest("POST", "/image", &requestBody) | ||
assert.NoError(t, err) | ||
req.Header.Set("Content-Type", writer.FormDataContentType()) | ||
|
||
// Create a response recorder | ||
response := httptest.NewRecorder() | ||
state.server.mux.ServeHTTP(response, req) | ||
assert.Equal(t, http.StatusOK, response.Code) | ||
|
||
resBody := UploadImageResponse{} | ||
err = json.Unmarshal(response.Body.Bytes(), &resBody) | ||
assert.NoError(t, err) | ||
println(resBody.Url) | ||
println(resBody.ImageId) | ||
} |
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
Oops, something went wrong.