diff --git a/.idea/go.iml b/.idea/go.iml index c956989b2..d1513990f 100644 --- a/.idea/go.iml +++ b/.idea/go.iml @@ -1,7 +1,12 @@ - + + + + + + diff --git a/pkg/ffmpeg/ffprobe.go b/pkg/ffmpeg/ffprobe.go index ebcb425dc..c1ba4d95c 100644 --- a/pkg/ffmpeg/ffprobe.go +++ b/pkg/ffmpeg/ffprobe.go @@ -90,7 +90,7 @@ func parse(filePath string, probeJSON *FFProbeJSON) (*VideoFile, error) { fileStat, _ := os.Stat(filePath) result.Size = fileStat.Size() result.StartTime, _ = strconv.ParseFloat(probeJSON.Format.StartTime, 64) - result.CreationTime = probeJSON.Format.Tags.CreationTime + result.CreationTime = probeJSON.Format.Tags.CreationTime.Time audioStream := result.GetAudioStream() if audioStream != nil { diff --git a/pkg/ffmpeg/types.go b/pkg/ffmpeg/types.go index c55962ff4..6f85e21c3 100644 --- a/pkg/ffmpeg/types.go +++ b/pkg/ffmpeg/types.go @@ -1,7 +1,7 @@ package ffmpeg import ( - "time" + "github.com/stashapp/stash/pkg/models" ) type FFProbeJSON struct { @@ -17,11 +17,11 @@ type FFProbeJSON struct { Size string `json:"size"` StartTime string `json:"start_time"` Tags struct { - CompatibleBrands string `json:"compatible_brands"` - CreationTime time.Time `json:"creation_time"` - Encoder string `json:"encoder"` - MajorBrand string `json:"major_brand"` - MinorVersion string `json:"minor_version"` + CompatibleBrands string `json:"compatible_brands"` + CreationTime models.JSONTime `json:"creation_time"` + Encoder string `json:"encoder"` + MajorBrand string `json:"major_brand"` + MinorVersion string `json:"minor_version"` } `json:"tags"` } `json:"format"` Streams []FFProbeStream `json:"streams"` @@ -76,10 +76,10 @@ type FFProbeStream struct { StartPts int `json:"start_pts"` StartTime string `json:"start_time"` Tags struct { - CreationTime time.Time `json:"creation_time"` - HandlerName string `json:"handler_name"` - Language string `json:"language"` - Rotate string `json:"rotate"` + CreationTime models.JSONTime `json:"creation_time"` + HandlerName string `json:"handler_name"` + Language string `json:"language"` + Rotate string `json:"rotate"` } `json:"tags"` TimeBase string `json:"time_base"` Width int `json:"width,omitempty"` diff --git a/pkg/manager/jsonschema/scraped.go b/pkg/manager/jsonschema/scraped.go index a48b8e77f..26684a85d 100644 --- a/pkg/manager/jsonschema/scraped.go +++ b/pkg/manager/jsonschema/scraped.go @@ -3,24 +3,25 @@ package jsonschema import ( "encoding/json" "fmt" + "github.com/stashapp/stash/pkg/models" "os" ) type ScrapedItem struct { - Title string `json:"title,omitempty"` - Description string `json:"description,omitempty"` - URL string `json:"url,omitempty"` - Date string `json:"date,omitempty"` - Rating string `json:"rating,omitempty"` - Tags string `json:"tags,omitempty"` - Models string `json:"models,omitempty"` - Episode int `json:"episode,omitempty"` - GalleryFilename string `json:"gallery_filename,omitempty"` - GalleryURL string `json:"gallery_url,omitempty"` - VideoFilename string `json:"video_filename,omitempty"` - VideoURL string `json:"video_url,omitempty"` - Studio string `json:"studio,omitempty"` - UpdatedAt RailsTime `json:"updated_at,omitempty"` + Title string `json:"title,omitempty"` + Description string `json:"description,omitempty"` + URL string `json:"url,omitempty"` + Date string `json:"date,omitempty"` + Rating string `json:"rating,omitempty"` + Tags string `json:"tags,omitempty"` + Models string `json:"models,omitempty"` + Episode int `json:"episode,omitempty"` + GalleryFilename string `json:"gallery_filename,omitempty"` + GalleryURL string `json:"gallery_url,omitempty"` + VideoFilename string `json:"video_filename,omitempty"` + VideoURL string `json:"video_url,omitempty"` + Studio string `json:"studio,omitempty"` + UpdatedAt models.JSONTime `json:"updated_at,omitempty"` } func LoadScrapedFile(filePath string) ([]ScrapedItem, error) { diff --git a/pkg/manager/jsonschema/time_rails.go b/pkg/manager/jsonschema/time_rails.go deleted file mode 100644 index 15123b882..000000000 --- a/pkg/manager/jsonschema/time_rails.go +++ /dev/null @@ -1,36 +0,0 @@ -package jsonschema - -import ( - "fmt" - "github.com/stashapp/stash/pkg/utils" - "strings" - "time" -) - -type RailsTime struct { - time.Time -} - -func (ct *RailsTime) UnmarshalJSON(b []byte) (err error) { - s := strings.Trim(string(b), "\"") - if s == "null" { - ct.Time = time.Time{} - return - } - t, err := utils.ParseDateStringAsTime(s) - if t != nil { - ct.Time = *t - } - return -} - -func (ct *RailsTime) MarshalJSON() ([]byte, error) { - if ct.Time.UnixNano() == nilTime { - return []byte("null"), nil - } - return []byte(fmt.Sprintf("\"%s\"", ct.Time.Format(time.RFC3339))), nil -} - -func (ct *RailsTime) IsSet() bool { - return ct.UnixNano() != nilTime -} diff --git a/pkg/manager/task_export.go b/pkg/manager/task_export.go index c4b5dab03..38f9542aa 100644 --- a/pkg/manager/task_export.go +++ b/pkg/manager/task_export.go @@ -381,7 +381,7 @@ func (t *ExportTask) ExportScrapedItems(ctx context.Context) { } newScrapedItemJSON.Studio = studioName - updatedAt := jsonschema.RailsTime{Time: scrapedItem.UpdatedAt.Timestamp} // TODO keeping ruby format + updatedAt := models.JSONTime{Time: scrapedItem.UpdatedAt.Timestamp} // TODO keeping ruby format newScrapedItemJSON.UpdatedAt = updatedAt t.Scraped = append(t.Scraped, newScrapedItemJSON) diff --git a/pkg/models/json_time.go b/pkg/models/json_time.go new file mode 100644 index 000000000..af3fe3308 --- /dev/null +++ b/pkg/models/json_time.go @@ -0,0 +1,30 @@ +package models + +import ( + "fmt" + "github.com/stashapp/stash/pkg/utils" + "strings" + "time" +) + +type JSONTime struct { + time.Time +} + +func (jt *JSONTime) UnmarshalJSON(b []byte) (err error) { + s := strings.Trim(string(b), "\"") + if s == "null" { + jt.Time = time.Time{} + return + } + + jt.Time, err = utils.ParseDateStringAsTime(s) + return +} + +func (jt *JSONTime) MarshalJSON() ([]byte, error) { + if jt.Time.IsZero() { + return []byte("null"), nil + } + return []byte(fmt.Sprintf("\"%s\"", jt.Time.Format(time.RFC3339))), nil +} diff --git a/pkg/utils/date.go b/pkg/utils/date.go index b88f2d793..ba9a1e58a 100644 --- a/pkg/utils/date.go +++ b/pkg/utils/date.go @@ -14,34 +14,34 @@ func GetYMDFromDatabaseDate(dateString string) string { func ParseDateStringAsFormat(dateString string, format string) (string, error) { t, e := ParseDateStringAsTime(dateString) - if t != nil { + if e == nil { return t.Format(format), e } return "", fmt.Errorf("ParseDateStringAsFormat failed: dateString <%s>, format <%s>", dateString, format) } -func ParseDateStringAsTime(dateString string) (*time.Time, error) { +func ParseDateStringAsTime(dateString string) (time.Time, error) { // https://stackoverflow.com/a/20234207 WTF? t, e := time.Parse(time.RFC3339, dateString) if e == nil { - return &t, nil + return t, nil } t, e = time.Parse("2006-01-02", dateString) if e == nil { - return &t, nil + return t, nil } t, e = time.Parse("2006-01-02 15:04:05", dateString) if e == nil { - return &t, nil + return t, nil } t, e = time.Parse(railsTimeLayout, dateString) if e == nil { - return &t, nil + return t, nil } - return nil, fmt.Errorf("ParseDateStringAsTime failed: dateString <%s>", dateString) + return time.Time{}, fmt.Errorf("ParseDateStringAsTime failed: dateString <%s>", dateString) }