From 24e33923446ea99cb42573b1ab5ed8c4d484fba2 Mon Sep 17 00:00:00 2001 From: Hasan Date: Sat, 11 Jan 2025 20:57:44 +0500 Subject: [PATCH 01/12] set up --- .env.example | 12 ------------ go.mod | 2 +- handler_login.go | 4 ++-- handler_refresh.go | 2 +- handler_upload_thumbnail.go | 2 +- handler_users.go | 4 ++-- handler_video_meta.go | 4 ++-- main.go | 2 +- 8 files changed, 10 insertions(+), 22 deletions(-) delete mode 100644 .env.example diff --git a/.env.example b/.env.example deleted file mode 100644 index fba2c3ea..00000000 --- a/.env.example +++ /dev/null @@ -1,12 +0,0 @@ -DB_PATH="./tubely.db" -JWT_SECRET="JKFNDKAJSDKFASFNJWIROIOTNKNFDSKNFD" -PLATFORM="dev" -FILEPATH_ROOT="./app" -ASSETS_ROOT="./assets" -S3_BUCKET="tubely-123456789" -S3_REGION="us-east-2" -S3_CF_DISTRO="TEST" -PORT="8091" -# aws credentials should be set in ~/.aws/credentials -# using the `aws configure` command, the SDK will automatically -# read them from there diff --git a/go.mod b/go.mod index f8ae5007..af3d58d6 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/bootdotdev/learn-file-storage-s3-golang-starter +module github.com/imhasandl/learn-file-storage-s3-golang-starter go 1.23.0 diff --git a/handler_login.go b/handler_login.go index 9ccf9f74..92d858c6 100644 --- a/handler_login.go +++ b/handler_login.go @@ -5,8 +5,8 @@ import ( "net/http" "time" - "github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/auth" - "github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/database" ) func (cfg *apiConfig) handlerLogin(w http.ResponseWriter, r *http.Request) { diff --git a/handler_refresh.go b/handler_refresh.go index 029f22ce..eefc36ea 100644 --- a/handler_refresh.go +++ b/handler_refresh.go @@ -4,7 +4,7 @@ import ( "net/http" "time" - "github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/auth" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" ) func (cfg *apiConfig) handlerRefresh(w http.ResponseWriter, r *http.Request) { diff --git a/handler_upload_thumbnail.go b/handler_upload_thumbnail.go index 765d87d3..00d677da 100644 --- a/handler_upload_thumbnail.go +++ b/handler_upload_thumbnail.go @@ -4,7 +4,7 @@ import ( "fmt" "net/http" - "github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/auth" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" "github.com/google/uuid" ) diff --git a/handler_users.go b/handler_users.go index d25e6327..e5604f4e 100644 --- a/handler_users.go +++ b/handler_users.go @@ -4,8 +4,8 @@ import ( "encoding/json" "net/http" - "github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/auth" - "github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/database" ) func (cfg *apiConfig) handlerUsersCreate(w http.ResponseWriter, r *http.Request) { diff --git a/handler_video_meta.go b/handler_video_meta.go index 32660382..56fee163 100644 --- a/handler_video_meta.go +++ b/handler_video_meta.go @@ -4,8 +4,8 @@ import ( "encoding/json" "net/http" - "github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/auth" - "github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/database" "github.com/google/uuid" ) diff --git a/main.go b/main.go index a48f1405..409d8ea3 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ import ( "net/http" "os" - "github.com/bootdotdev/learn-file-storage-s3-golang-starter/internal/database" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/database" "github.com/google/uuid" "github.com/joho/godotenv" From d8c8b0973fffe1f10c46f606e6b1d29f20dbf1dc Mon Sep 17 00:00:00 2001 From: Hasan Date: Sun, 12 Jan 2025 21:45:16 +0500 Subject: [PATCH 02/12] security check --- internal/database/users.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/database/users.go b/internal/database/users.go index 4f162240..9377a16d 100644 --- a/internal/database/users.go +++ b/internal/database/users.go @@ -17,7 +17,7 @@ type User struct { type CreateUserParams struct { Email string `json:"email"` - Password string `json:"password"` + Password string `json:"-"` } func (c Client) GetUsers() ([]User, error) { From 37cb320f9403798937f190c609c602f470aa0fbb Mon Sep 17 00:00:00 2001 From: Hasan Date: Sun, 12 Jan 2025 22:27:35 +0500 Subject: [PATCH 03/12] upload video method --- handler_upload_thumbnail.go | 53 +++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/handler_upload_thumbnail.go b/handler_upload_thumbnail.go index 00d677da..9b07541b 100644 --- a/handler_upload_thumbnail.go +++ b/handler_upload_thumbnail.go @@ -2,10 +2,11 @@ package main import ( "fmt" + "io" "net/http" - "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" "github.com/google/uuid" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" ) func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Request) { @@ -33,5 +34,53 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ // TODO: implement the upload here - respondWithJSON(w, http.StatusOK, struct{}{}) + const maxMemory = 10 << 20 + r.ParseMultipartForm(maxMemory) + + file, header, err := r.FormFile("thumbnail") + if err != nil { + respondWithError(w, http.StatusBadRequest, "Unable to parse form file", err) + return + } + defer file.Close() + + mediaType := header.Header.Get("Content-Type") + if mediaType == "" { + respondWithError(w, http.StatusBadRequest, "Missing Content-Type for thumbnail", nil) + return + } + + data, err := io.ReadAll(file) + if err != nil { + respondWithError(w, http.StatusBadRequest, "can't read file", err) + return + } + + video, err := cfg.db.GetVideo(videoID) + if err != nil { + respondWithError(w, http.StatusUnauthorized, "can't get video using id", err) + return + } + + videoThumbnails[videoID] = thumbnail{ + data: data, + mediaType: mediaType, + } + + if video.UserID != userID { + respondWithError(w, http.StatusUnauthorized, "Not authorized to update this video", nil) + return + } + + + url := fmt.Sprintf("http://localhost:%s/api/thumbnails/%s", cfg.port, videoID) + video.ThumbnailURL = &url + + err = cfg.db.UpdateVideo(video) + if err != nil { + respondWithError(w, http.StatusBadRequest, "can't update video", err) + return + } + + respondWithJSON(w, http.StatusOK, video) } From 4de03baaa1549581f418139a5e45f2e92e9ee62a Mon Sep 17 00:00:00 2001 From: Hasan Date: Mon, 13 Jan 2025 14:16:37 +0500 Subject: [PATCH 04/12] get rid of storing tmubnails in cache --- handler_get_thumbnail.go | 32 -------------------------------- handler_upload_thumbnail.go | 12 ++++++++---- main.go | 8 -------- 3 files changed, 8 insertions(+), 44 deletions(-) delete mode 100644 handler_get_thumbnail.go diff --git a/handler_get_thumbnail.go b/handler_get_thumbnail.go deleted file mode 100644 index 1ddac141..00000000 --- a/handler_get_thumbnail.go +++ /dev/null @@ -1,32 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - - "github.com/google/uuid" -) - -func (cfg *apiConfig) handlerThumbnailGet(w http.ResponseWriter, r *http.Request) { - videoIDString := r.PathValue("videoID") - videoID, err := uuid.Parse(videoIDString) - if err != nil { - respondWithError(w, http.StatusBadRequest, "Invalid video ID", err) - return - } - - tn, ok := videoThumbnails[videoID] - if !ok { - respondWithError(w, http.StatusNotFound, "Thumbnail not found", nil) - return - } - - w.Header().Set("Content-Type", tn.mediaType) - w.Header().Set("Content-Length", fmt.Sprintf("%d", len(tn.data))) - - _, err = w.Write(tn.data) - if err != nil { - respondWithError(w, http.StatusInternalServerError, "Error writing response", err) - return - } -} diff --git a/handler_upload_thumbnail.go b/handler_upload_thumbnail.go index 9b07541b..7b2c6a6f 100644 --- a/handler_upload_thumbnail.go +++ b/handler_upload_thumbnail.go @@ -1,6 +1,7 @@ package main import ( + "encoding/base64" "fmt" "io" "net/http" @@ -62,17 +63,20 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ return } - videoThumbnails[videoID] = thumbnail{ - data: data, - mediaType: mediaType, + base := base64.StdEncoding.EncodeToString(data) + if mediaType == "" { + respondWithError(w, http.StatusBadRequest, "can't encode to string", nil) + return } + + url := fmt.Sprintf("data:%s;base64,%s", mediaType, base) + if video.UserID != userID { respondWithError(w, http.StatusUnauthorized, "Not authorized to update this video", nil) return } - url := fmt.Sprintf("http://localhost:%s/api/thumbnails/%s", cfg.port, videoID) video.ThumbnailURL = &url diff --git a/main.go b/main.go index 409d8ea3..dee1a8ba 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "os" "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/database" - "github.com/google/uuid" "github.com/joho/godotenv" _ "github.com/lib/pq" @@ -24,13 +23,6 @@ type apiConfig struct { port string } -type thumbnail struct { - data []byte - mediaType string -} - -var videoThumbnails = map[uuid.UUID]thumbnail{} - func main() { godotenv.Load(".env") From 522630767a3ae958aa58f8001369351b624a5abd Mon Sep 17 00:00:00 2001 From: Hasan Date: Mon, 13 Jan 2025 14:25:08 +0500 Subject: [PATCH 05/12] video url --- handler_upload_thumbnail.go | 13 +++---------- main.go | 1 - 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/handler_upload_thumbnail.go b/handler_upload_thumbnail.go index 7b2c6a6f..27c4b2fd 100644 --- a/handler_upload_thumbnail.go +++ b/handler_upload_thumbnail.go @@ -63,23 +63,16 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ return } - base := base64.StdEncoding.EncodeToString(data) - if mediaType == "" { - respondWithError(w, http.StatusBadRequest, "can't encode to string", nil) - return - } + base64Encoded := base64.StdEncoding.EncodeToString(data) + base64DataURL := fmt.Sprintf("data:%s;base64,%s", mediaType, base64Encoded) - url := fmt.Sprintf("data:%s;base64,%s", mediaType, base) - + video.ThumbnailURL = &base64DataURL if video.UserID != userID { respondWithError(w, http.StatusUnauthorized, "Not authorized to update this video", nil) return } - url := fmt.Sprintf("http://localhost:%s/api/thumbnails/%s", cfg.port, videoID) - video.ThumbnailURL = &url - err = cfg.db.UpdateVideo(video) if err != nil { respondWithError(w, http.StatusBadRequest, "can't update video", err) diff --git a/main.go b/main.go index dee1a8ba..5a2d5f49 100644 --- a/main.go +++ b/main.go @@ -111,7 +111,6 @@ func main() { mux.HandleFunc("POST /api/video_upload/{videoID}", cfg.handlerUploadVideo) mux.HandleFunc("GET /api/videos", cfg.handlerVideosRetrieve) mux.HandleFunc("GET /api/videos/{videoID}", cfg.handlerVideoGet) - mux.HandleFunc("GET /api/thumbnails/{videoID}", cfg.handlerThumbnailGet) mux.HandleFunc("DELETE /api/videos/{videoID}", cfg.handlerVideoMetaDelete) mux.HandleFunc("POST /admin/reset", cfg.handlerReset) From 9027f143d60719de80b7259bad7ff07d7a40ddbd Mon Sep 17 00:00:00 2001 From: Hasan Date: Mon, 13 Jan 2025 14:27:27 +0500 Subject: [PATCH 06/12] readme deletion --- README.md | 65 ------------------------------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 34d955bc..00000000 --- a/README.md +++ /dev/null @@ -1,65 +0,0 @@ -# learn-file-storage-s3-golang-starter (Tubely) - -This repo contains the starter code for the Tubely application - the #1 tool for engagement bait - for the "Learn File Servers and CDNs with S3 and CloudFront" [course](https://www.boot.dev/courses/learn-file-servers-s3-cloudfront-golang) on [boot.dev](https://www.boot.dev) - -## Quickstart - -*This is to be used as a *reference\* in case you need it, you should follow the instructions in the course rather than trying to do everything here. - -## 1. Install dependencies - -- [Go](https://golang.org/doc/install) -- `go mod download` to download all dependencies -- [FFMPEG](https://ffmpeg.org/download.html) - both `ffmpeg` and `ffprobe` are required to be in your `PATH`. - -```bash -# linux -sudo apt update -sudo apt install ffmpeg - -# mac -brew update -brew install ffmpeg -``` - -- [SQLite 3](https://www.sqlite.org/download.html) only required for you to manually inspect the database. - -```bash -# linux -sudo apt update -sudo apt install sqlite3 - -# mac -brew update -brew install sqlite3 -``` - -- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) - -## 2. Download sample images and videos - -```bash -./samplesdownload.sh -# samples/ dir will be created -# with sample images and videos -``` - -## 3. Configure environment variables - -Copy the `.env.example` file to `.env` and fill in the values. - -```bash -cp .env.example .env -``` - -You'll need to update values in the `.env` file to match your configuration, but _you won't need to do anything here until the course tells you to_. - -## 3. Run the server - -```bash -go run . -``` - -- You should see a new database file `tubely.db` created in the root directory. -- You should see a new `assets` directory created in the root directory, this is where the images will be stored. -- You should see a link in your console to open the local web page. From 383cd889ae9c528bd7755e05d71321643c5832bb Mon Sep 17 00:00:00 2001 From: Hasan Date: Mon, 13 Jan 2025 14:43:31 +0500 Subject: [PATCH 07/12] assets additinal functions --- assets.go | 26 ++++++++++++++++++++++++++ handler_upload_thumbnail.go | 5 ++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/assets.go b/assets.go index 8315787d..6806a53a 100644 --- a/assets.go +++ b/assets.go @@ -1,7 +1,12 @@ package main import ( + "fmt" "os" + "path/filepath" + "strings" + + "github.com/google/uuid" ) func (cfg apiConfig) ensureAssetsDir() error { @@ -10,3 +15,24 @@ func (cfg apiConfig) ensureAssetsDir() error { } return nil } + +func getAssetPath(videoID uuid.UUID, mediaType string) string { + ext := mediaTypeToExt(mediaType) + return fmt.Sprintf("%s%s", videoID, ext) +} + +func (cfg apiConfig) getAssetDiskPath(assetPath string) string { + return filepath.Join(cfg.assetsRoot, assetPath) +} + +func (cfg apiConfig) getAssetURL(assetPath string) string { + return fmt.Sprintf("http://localhost:%s/assets/%s", cfg.port, assetPath) +} + +func mediaTypeToExt(mediaType string) string { + parts := strings.Split(mediaType, "/") + if len(parts) != 2 { + return ".bin" + } + return "." + parts[1] +} diff --git a/handler_upload_thumbnail.go b/handler_upload_thumbnail.go index 27c4b2fd..07dc5e4c 100644 --- a/handler_upload_thumbnail.go +++ b/handler_upload_thumbnail.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "path/filepath" "github.com/google/uuid" "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" @@ -63,10 +64,8 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ return } - base64Encoded := base64.StdEncoding.EncodeToString(data) - base64DataURL := fmt.Sprintf("data:%s;base64,%s", mediaType, base64Encoded) + DataURL := filepath.Join("assets", "%d", "%s", , ) - video.ThumbnailURL = &base64DataURL if video.UserID != userID { respondWithError(w, http.StatusUnauthorized, "Not authorized to update this video", nil) From c369dd594b72327f89cee6e0b381860a86f9f125 Mon Sep 17 00:00:00 2001 From: Hasan Date: Mon, 13 Jan 2025 15:04:05 +0500 Subject: [PATCH 08/12] fix --- handler_upload_thumbnail.go | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/handler_upload_thumbnail.go b/handler_upload_thumbnail.go index 07dc5e4c..6d8c6a03 100644 --- a/handler_upload_thumbnail.go +++ b/handler_upload_thumbnail.go @@ -1,14 +1,12 @@ package main import ( - "encoding/base64" - "fmt" "io" "net/http" - "path/filepath" + "os" - "github.com/google/uuid" "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" + "github.com/google/uuid" ) func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Request) { @@ -31,12 +29,7 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ return } - - fmt.Println("uploading thumbnail for video", videoID, "by user", userID) - - // TODO: implement the upload here - - const maxMemory = 10 << 20 + const maxMemory = 10 << 20 // 10 MB r.ParseMultipartForm(maxMemory) file, header, err := r.FormFile("thumbnail") @@ -52,30 +45,36 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ return } - data, err := io.ReadAll(file) + assetPath := getAssetPath(videoID, mediaType) + assetDiskPath := cfg.getAssetDiskPath(assetPath) + + dst, err := os.Create(assetDiskPath) if err != nil { - respondWithError(w, http.StatusBadRequest, "can't read file", err) + respondWithError(w, http.StatusInternalServerError, "Unable to create file on server", err) + return + } + defer dst.Close() + if _, err = io.Copy(dst, file); err != nil { + respondWithError(w, http.StatusInternalServerError, "Error saving file", err) return } video, err := cfg.db.GetVideo(videoID) if err != nil { - respondWithError(w, http.StatusUnauthorized, "can't get video using id", err) + respondWithError(w, http.StatusInternalServerError, "Couldn't find video", err) return } - - DataURL := filepath.Join("assets", "%d", "%s", , ) - - if video.UserID != userID { respondWithError(w, http.StatusUnauthorized, "Not authorized to update this video", nil) return } + url := cfg.getAssetURL(assetPath) + video.ThumbnailURL = &url err = cfg.db.UpdateVideo(video) if err != nil { - respondWithError(w, http.StatusBadRequest, "can't update video", err) - return + respondWithError(w, http.StatusInternalServerError, "Couldn't update video", err) + return } respondWithJSON(w, http.StatusOK, video) From 932374a1490c0b1efe15639b5af59ebf2d3f34b7 Mon Sep 17 00:00:00 2001 From: Hasan Date: Mon, 13 Jan 2025 15:34:25 +0500 Subject: [PATCH 09/12] check the image type before uploading it --- handler_upload_thumbnail.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/handler_upload_thumbnail.go b/handler_upload_thumbnail.go index 6d8c6a03..86cb76f9 100644 --- a/handler_upload_thumbnail.go +++ b/handler_upload_thumbnail.go @@ -2,11 +2,12 @@ package main import ( "io" + "mime" "net/http" "os" - "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" "github.com/google/uuid" + "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" ) func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Request) { @@ -39,9 +40,13 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ } defer file.Close() - mediaType := header.Header.Get("Content-Type") - if mediaType == "" { - respondWithError(w, http.StatusBadRequest, "Missing Content-Type for thumbnail", nil) + mediaType, _, err := mime.ParseMediaType(header.Header.Get("Content-Type")) + if err != nil { + respondWithError(w, http.StatusBadRequest, "invalid content-type", err) + return + } + if mediaType != "image/jpeg" && mediaType != "images/png" { + respondWithError(w, http.StatusBadRequest, "invalid file type", err) return } From 32e88cb32d7d3352d6e44591cee5fe0ee3f713c9 Mon Sep 17 00:00:00 2001 From: Hasan Date: Wed, 15 Jan 2025 14:03:32 +0500 Subject: [PATCH 10/12] front end change --- app/app.js | 2 +- handler_upload_thumbnail.go | 8 ++++---- main.go | 1 - 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/app.js b/app/app.js index f688af96..50cf6f85 100644 --- a/app/app.js +++ b/app/app.js @@ -261,7 +261,7 @@ function viewVideo(video) { thumbnailImg.style.display = 'none'; } else { thumbnailImg.style.display = 'block'; - thumbnailImg.src = video.thumbnail_url; + thumbnailImg.src = `${video.thumbnail_url}?v=${Date.now()}`; } const videoPlayer = document.getElementById('video-player'); diff --git a/handler_upload_thumbnail.go b/handler_upload_thumbnail.go index 86cb76f9..906ffde6 100644 --- a/handler_upload_thumbnail.go +++ b/handler_upload_thumbnail.go @@ -6,8 +6,8 @@ import ( "net/http" "os" - "github.com/google/uuid" "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/auth" + "github.com/google/uuid" ) func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Request) { @@ -42,11 +42,11 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ mediaType, _, err := mime.ParseMediaType(header.Header.Get("Content-Type")) if err != nil { - respondWithError(w, http.StatusBadRequest, "invalid content-type", err) + respondWithError(w, http.StatusBadRequest, "Invalid Content-Type", err) return } - if mediaType != "image/jpeg" && mediaType != "images/png" { - respondWithError(w, http.StatusBadRequest, "invalid file type", err) + if mediaType != "image/jpeg" && mediaType != "image/png" { + respondWithError(w, http.StatusBadRequest, "Invalid file type", nil) return } diff --git a/main.go b/main.go index 5a2d5f49..70ae5ae4 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "os" "github.com/imhasandl/learn-file-storage-s3-golang-starter/internal/database" - "github.com/joho/godotenv" _ "github.com/lib/pq" ) From fded9a8a7bdc2b6c06a411e2063967bac38cbac6 Mon Sep 17 00:00:00 2001 From: Hasan Date: Wed, 15 Jan 2025 14:52:11 +0500 Subject: [PATCH 11/12] no cache functinality --- cache.go | 4 ++-- main.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cache.go b/cache.go index 84ac0afb..6cdfaf48 100644 --- a/cache.go +++ b/cache.go @@ -2,9 +2,9 @@ package main import "net/http" -func cacheMiddleware(next http.Handler) http.Handler { +func noCacheMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Cache-Control", "max-age=3600") + w.Header().Set("Cache-Control", "no-store") next.ServeHTTP(w, r) }) } diff --git a/main.go b/main.go index 70ae5ae4..65a41cc9 100644 --- a/main.go +++ b/main.go @@ -97,7 +97,7 @@ func main() { mux.Handle("/app/", appHandler) assetsHandler := http.StripPrefix("/assets", http.FileServer(http.Dir(assetsRoot))) - mux.Handle("/assets/", cacheMiddleware(assetsHandler)) + mux.Handle("/assets/", noCacheMiddleware(assetsHandler)) mux.HandleFunc("POST /api/login", cfg.handlerLogin) mux.HandleFunc("POST /api/refresh", cfg.handlerRefresh) From 38a7cac3bb30affafa4b0326bdab24238f8d6940 Mon Sep 17 00:00:00 2001 From: Hasan Date: Wed, 15 Jan 2025 15:29:54 +0500 Subject: [PATCH 12/12] delete video id and use random id --- assets.go | 14 +++++++++++--- handler_upload_thumbnail.go | 2 +- main.go | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/assets.go b/assets.go index 6806a53a..c7de63fb 100644 --- a/assets.go +++ b/assets.go @@ -1,12 +1,12 @@ package main import ( + "crypto/rand" + "encoding/base64" "fmt" "os" "path/filepath" "strings" - - "github.com/google/uuid" ) func (cfg apiConfig) ensureAssetsDir() error { @@ -16,7 +16,15 @@ func (cfg apiConfig) ensureAssetsDir() error { return nil } -func getAssetPath(videoID uuid.UUID, mediaType string) string { +func getAssetPath(mediaType string) string { + base := make([]byte, 32) + _, err := rand.Read(base) + if err != nil { + panic("failed to generate video id") + } + + videoID := base64.RawURLEncoding.EncodeToString(base) + ext := mediaTypeToExt(mediaType) return fmt.Sprintf("%s%s", videoID, ext) } diff --git a/handler_upload_thumbnail.go b/handler_upload_thumbnail.go index 906ffde6..4b0b6e21 100644 --- a/handler_upload_thumbnail.go +++ b/handler_upload_thumbnail.go @@ -50,7 +50,7 @@ func (cfg *apiConfig) handlerUploadThumbnail(w http.ResponseWriter, r *http.Requ return } - assetPath := getAssetPath(videoID, mediaType) + assetPath := getAssetPath(mediaType) assetDiskPath := cfg.getAssetDiskPath(assetPath) dst, err := os.Create(assetDiskPath) diff --git a/main.go b/main.go index 65a41cc9..a03a0c9c 100644 --- a/main.go +++ b/main.go @@ -101,7 +101,7 @@ func main() { mux.HandleFunc("POST /api/login", cfg.handlerLogin) mux.HandleFunc("POST /api/refresh", cfg.handlerRefresh) - mux.HandleFunc("POST /api/revoke", cfg.handlerRevoke) + mux.HandleFunc("POScT /api/revoke", cfg.handlerRevoke) mux.HandleFunc("POST /api/users", cfg.handlerUsersCreate)