Skip to content

Commit

Permalink
adding video component
Browse files Browse the repository at this point in the history
  • Loading branch information
damongolding committed Jan 21, 2025
1 parent 74a258f commit f07aa94
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/immich/immich_album.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (i *ImmichAsset) ImageFromAlbum(albumID string, albumAssetsOrder ImmichAsse
for assetIndex, asset := range album.Assets {

// We only want images and that are not trashed or archived (unless wanted by user)
isInvalidType := asset.Type != ImageType
isInvalidType := asset.Type == AudioType || asset.Type == OtherType
isTrashed := asset.IsTrashed
isArchived := asset.IsArchived && !requestConfig.ShowArchived
isInvalidRatio := !i.ratioCheck(&asset)
Expand Down
19 changes: 19 additions & 0 deletions internal/templates/componentes/video/video.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package components

import (
"fmt"
"github.com/damongolding/immich-kiosk/internal/common"
)

templ Video(viewData common.ViewData) {
<div class="frame">
<div class="frame--video">
<video
muted
autoplay
src={ fmt.Sprintf("/video/%s", viewData.Images[0].ImmichImage.ID) }
></video>
</div>
</div>
@renderHistory(viewData)
}
78 changes: 77 additions & 1 deletion internal/video/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package video

import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"slices"
"sync"
"time"

"github.com/charmbracelet/log"
"github.com/damongolding/immich-kiosk/internal/config"
)

var (
Expand All @@ -21,11 +26,14 @@ type Video struct {
ID string
LastAccessed time.Time
FileName string
FilePath string
}

type VideoManager struct {
mu sync.RWMutex

baseConfig config.Config

DownloadQueue []string

Videos []Video
Expand Down Expand Up @@ -121,13 +129,27 @@ func (v *VideoManager) IsDownloading(id string) bool {
return slices.Contains(v.DownloadQueue, id)
}

func (v *VideoManager) AddVideo(id, fileName string) {
func (v *VideoManager) GetVideo(id string) (Video, error) {
v.mu.RLock()
defer v.mu.RUnlock()

for _, video := range v.Videos {
if video.ID == id {
return video, nil
}
}

return Video{}, fmt.Errorf("video not found")
}

func (v *VideoManager) AddVideo(id, fileName, filePath string) {
v.mu.Lock()
defer v.mu.Unlock()

v.Videos = append(v.Videos, Video{
ID: id,
FileName: fileName,
FilePath: filePath,
LastAccessed: time.Now(),
})
}
Expand All @@ -146,4 +168,58 @@ func (v *VideoManager) updateLastAccessed(id string) {

func (v *VideoManager) downloadVideo(id string) {

u, err := url.Parse(v.baseConfig.ImmichUrl)
if err != nil {
log.Error(err)
return
}

apiUrl := url.URL{
Scheme: u.Scheme,
Host: u.Host,
Path: path.Join("api", "assets", id, "video", "playback"),
}

// Create HTTP client
client := &http.Client{}

// Create request to your API
req, err := http.NewRequest("GET", apiUrl.String(), nil)
if err != nil {
log.Error("Error fetching video")
return
}

req.Header.Set("Accept", "application/octet-stream")
req.Header.Set("x-api-key", v.baseConfig.ImmichApiKey)

// Make the request
resp, err := client.Do(req)
if err != nil {
log.Error("Error fetching video")
return
}
defer resp.Body.Close()

// Get the video filename
filename := id + ".mp4"
filePath := filepath.Join(customTempVideoDir, filename)

// Create a file to save the video
out, err := os.Create(filePath)
if err != nil {
log.Error("Error creating video file", "err", err)
return
}
defer out.Close()

// Copy the video data to the file
_, err = out.ReadFrom(resp.Body)
if err != nil {
log.Error("Error writing video file", "err", err)
return
}

v.AddVideo(id, filename, filePath)
log.Debug("downloaded video", "path", filePath)
}

0 comments on commit f07aa94

Please sign in to comment.