package models import ( "crypto/md5" "encoding/json" "fmt" "strings" "time" ) type ImageEditSession struct { GUID string `json:"guid"` SiteID int64 `json:"siteId"` UserID int64 `json:"userId"` BaseUploadID int64 `json:"baseUploadId"` ImageExt string `json:"imageExt"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` Processors []ImageEditProcessor `json:"processors"` } func (ieh ImageEditSession) PreviewURL() string { return fmt.Sprintf("/sites/%v/imageedit/%v/preview/%v", ieh.SiteID, ieh.GUID, ieh.Processors[len(ieh.Processors)-1].VersionID) } func (ieh *ImageEditSession) RecalcVersionIDs() { for i, p := range ieh.Processors { if i == 0 { p.SetVersionID("") } else { p.SetVersionID(ieh.Processors[i-1].VersionID) } ieh.Processors[i] = p } } type ImageEditProcessor struct { ID string `json:"id"` Type string `json:"type"` Props json.RawMessage `json:"props"` // VersionID is a unique hash of the particular processor. This includes the version ID of the previous processor, // thereby causing a change of one processor to affect the version IDs of processors down the line. VersionID string `json:"versionId"` } func (ieh *ImageEditProcessor) SetVersionID(previousVersionID string) { var sb strings.Builder sb.WriteString(ieh.ID) sb.WriteString("-") sb.WriteString(previousVersionID) sb.WriteString("-") sb.WriteString(ieh.Type) sb.WriteString("-") sb.WriteString(string(ieh.Props)) ieh.VersionID = fmt.Sprintf("%x", md5.Sum([]byte(sb.String()))) } type CopyUploadProps struct { UploadID int64 `json:"uploadId"` }