This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/graph/schema.resolvers.go

442 lines
12 KiB
Go
Raw Normal View History

2021-02-02 20:34:10 +00:00
package graph
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
import (
2021-02-06 03:01:51 +00:00
"bytes"
2021-02-02 20:34:10 +00:00
"context"
2021-02-06 03:01:51 +00:00
"errors"
"io"
2021-02-04 10:16:13 +00:00
"net/http"
2021-02-06 03:01:51 +00:00
"os"
"path"
2021-02-04 10:16:13 +00:00
"strings"
2021-02-06 03:01:51 +00:00
"time"
2021-02-02 20:34:10 +00:00
2021-02-06 03:01:51 +00:00
"github.com/gabriel-vasile/mimetype"
2021-02-04 10:16:13 +00:00
"github.com/google/uuid"
2021-02-02 20:34:10 +00:00
"reichard.io/imagini/graph/generated"
"reichard.io/imagini/graph/model"
)
func (r *mutationResolver) CreateMediaItem(ctx context.Context, input model.NewMediaItem) (*model.MediaItem, error) {
2021-02-09 00:42:20 +00:00
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
2021-02-06 03:01:51 +00:00
}
2021-02-02 20:34:10 +00:00
2021-02-06 03:01:51 +00:00
// File header placeholder
fileHeader := make([]byte, 64)
// Copy headers into the buffer
if _, err := input.File.File.Read(fileHeader); err != nil {
return nil, errors.New("Upload Failed")
}
// Determine media type
fileMime := mimetype.Detect(fileHeader)
contentType := fileMime.String()
var isVideo bool
if strings.HasPrefix(contentType, "image/") {
isVideo = false
} else if strings.HasPrefix(contentType, "video/") {
isVideo = true
} else {
return nil, errors.New("Upload Failed")
}
// Derive Folder & File Path
mediaItemID := uuid.New().String()
fileName := mediaItemID + fileMime.Extension()
2021-02-09 00:42:20 +00:00
folderPath := path.Join("/" + r.Config.DataPath + "/media/" + userID)
2021-02-06 03:01:51 +00:00
os.MkdirAll(folderPath, 0700)
filePath := path.Join(folderPath + "/" + fileName)
// Create File
f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return nil, errors.New("Upload Failed")
}
defer f.Close()
// Copy header to file
_, err = io.Copy(f, bytes.NewReader(fileHeader))
if err != nil {
return nil, errors.New("Upload Failed")
}
// Copy remaining file
_, err = io.Copy(f, input.File.File)
if err != nil {
return nil, errors.New("Upload Failed")
}
// Create MediaItem From EXIF Data
mediaItem, err := mediaItemFromEXIFData(filePath)
if err != nil {
return nil, errors.New("Upload Failed")
}
// Add Additional MediaItem Fields
mediaItem.ID = mediaItemID
2021-02-09 00:42:20 +00:00
mediaItem.UserID = userID
2021-02-06 03:01:51 +00:00
mediaItem.IsVideo = isVideo
mediaItem.FileName = fileName
mediaItem.OrigName = input.File.Filename
// Create MediaItem in DB
err = r.DB.CreateMediaItem(mediaItem)
if err != nil {
return nil, errors.New("Upload Failed")
}
// Success
return mediaItem, nil
2021-02-02 20:34:10 +00:00
}
func (r *mutationResolver) CreateAlbum(ctx context.Context, input model.NewAlbum) (*model.Album, error) {
2021-02-09 00:42:20 +00:00
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
2021-02-07 02:11:44 +00:00
}
album := &model.Album{
2021-02-08 17:41:58 +00:00
Name: input.Name,
2021-02-09 00:42:20 +00:00
UserID: userID,
2021-02-07 02:11:44 +00:00
}
2021-02-09 00:42:20 +00:00
err = r.DB.CreateAlbum(album)
2021-02-07 02:11:44 +00:00
if err != nil {
return nil, err
}
return album, nil
2021-02-02 20:34:10 +00:00
}
func (r *mutationResolver) CreateTag(ctx context.Context, input model.NewTag) (*model.Tag, error) {
2021-02-09 00:42:20 +00:00
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
2021-02-08 17:41:58 +00:00
}
2021-02-07 02:11:44 +00:00
tag := &model.Tag{
2021-02-08 17:41:58 +00:00
Name: input.Name,
2021-02-09 00:42:20 +00:00
UserID: userID,
2021-02-07 02:11:44 +00:00
}
2021-02-09 00:42:20 +00:00
err = r.DB.CreateTag(tag)
2021-02-07 02:11:44 +00:00
if err != nil {
return nil, err
}
return tag, nil
2021-02-02 20:34:10 +00:00
}
func (r *mutationResolver) CreateUser(ctx context.Context, input model.NewUser) (*model.User, error) {
2021-02-03 03:55:35 +00:00
user := &model.User{
Email: input.Email,
Username: input.Username,
FirstName: input.FirstName,
LastName: input.LastName,
Role: input.Role,
AuthType: input.AuthType,
Password: input.Password,
}
err := r.DB.CreateUser(user)
if err != nil {
2021-02-04 10:16:13 +00:00
return nil, err
2021-02-03 03:55:35 +00:00
}
return user, nil
}
2021-02-04 10:16:13 +00:00
func (r *queryResolver) Login(ctx context.Context, user string, password string, deviceID *string) (*model.AuthResponse, error) {
2021-02-09 00:42:20 +00:00
// Acquire Context
resp, req, err := getContextHTTP(ctx)
if err != nil {
return nil, err
}
2021-02-04 10:16:13 +00:00
2021-02-06 03:01:51 +00:00
// Clear All Cookies By Default
accessCookie := http.Cookie{Name: "AccessToken", Path: "/", HttpOnly: true, MaxAge: -1, Expires: time.Now().Add(-100 * time.Hour)}
refreshCookie := http.Cookie{Name: "RefreshToken", Path: "/", HttpOnly: true, MaxAge: -1, Expires: time.Now().Add(-100 * time.Hour)}
http.SetCookie(*resp, &accessCookie)
http.SetCookie(*resp, &refreshCookie)
2021-02-04 10:16:13 +00:00
// Do Login
foundUser, success := r.Auth.AuthenticateUser(user, password)
if !success {
return &model.AuthResponse{Result: model.AuthResultFailure}, nil
}
2021-02-03 03:55:35 +00:00
2021-02-04 10:16:13 +00:00
// Upsert Device
2021-02-06 03:01:51 +00:00
foundDevice := model.Device{UserID: foundUser.ID}
2021-02-04 10:16:13 +00:00
if deviceID != nil {
parsedDeviceID, err := uuid.Parse(*deviceID)
if err != nil {
return &model.AuthResponse{Result: model.AuthResultFailure}, nil
}
foundDevice.ID = parsedDeviceID.String()
count, err := r.DB.Device(&foundDevice)
if count != 1 || err != nil {
return &model.AuthResponse{Result: model.AuthResultFailure}, nil
}
} else {
foundDevice.Type = deriveDeviceType(req)
err := r.DB.CreateDevice(&foundDevice)
if err != nil {
return &model.AuthResponse{Result: model.AuthResultFailure}, nil
}
}
// Create Tokens
accessToken, err := r.Auth.CreateJWTAccessToken(foundUser, foundDevice)
if err != nil {
return &model.AuthResponse{Result: model.AuthResultFailure}, nil
}
refreshToken, err := r.Auth.CreateJWTRefreshToken(foundUser, foundDevice)
if err != nil {
return &model.AuthResponse{Result: model.AuthResultFailure}, nil
}
// Set appropriate cookies (TODO: Only for web!)
accessCookie = http.Cookie{Name: "AccessToken", Value: accessToken, Path: "/", HttpOnly: false}
refreshCookie = http.Cookie{Name: "RefreshToken", Value: refreshToken, Path: "/", HttpOnly: false}
2021-02-04 10:16:13 +00:00
http.SetCookie(*resp, &accessCookie)
http.SetCookie(*resp, &refreshCookie)
// Only for iOS & Android (TODO: Remove for web! Only cause affected by CORS during development)
(*resp).Header().Set("X-Imagini-AccessToken", accessToken)
(*resp).Header().Set("X-Imagini-RefreshToken", refreshToken)
2021-02-04 10:16:13 +00:00
return &model.AuthResponse{Result: model.AuthResultSuccess, Device: &foundDevice}, nil
2021-02-03 03:55:35 +00:00
}
2021-02-04 10:16:13 +00:00
func (r *queryResolver) Logout(ctx context.Context) (*model.AuthResponse, error) {
2021-02-09 00:42:20 +00:00
// Acquire Context
resp, _, err := getContextHTTP(ctx)
if err != nil {
return nil, err
}
2021-02-06 03:01:51 +00:00
// Clear All Cookies
accessCookie := http.Cookie{Name: "AccessToken", Path: "/", HttpOnly: true, MaxAge: -1, Expires: time.Now().Add(-100 * time.Hour)}
refreshCookie := http.Cookie{Name: "RefreshToken", Path: "/", HttpOnly: true, MaxAge: -1, Expires: time.Now().Add(-100 * time.Hour)}
http.SetCookie(*resp, &accessCookie)
http.SetCookie(*resp, &refreshCookie)
2021-02-04 10:16:13 +00:00
return &model.AuthResponse{Result: model.AuthResultSuccess}, nil
2021-02-02 20:34:10 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) MediaItem(ctx context.Context, id string) (*model.MediaItem, error) {
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
2021-02-08 17:41:58 +00:00
}
mediaItemID, err := uuid.Parse(id)
2021-02-07 02:11:44 +00:00
if err != nil {
return nil, errors.New("Invalid ID Format")
}
2021-02-09 00:42:20 +00:00
foundMediaItem := &model.MediaItem{ID: mediaItemID.String(), UserID: userID}
2021-02-07 02:11:44 +00:00
count, err := r.DB.MediaItem(foundMediaItem)
if err != nil {
return nil, errors.New("DB Error")
} else if count != 1 {
return nil, errors.New("MediaItem Not Found")
}
return foundMediaItem, nil
2021-02-02 20:34:10 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) Device(ctx context.Context, id string) (*model.Device, error) {
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
2021-02-08 17:41:58 +00:00
}
2021-02-07 02:11:44 +00:00
deviceID, err := uuid.Parse(id)
if err != nil {
return nil, errors.New("Invalid ID Format")
}
2021-02-09 00:42:20 +00:00
foundDevice := &model.Device{ID: deviceID.String(), UserID: userID}
2021-02-07 02:11:44 +00:00
count, err := r.DB.Device(foundDevice)
if err != nil {
return nil, errors.New("DB Error")
} else if count != 1 {
return nil, errors.New("Device Not Found")
}
return foundDevice, nil
2021-02-02 20:34:10 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) Album(ctx context.Context, id string) (*model.Album, error) {
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
2021-02-08 17:41:58 +00:00
}
2021-02-07 02:11:44 +00:00
albumID, err := uuid.Parse(id)
if err != nil {
return nil, errors.New("Invalid ID Format")
}
2021-02-09 00:42:20 +00:00
foundAlbum := &model.Album{ID: albumID.String(), UserID: userID}
2021-02-07 02:11:44 +00:00
count, err := r.DB.Album(foundAlbum)
if err != nil {
return nil, errors.New("DB Error")
} else if count != 1 {
return nil, errors.New("Album Not Found")
}
return foundAlbum, nil
2021-02-02 20:34:10 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) User(ctx context.Context, id string) (*model.User, error) {
2021-02-07 02:11:44 +00:00
userID, err := uuid.Parse(id)
if err != nil {
return nil, errors.New("Invalid ID Format")
}
foundUser := &model.User{ID: userID.String()}
count, err := r.DB.User(foundUser)
if err != nil {
return nil, errors.New("DB Error")
} else if count != 1 {
return nil, errors.New("User Not Found")
}
return foundUser, nil
2021-02-03 03:55:35 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) Tag(ctx context.Context, id string) (*model.Tag, error) {
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
}
2021-02-07 02:11:44 +00:00
tagID, err := uuid.Parse(id)
if err != nil {
return nil, errors.New("Invalid ID Format")
}
2021-02-09 00:42:20 +00:00
foundTag := &model.Tag{ID: tagID.String(), UserID: userID}
2021-02-07 02:11:44 +00:00
count, err := r.DB.Tag(foundTag)
if err != nil {
return nil, errors.New("DB Error")
} else if count != 1 {
return nil, errors.New("Tag Not Found")
}
return foundTag, nil
2021-02-02 20:34:10 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) Me(ctx context.Context) (*model.User, error) {
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
2021-02-07 02:11:44 +00:00
}
2021-02-09 00:42:20 +00:00
foundUser := &model.User{ID: userID}
2021-02-07 02:11:44 +00:00
count, err := r.DB.User(foundUser)
if err != nil || count != 1 {
return nil, errors.New("DB Error")
}
return foundUser, nil
2021-02-02 20:34:10 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) MediaItems(ctx context.Context, filter *model.MediaItemFilter, page *model.Page, order *model.Order) (*model.MediaItemResponse, error) {
// Acquire Context
userID, _, err := getContextIDs(ctx)
2021-02-07 02:11:44 +00:00
if err != nil {
2021-02-09 00:42:20 +00:00
return nil, err
}
resp, pageResponse, err := r.DB.MediaItems(userID, filter, page, order)
if err != nil {
return nil, errors.New("DB Error")
2021-02-07 02:11:44 +00:00
}
return &model.MediaItemResponse{
Data: resp,
2021-02-09 00:42:20 +00:00
Page: &pageResponse,
2021-02-07 02:11:44 +00:00
}, nil
2021-02-02 20:34:10 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) Devices(ctx context.Context, filter *model.DeviceFilter, page *model.Page, order *model.Order) (*model.DeviceResponse, error) {
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
2021-02-08 17:41:58 +00:00
}
2021-02-09 00:42:20 +00:00
resp, pageResponse, err := r.DB.Devices(userID, filter, page, order)
2021-02-07 02:11:44 +00:00
if err != nil {
2021-02-08 17:41:58 +00:00
return nil, errors.New("DB Error")
2021-02-07 02:11:44 +00:00
}
return &model.DeviceResponse{
Data: resp,
2021-02-09 00:42:20 +00:00
Page: &pageResponse,
2021-02-07 02:11:44 +00:00
}, nil
2021-02-02 20:34:10 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) Albums(ctx context.Context, filter *model.AlbumFilter, page *model.Page, order *model.Order) (*model.AlbumResponse, error) {
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
}
resp, pageResponse, err := r.DB.Albums(userID, filter, page, order)
2021-02-07 02:11:44 +00:00
if err != nil {
return nil, errors.New("Context Error")
}
return &model.AlbumResponse{
Data: resp,
2021-02-09 00:42:20 +00:00
Page: &pageResponse,
2021-02-07 02:11:44 +00:00
}, nil
2021-02-02 20:34:10 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) Tags(ctx context.Context, filter *model.TagFilter, page *model.Page, order *model.Order) (*model.TagResponse, error) {
// Acquire Context
userID, _, err := getContextIDs(ctx)
if err != nil {
return nil, err
}
resp, pageResponse, err := r.DB.Tags(userID, filter, page, order)
2021-02-07 02:11:44 +00:00
if err != nil {
return nil, errors.New("Context Error")
}
return &model.TagResponse{
Data: resp,
2021-02-09 00:42:20 +00:00
Page: &pageResponse,
2021-02-07 02:11:44 +00:00
}, nil
2021-02-02 20:34:10 +00:00
}
2021-02-09 00:42:20 +00:00
func (r *queryResolver) Users(ctx context.Context, filter *model.UserFilter, page *model.Page, order *model.Order) (*model.UserResponse, error) {
resp, pageResponse, err := r.DB.Users(filter, page, order)
2021-02-03 03:55:35 +00:00
if err != nil {
2021-02-07 02:11:44 +00:00
return nil, errors.New("Context Error")
2021-02-03 03:55:35 +00:00
}
return &model.UserResponse{
Data: resp,
2021-02-09 00:42:20 +00:00
Page: &pageResponse,
2021-02-03 03:55:35 +00:00
}, nil
2021-02-02 20:34:10 +00:00
}
// Mutation returns generated.MutationResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResolver{r} }
// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }
type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }