Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tags #512

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
300 changes: 255 additions & 45 deletions cmd/upload/upload.go

Large diffs are not rendered by default.

68 changes: 60 additions & 8 deletions cmd/upload/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ func (c *stubIC) GetAllAssetsWithFilter(context.Context, func(*immich.Asset) err
return nil
}

func (c *stubIC) AssetUpload(context.Context, *browser.LocalAssetFile) (immich.AssetResponse, error) {
func (c *stubIC) AssetUpload(
context.Context,
*browser.LocalAssetFile,
) (immich.AssetResponse, error) {
return immich.AssetResponse{}, nil
}

Expand All @@ -35,23 +38,45 @@ func (c *stubIC) GetAllAlbums(context.Context) ([]immich.AlbumSimplified, error)
return nil, nil
}

func (c *stubIC) AddAssetToAlbum(context.Context, string, []string) ([]immich.UpdateAlbumResult, error) {
func (c *stubIC) AddAssetToAlbum(
context.Context,
string,
[]string,
) ([]immich.UpdateAlbumResult, error) {
return nil, nil
}

func (c *stubIC) CreateAlbum(context.Context, string, string, []string) (immich.AlbumSimplified, error) {
func (c *stubIC) CreateAlbum(
context.Context,
string,
string,
[]string,
) (immich.AlbumSimplified, error) {
return immich.AlbumSimplified{}, nil
}

func (c *stubIC) UpdateAssets(ctx context.Context, ids []string, isArchived bool, isFavorite bool, latitude float64, longitude float64, removeParent bool, stackParentID string) error {
func (c *stubIC) UpdateAssets(
ctx context.Context,
ids []string,
isArchived bool,
isFavorite bool,
latitude float64,
longitude float64,
removeParent bool,
stackParentID string,
) error {
return nil
}

func (c *stubIC) StackAssets(ctx context.Context, cover string, ids []string) error {
return nil
}

func (c *stubIC) UpdateAsset(ctx context.Context, id string, a *browser.LocalAssetFile) (*immich.Asset, error) {
func (c *stubIC) UpdateAsset(
ctx context.Context,
id string,
a *browser.LocalAssetFile,
) (*immich.Asset, error) {
return nil, nil
}

Expand Down Expand Up @@ -112,20 +137,32 @@ type icCatchUploadsAssets struct {
albums map[string][]string
}

func (c *icCatchUploadsAssets) AssetUpload(ctx context.Context, a *browser.LocalAssetFile) (immich.AssetResponse, error) {
func (c *icCatchUploadsAssets) AssetUpload(
ctx context.Context,
a *browser.LocalAssetFile,
) (immich.AssetResponse, error) {
c.assets = append(c.assets, a.FileName)
return immich.AssetResponse{
ID: a.FileName,
}, nil
}

func (c *icCatchUploadsAssets) AddAssetToAlbum(ctx context.Context, album string, ids []string) ([]immich.UpdateAlbumResult, error) {
func (c *icCatchUploadsAssets) AddAssetToAlbum(
ctx context.Context,
album string,
ids []string,
) ([]immich.UpdateAlbumResult, error) {
l := c.albums[album]
c.albums[album] = append(l, ids...)
return nil, nil
}

func (c *icCatchUploadsAssets) CreateAlbum(ctx context.Context, album string, description string, ids []string) (immich.AlbumSimplified, error) {
func (c *icCatchUploadsAssets) CreateAlbum(
ctx context.Context,
album string,
description string,
ids []string,
) (immich.AlbumSimplified, error) {
if album == "" {
panic("can't create album without name")
}
Expand All @@ -140,6 +177,21 @@ func (c *icCatchUploadsAssets) CreateAlbum(ctx context.Context, album string, de
}, nil
}

func (c *icCatchUploadsAssets) UpsertTags(
ctx context.Context,
tags []string,
) ([]immich.TagSimplified, error) {
return nil, nil
}

func (c *icCatchUploadsAssets) TagAssets(
ctx context.Context,
tagID string,
assetIDs []string,
) ([]immich.TagAssetsResponse, error) {
return nil, nil
}

func TestUpload(t *testing.T) {
testCases := []struct {
name string
Expand Down
8 changes: 7 additions & 1 deletion helpers/fileevent/fileevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
UploadAlbumCreated
UploadAddToAlbum // = "Added to an album"
UploadServerError // = "Server error"
UploadTagAssetError

Uploaded // = "Uploaded"
Stacked // = "Stacked"
Expand Down Expand Up @@ -66,6 +67,7 @@ var _code = map[Code]string{
UploadServerBetter: "server has a better asset",
UploadAlbumCreated: "album created/updated",
UploadServerError: "upload error",
UploadTagAssetError: "tag asset error",
Uploaded: "uploaded",

Stacked: "Stacked",
Expand Down Expand Up @@ -235,7 +237,11 @@ func (r *Recorder) WriteFileCounts(w io.Writer) error {
}

func (r *Recorder) TotalAssets() int64 {
return atomic.LoadInt64(&r.counts[DiscoveredImage]) + atomic.LoadInt64(&r.counts[DiscoveredVideo])
return atomic.LoadInt64(
&r.counts[DiscoveredImage],
) + atomic.LoadInt64(
&r.counts[DiscoveredVideo],
)
}

func (r *Recorder) TotalProcessedGP() int64 {
Expand Down
23 changes: 20 additions & 3 deletions immich/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
EndPointGetAssetStatistics = "GetAssetStatistics"
EndPointGetSupportedMediaTypes = "GetSupportedMediaTypes"
EndPointGetAllAssets = "GetAllAssets"
EndPointUpsertTags = "UpsertTags"
EndPointTagAssets = "TagAssets"
)

type TooManyInternalError struct {
Expand Down Expand Up @@ -136,7 +138,11 @@ var callSequence atomic.Int64

const ctxCallSequenceID = "api-call-sequence"

func (sc *serverCall) request(method string, url string, opts ...serverRequestOption) *http.Request {
func (sc *serverCall) request(
method string,
url string,
opts ...serverRequestOption,
) *http.Request {
if sc.ic.apiTraceWriter != nil && sc.endPoint != EndPointGetJobs {
seq := callSequence.Add(1)
sc.ctx = context.WithValue(sc.ctx, ctxCallSequenceID, seq)
Expand Down Expand Up @@ -168,7 +174,10 @@ func postRequest(url string, cType string, opts ...serverRequestOption) requestF
if sc.err != nil {
return nil
}
return sc.request(http.MethodPost, sc.ic.endPoint+url, append(opts, setContentType(cType))...)
return sc.request(
http.MethodPost,
sc.ic.endPoint+url,
append(opts, setContentType(cType))...)
}
}

Expand Down Expand Up @@ -296,7 +305,15 @@ func responseJSON[T any](object *T) serverResponseOption {
err := json.NewDecoder(resp.Body).Decode(object)
if sc.ic.apiTraceWriter != nil && sc.endPoint != EndPointGetJobs {
seq := sc.ctx.Value(ctxCallSequenceID)
fmt.Fprintln(sc.ic.apiTraceWriter, time.Now().Format(time.RFC3339), "RESPONSE", seq, sc.endPoint, resp.Request.Method, resp.Request.URL.String())
fmt.Fprintln(
sc.ic.apiTraceWriter,
time.Now().Format(time.RFC3339),
"RESPONSE",
seq,
sc.endPoint,
resp.Request.Method,
resp.Request.URL.String(),
)
fmt.Fprintln(sc.ic.apiTraceWriter, " Status:", resp.Status)
fmt.Fprintln(sc.ic.apiTraceWriter, "-- response body --")
dec := json.NewEncoder(newLimitWriter(sc.ic.apiTraceWriter, 100))
Expand Down
25 changes: 23 additions & 2 deletions immich/immich.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,42 @@ type ImmichInterface interface {
UpdateAsset(ctx context.Context, ID string, a *browser.LocalAssetFile) (*Asset, error)
GetAllAssets(ctx context.Context) ([]*Asset, error)
AddAssetToAlbum(context.Context, string, []string) ([]UpdateAlbumResult, error)
UpdateAssets(ctx context.Context, IDs []string, isArchived bool, isFavorite bool, latitude float64, longitude float64, removeParent bool, stackParentID string) error
UpdateAssets(
ctx context.Context,
IDs []string,
isArchived bool,
isFavorite bool,
latitude float64,
longitude float64,
removeParent bool,
stackParentID string,
) error
GetAllAssetsWithFilter(context.Context, func(*Asset) error) error
AssetUpload(context.Context, *browser.LocalAssetFile) (AssetResponse, error)
DeleteAssets(context.Context, []string, bool) error

GetAllAlbums(ctx context.Context) ([]AlbumSimplified, error)
GetAlbumInfo(ctx context.Context, id string, withoutAssets bool) (AlbumContent, error)
CreateAlbum(ctx context.Context, tilte string, description string, ids []string) (AlbumSimplified, error)
CreateAlbum(
ctx context.Context,
tilte string,
description string,
ids []string,
) (AlbumSimplified, error)
GetAssetAlbums(ctx context.Context, ID string) ([]AlbumSimplified, error)
DeleteAlbum(ctx context.Context, id string) error

StackAssets(ctx context.Context, cover string, IDs []string) error

SupportedMedia() SupportedMedia
GetJobs(ctx context.Context) (map[string]Job, error)

UpsertTags(ctx context.Context, tags []string) ([]TagSimplified, error)
TagAssets(
ctx context.Context,
tagID string,
assetIDs []string,
) ([]TagAssetsResponse, error)
}

type UnsupportedMedia struct {
Expand Down
50 changes: 50 additions & 0 deletions immich/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package immich

import (
"context"
"fmt"
)

type TagSimplified struct {
ID string `json:"id"`
Name string `json:"name"`
Value string `json:"value"`
}

type TagAssetsResponse struct {
Error string `json:"error,omitempty"` // [duplicate, no_permission, not_found, unknown]
ID string `json:"id"`
Success bool `json:"success"`
}

func (ic *ImmichClient) UpsertTags(ctx context.Context, tags []string) ([]TagSimplified, error) {
var resp []TagSimplified
body := struct {
Tags []string `json:"tags"`
}{Tags: tags}
err := ic.newServerCall(ctx, EndPointUpsertTags).
do(putRequest("/tags", setJSONBody(body), setAcceptJSON()), responseJSON(&resp))
if err != nil {
return nil, err
}
return resp, nil
}

func (ic *ImmichClient) TagAssets(
ctx context.Context,
tagID string,
assetIDs []string,
) ([]TagAssetsResponse, error) {
var resp []TagAssetsResponse

body := struct {
IDs []string `json:"ids"`
}{IDs: assetIDs}
err := ic.newServerCall(ctx, EndPointTagAssets).
do(putRequest(fmt.Sprintf("/tags/%s/assets", tagID), setJSONBody(body), setAcceptJSON()), responseJSON(&resp))

if err != nil {
return nil, err
}
return resp, nil
}
55 changes: 49 additions & 6 deletions internal/fakeImmich/immich.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ func (c *MockedCLient) GetAllAssetsWithFilter(context.Context, func(*immich.Asse
return nil
}

func (c *MockedCLient) AssetUpload(context.Context, *browser.LocalAssetFile) (immich.AssetResponse, error) {
func (c *MockedCLient) AssetUpload(
context.Context,
*browser.LocalAssetFile,
) (immich.AssetResponse, error) {
return immich.AssetResponse{}, nil
}

Expand All @@ -26,23 +29,45 @@ func (c *MockedCLient) GetAllAlbums(context.Context) ([]immich.AlbumSimplified,
return nil, nil
}

func (c *MockedCLient) AddAssetToAlbum(context.Context, string, []string) ([]immich.UpdateAlbumResult, error) {
func (c *MockedCLient) AddAssetToAlbum(
context.Context,
string,
[]string,
) ([]immich.UpdateAlbumResult, error) {
return nil, nil
}

func (c *MockedCLient) CreateAlbum(context.Context, string, string, []string) (immich.AlbumSimplified, error) {
func (c *MockedCLient) CreateAlbum(
context.Context,
string,
string,
[]string,
) (immich.AlbumSimplified, error) {
return immich.AlbumSimplified{}, nil
}

func (c *MockedCLient) UpdateAssets(ctx context.Context, ids []string, isArchived bool, isFavorite bool, latitude float64, longitude float64, removeParent bool, stackParentID string) error {
func (c *MockedCLient) UpdateAssets(
ctx context.Context,
ids []string,
isArchived bool,
isFavorite bool,
latitude float64,
longitude float64,
removeParent bool,
stackParentID string,
) error {
return nil
}

func (c *MockedCLient) StackAssets(ctx context.Context, cover string, ids []string) error {
return nil
}

func (c *MockedCLient) UpdateAsset(ctx context.Context, id string, a *browser.LocalAssetFile) (*immich.Asset, error) {
func (c *MockedCLient) UpdateAsset(
ctx context.Context,
id string,
a *browser.LocalAssetFile,
) (*immich.Asset, error) {
return nil, nil
}

Expand All @@ -64,7 +89,10 @@ func (c *MockedCLient) ValidateConnection(ctx context.Context) (immich.User, err
return immich.User{}, nil
}

func (c *MockedCLient) GetAssetAlbums(ctx context.Context, id string) ([]immich.AlbumSimplified, error) {
func (c *MockedCLient) GetAssetAlbums(
ctx context.Context,
id string,
) ([]immich.AlbumSimplified, error) {
return nil, nil
}

Expand Down Expand Up @@ -95,3 +123,18 @@ func (c *MockedCLient) GetJobs(ctx context.Context) (map[string]immich.Job, erro
func (c *MockedCLient) GetAlbumInfo(context.Context, string, bool) (immich.AlbumContent, error) {
return immich.AlbumContent{}, nil
}

func (c *MockedCLient) UpsertTags(
ctx context.Context,
tags []string,
) ([]immich.TagSimplified, error) {
return nil, nil
}

func (c *MockedCLient) TagAssets(
ctx context.Context,
tagID string,
assetIDs []string,
) ([]immich.TagAssetsResponse, error) {
return nil, nil
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Use this command for uploading photos and videos from a local directory, a zippe
| `-exclude-types=".ext,.ext,.ext..."` | List of excluded extensions. | |
| `-when-no-date=FILE\|NOW` | When the date of take can't be determined, use the FILE's date or the current time NOW. | `FILE` |
| `-exclude-files=pattern` | Ignore files based on a pattern. Case insensitive. Repeat the option for each pattern do you need. | `@eaDir/`<br>`@__thumb/`<br>`SYNOFILE_THUMB_*.*`<br>`Lightroom Catalog/`<br>`thumbnails/` |
| `tags="COMMA-SEPARATED,LIST/OF,TAGS"` | Tag imported assets with specified tags. Can use forward slashes for hierarchal tags (ex. "Holiday/Groundhog's Day"). | |

### Date selection:
Fine-tune import based on specific dates:
Expand Down