-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1c9d0d4
commit 395b771
Showing
8 changed files
with
255 additions
and
29 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 |
---|---|---|
|
@@ -13,3 +13,6 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
database/ | ||
*.db |
Binary file not shown.
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,85 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/znip-in/tiddi/src/db" | ||
) | ||
|
||
type GetImageRequest struct { | ||
Uiid string `json:"uiid"` | ||
} | ||
|
||
type GetImageResponse struct { | ||
Title string `json:"title"` | ||
Image []byte `json:"data"` | ||
} | ||
|
||
/* | ||
GetImage is the endpoint to get the image details | ||
Endpoint: POST https://your-domain.com/get-image/ | ||
@description | ||
Request.Body = { | ||
"uiid": "uiid of the image" | ||
} | ||
`uiid` -> Unique Image Id | ||
@author Kunal Singh <[email protected]> | ||
@type Api Handler Function | ||
*/ | ||
func GetImage(w http.ResponseWriter, r *http.Request) { | ||
EnableCors(&w) | ||
data, err := io.ReadAll(r.Body) | ||
|
||
if err != nil || len(data) == 0 { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte("Bad Request")) | ||
return | ||
} | ||
|
||
var request GetImageRequest | ||
if err := json.Unmarshal(data, &request); err != nil { | ||
log.Printf("[GET-IMAGE] unable to unmarshal data received: %v\n", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte("Internal Server Error")) | ||
return | ||
} | ||
|
||
if request.Uiid == "" { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte("Bad Request")) | ||
return | ||
} | ||
|
||
title, image, err := db.GetImageDetails(request.Uiid) | ||
|
||
if err != nil { | ||
log.Printf("[GET-IMAGE] Unable to get image details: %v\n", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte("Internal Server Error")) | ||
return | ||
} | ||
|
||
response := GetImageResponse{ | ||
Title: title, | ||
Image: image, | ||
} | ||
|
||
resBytes, err := json.Marshal(response) | ||
|
||
if err != nil { | ||
log.Printf("[GET-IMAGE] Unable to marshal response: %v\n", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte("Internal Server Error")) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write(resBytes) | ||
} |
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,106 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Upload Image</title> | ||
<style> | ||
body { | ||
padding: 0; | ||
margin: 0; | ||
background-color: antiquewhite; | ||
font-family: Verdana, Geneva, Tahoma, sans-serif; | ||
} | ||
.form-container { | ||
position: fixed; | ||
top: 50%; | ||
left: 50%; | ||
transform: translate(-50%, -50%); | ||
width: 70%; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
.link { | ||
width: 100%; | ||
max-width: 600px; | ||
} | ||
a { | ||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; | ||
letter-spacing: 1px; | ||
} | ||
|
||
</style> | ||
</head> | ||
<body> | ||
<div class="form-container"> | ||
<form id="form"> | ||
<input type="file" id="file" accept="image/*" name="filename "> | ||
<input type="submit" id="submit" class="btn"> | ||
</form> | ||
<div class="link"> | ||
</div> | ||
</div> | ||
<script> | ||
|
||
const submit = document.getElementById("submit") | ||
const file = document.getElementById("file") | ||
const linkBox = document.querySelector('.link') | ||
|
||
function showBox(color, colorLight, inner){ | ||
linkBox.style.padding = '5px' | ||
linkBox.style.marginTop = '15px' | ||
linkBox.style.borderRadius = '10px' | ||
linkBox.style.border = `1px solid ${color}` | ||
linkBox.style.backgroundColor = colorLight | ||
linkBox.innerHTML = inner | ||
} | ||
|
||
|
||
async function postData(data) { | ||
// Use Go template to provide url | ||
const resp = await fetch("http://localhost:5656/upload-image/", { | ||
method: "POST", | ||
mode: "cors", | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(data) | ||
}) | ||
return resp.json() | ||
} | ||
|
||
submit.addEventListener('click', (e) => { | ||
e.preventDefault() | ||
|
||
showBox("black", "#d3d3d3", "<p>Getting URL...</p>") | ||
|
||
const reader = new FileReader() | ||
|
||
reader.readAsArrayBuffer(file.files[0]) | ||
|
||
reader.onload = function() { | ||
const title = file.files[0].name | ||
const uint8array = new Uint8Array(reader.result); | ||
const bytes = Array.from(uint8array) | ||
|
||
postData({ | ||
title, | ||
data: bytes | ||
}).then((data) => { | ||
showBox("green", "#DAF7A6", `<p><strong>URL</strong>: <a href="${data.url}" target=_black>${data.url}</a>`) | ||
}).catch(() => { | ||
showBox("red", "#FFCCCB", "<p>Something went wrong, try again!</p>") | ||
}) | ||
}; | ||
|
||
reader.onerror = function() { | ||
showBox("red", "#FFCCCB", "<p>Something went wrong, try again!</p>") | ||
}; | ||
}) | ||
|
||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.