From dd36c852d386c6587052f8121e0e65c3e5796ed9 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 4 Jun 2019 13:37:59 +0900 Subject: [PATCH] Achieve consistency by using a single Image type --- .vscode/settings.json | 1 + arn/User.go | 3 ++- arn/UserAvatar.go | 4 ++-- jobs/kitsu-characters-parse/kitsu-characters-parse.go | 4 ++-- pages/index/apiroutes/apiroutes.go | 4 ++-- pages/settings/personal.pixy | 4 ++-- pages/upload/{amv.go => amv-file.go} | 0 pages/upload/{character.go => character-image.go} | 0 pages/upload/{image-formats.go => formats.go} | 0 pages/upload/{group.go => group-image.go} | 0 pages/upload/{cover.go => user-cover.go} | 4 ++-- pages/upload/{avatar.go => user-image.go} | 6 +++--- scripts/Actions/Upload.ts | 10 +++++----- utils/routetests/All.go | 1 - 14 files changed, 21 insertions(+), 20 deletions(-) rename pages/upload/{amv.go => amv-file.go} (100%) rename pages/upload/{character.go => character-image.go} (100%) rename pages/upload/{image-formats.go => formats.go} (100%) rename pages/upload/{group.go => group-image.go} (100%) rename pages/upload/{cover.go => user-cover.go} (89%) rename pages/upload/{avatar.go => user-image.go} (84%) diff --git a/.vscode/settings.json b/.vscode/settings.json index e7f69be6..2310daab 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,6 +35,7 @@ "**/*.ogg": true, "**/*.m4a": true, "**/*.webm": true, + "**/*.log": true }, "[jade]": { "files.trimTrailingWhitespace": false diff --git a/arn/User.go b/arn/User.go index 87ddcaae..201fa467 100644 --- a/arn/User.go +++ b/arn/User.go @@ -55,6 +55,7 @@ type User struct { IP string `json:"ip" private:"true"` UserAgent string `json:"agent" private:"true"` Balance int `json:"balance" private:"true"` + Image Image `json:"image"` Avatar UserAvatar `json:"avatar"` Cover UserCover `json:"cover"` Accounts UserAccounts `json:"accounts" private:"true"` @@ -138,7 +139,7 @@ func RegisterUser(user *User) { if err == nil && response.StatusCode() == http.StatusOK { data := response.Bytes() - err = user.SetAvatarBytes(data) + err = user.SetImageBytes(data) if err != nil { fmt.Println(err) diff --git a/arn/UserAvatar.go b/arn/UserAvatar.go index 88592280..42102c86 100644 --- a/arn/UserAvatar.go +++ b/arn/UserAvatar.go @@ -65,8 +65,8 @@ type UserAvatar struct { LastModified int64 `json:"lastModified"` } -// SetAvatarBytes accepts a byte buffer that represents an image file and updates the avatar. -func (user *User) SetAvatarBytes(data []byte) error { +// SetImageBytes accepts a byte buffer that represents an image file and updates the avatar. +func (user *User) SetImageBytes(data []byte) error { // Decode img, format, err := image.Decode(bytes.NewReader(data)) diff --git a/jobs/kitsu-characters-parse/kitsu-characters-parse.go b/jobs/kitsu-characters-parse/kitsu-characters-parse.go index e961e341..156b47a4 100644 --- a/jobs/kitsu-characters-parse/kitsu-characters-parse.go +++ b/jobs/kitsu-characters-parse/kitsu-characters-parse.go @@ -7,8 +7,8 @@ import ( "strings" "github.com/akyoto/color" - "github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/kitsu" + "github.com/animenotifier/notify.moe/arn" ) func main() { @@ -27,7 +27,7 @@ func main() { Japanese: kitsuCharacter.Attributes.Names.JaJp, Synonyms: kitsuCharacter.Attributes.OtherNames, }, - Image: arn.CharacterImage{ + Image: arn.Image{ Extension: path.Ext(kitsu.FixImageURL(kitsuCharacter.Attributes.Image.Original)), }, Description: kitsuCharacter.Attributes.Description, diff --git a/pages/index/apiroutes/apiroutes.go b/pages/index/apiroutes/apiroutes.go index 9e1332e5..bdb01d63 100644 --- a/pages/index/apiroutes/apiroutes.go +++ b/pages/index/apiroutes/apiroutes.go @@ -54,8 +54,8 @@ func Register(app *aero.Application) { app.Post("/api/soundtrack/:id/download", soundtrack.Download) // Upload - app.Post("/api/upload/avatar", upload.Avatar) - app.Post("/api/upload/cover", upload.Cover) + app.Post("/api/upload/user/image", upload.UserImage) + app.Post("/api/upload/user/cover", upload.UserCover) app.Post("/api/upload/anime/:id/image", upload.AnimeImage) app.Post("/api/upload/character/:id/image", upload.CharacterImage) app.Post("/api/upload/group/:id/image", upload.GroupImage) diff --git a/pages/settings/personal.pixy b/pages/settings/personal.pixy index 518dec07..53eee477 100644 --- a/pages/settings/personal.pixy +++ b/pages/settings/personal.pixy @@ -49,7 +49,7 @@ component SettingsPersonal(user *arn.User) Icon("picture-o") span Cover - InputFileUpload("cover-input", "File", "image", "/api/upload/cover") + InputFileUpload("cover-input", "File", "image", "/api/upload/user/cover") .cover-preview(title="Recommended: 1920 x 450 | PNG or JPG") img.profile-cover.cover-input-preview.lazy(data-src=user.CoverLink("small"), data-webp="true", alt="Cover image") @@ -59,7 +59,7 @@ component SettingsPersonal(user *arn.User) p PRO account required. component AvatarInput(user *arn.User) - InputFileUpload("avatar-input", "File", "image", "/api/upload/avatar") + InputFileUpload("avatar-input", "File", "image", "/api/upload/user/image") .profile-image-container.avatar-preview if user.HasAvatar() diff --git a/pages/upload/amv.go b/pages/upload/amv-file.go similarity index 100% rename from pages/upload/amv.go rename to pages/upload/amv-file.go diff --git a/pages/upload/character.go b/pages/upload/character-image.go similarity index 100% rename from pages/upload/character.go rename to pages/upload/character-image.go diff --git a/pages/upload/image-formats.go b/pages/upload/formats.go similarity index 100% rename from pages/upload/image-formats.go rename to pages/upload/formats.go diff --git a/pages/upload/group.go b/pages/upload/group-image.go similarity index 100% rename from pages/upload/group.go rename to pages/upload/group-image.go diff --git a/pages/upload/cover.go b/pages/upload/user-cover.go similarity index 89% rename from pages/upload/cover.go rename to pages/upload/user-cover.go index c305c954..92cff86d 100644 --- a/pages/upload/cover.go +++ b/pages/upload/user-cover.go @@ -7,8 +7,8 @@ import ( "github.com/animenotifier/notify.moe/utils" ) -// Cover handles the cover image upload. -func Cover(ctx aero.Context) error { +// UserCover handles the cover image upload. +func UserCover(ctx aero.Context) error { user := utils.GetUser(ctx) if user == nil { diff --git a/pages/upload/avatar.go b/pages/upload/user-image.go similarity index 84% rename from pages/upload/avatar.go rename to pages/upload/user-image.go index 6a03abbe..0763b324 100644 --- a/pages/upload/avatar.go +++ b/pages/upload/user-image.go @@ -7,8 +7,8 @@ import ( "github.com/animenotifier/notify.moe/utils" ) -// Avatar handles the avatar upload. -func Avatar(ctx aero.Context) error { +// UserImage handles the avatar upload. +func UserImage(ctx aero.Context) error { user := utils.GetUser(ctx) if user == nil { @@ -23,7 +23,7 @@ func Avatar(ctx aero.Context) error { } // Set avatar file - err = user.SetAvatarBytes(data) + err = user.SetImageBytes(data) if err != nil { return ctx.Error(http.StatusInternalServerError, "Invalid image format", err) diff --git a/scripts/Actions/Upload.ts b/scripts/Actions/Upload.ts index 36df94b0..58b5df46 100644 --- a/scripts/Actions/Upload.ts +++ b/scripts/Actions/Upload.ts @@ -6,7 +6,7 @@ export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) { let fileType = button.dataset.type let endpoint = button.dataset.endpoint - if(endpoint === "/api/upload/cover" && arn.user && arn.user.dataset.pro !== "true") { + if(endpoint === "/api/upload/user/cover" && arn.user && arn.user.dataset.pro !== "true") { alert("Please buy a PRO account to use this feature.") return } @@ -47,14 +47,14 @@ export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) { let img = await loadImage(dataURL) switch(endpoint) { - case "/api/upload/avatar": + case "/api/upload/user/image": if(img.naturalWidth <= 280 || img.naturalHeight < 280) { arn.statusMessage.showError(`Your image has a resolution of ${img.naturalWidth} x ${img.naturalHeight} pixels which is too small. Recommended: 560 x 560. Minimum: 280 x 280.`, 8000) return } break - case "/api/upload/cover": + case "/api/upload/user/cover": if(img.naturalWidth <= 960 || img.naturalHeight < 225) { arn.statusMessage.showError(`Your image has a resolution of ${img.naturalWidth} x ${img.naturalHeight} pixels which is too small. Recommended: 1920 x 450. Minimum: 960 x 225.`, 8000) return @@ -101,7 +101,7 @@ function uploadFile(file: File, fileType: string, endpoint: string, arn: AnimeNo arn.statusMessage.showInfo(`Successfully uploaded your new ${fileType}.`) - if(endpoint === "/api/upload/avatar") { + if(endpoint === "/api/upload/user/image") { // We received the new avatar URL updateSideBarAvatar(responseText) } @@ -152,7 +152,7 @@ function loadImage(url: string): Promise { // Preview image function previewImage(dataURL: string, endpoint: string, previews: HTMLCollectionOf) { - if(endpoint === "/api/upload/avatar") { + if(endpoint === "/api/upload/user/image") { let svgPreview = document.getElementById("avatar-input-preview-svg") as HTMLImageElement if(svgPreview) { diff --git a/utils/routetests/All.go b/utils/routetests/All.go index d1fb76c2..fd81dfd6 100644 --- a/utils/routetests/All.go +++ b/utils/routetests/All.go @@ -478,7 +478,6 @@ var routeTests = map[string][]string{ "/settings/accounts": nil, "/settings/notifications": nil, "/settings/info": nil, - "/settings/avatar": nil, "/settings/style": nil, "/settings/extras": nil, "/shop": nil,