Fix json time when unmarshalling

https://github.com/stashapp/stash/issues/25
This commit is contained in:
Stash Dev 2019-03-09 10:14:55 -08:00
parent f57c2bff1d
commit c4d45db30c
8 changed files with 70 additions and 70 deletions

View File

@ -1,7 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/certs" />
<excludeFolder url="file://$MODULE_DIR$/dist" />
<excludeFolder url="file://$MODULE_DIR$/ui/v1/dist" />
<excludeFolder url="file://$MODULE_DIR$/ui/v2/node_modules" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

View File

@ -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 {

View File

@ -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"`

View File

@ -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) {

View File

@ -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
}

View File

@ -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)

30
pkg/models/json_time.go Normal file
View File

@ -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
}

View File

@ -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)
}