image previews wip

This commit is contained in:
erinn 2021-12-14 13:21:45 -08:00
parent 2c396826e7
commit d8bf2d3227
1 changed files with 36 additions and 0 deletions

View File

@ -8,8 +8,10 @@ import (
"fmt"
"io"
"math"
"os"
path "path/filepath"
"strconv"
"strings"
"time"
"cwtch.im/cwtch/model"
@ -31,6 +33,13 @@ func FunctionalityGate(experimentMap map[string]bool) (*Functionality, error) {
return nil, errors.New("filesharing is not enabled")
}
func PreviewFunctionalityGate(experimentMap map[string]bool) (*Functionality, error) {
if experimentMap["filesharing"] == true && experimentMap["filesharing-images"] == true {
return new(Functionality), nil
}
return nil, errors.New("image previews are not enabled")
}
// OverlayMessage presents the canonical format of the File Sharing functionality Overlay Message
// This is the format that the UI will parse to display the message
type OverlayMessage struct {
@ -40,6 +49,10 @@ type OverlayMessage struct {
Size uint64 `json:"s"`
}
func (om *OverlayMessage) FileKey() string {
return fmt.Sprintf("%s.%s", om.Hash, om.Nonce)
}
// DownloadFile given a profile, a conversation handle and a file sharing key, start off a download process
// to downloadFilePath
func (f *Functionality) DownloadFile(profile peer.CwtchPeer, handle string, downloadFilePath string, manifestFilePath string, key string) {
@ -101,3 +114,26 @@ func (f *Functionality) ShareFile(filepath string, profile peer.CwtchPeer, handl
return nil
}
func GenerateDownloadPath(basePath, fileName string) (filePath, manifestPath string) {
filePath = fmt.Sprintf("%s%s", basePath, fileName)
manifestPath = fmt.Sprintf("%s.manifest", filePath)
parts := strings.Split(fileName, ".")
fileNameBase := parts[0]
fileNameExt := ""
if len(parts) > 1 {
fileNameBase = strings.Join(parts[0:len(parts)-1], ".")
fileNameExt = fmt.Sprintf(".%s", parts[len(parts)-1])
}
for i := 2; ; i++ {
if _, err := os.Open(filePath); os.IsNotExist(err) {
if _, err := os.Open(manifestPath); os.IsNotExist(err) {
return
}
}
filePath = fmt.Sprintf("%s%s (%d)%s", basePath, fileNameBase, i, fileNameExt)
manifestPath = fmt.Sprintf("%s.manifest", filePath)
}
}