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"?> <?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4"> <module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager"> <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="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>

View file

@ -90,7 +90,7 @@ func parse(filePath string, probeJSON *FFProbeJSON) (*VideoFile, error) {
fileStat, _ := os.Stat(filePath) fileStat, _ := os.Stat(filePath)
result.Size = fileStat.Size() result.Size = fileStat.Size()
result.StartTime, _ = strconv.ParseFloat(probeJSON.Format.StartTime, 64) result.StartTime, _ = strconv.ParseFloat(probeJSON.Format.StartTime, 64)
result.CreationTime = probeJSON.Format.Tags.CreationTime result.CreationTime = probeJSON.Format.Tags.CreationTime.Time
audioStream := result.GetAudioStream() audioStream := result.GetAudioStream()
if audioStream != nil { if audioStream != nil {

View file

@ -1,7 +1,7 @@
package ffmpeg package ffmpeg
import ( import (
"time" "github.com/stashapp/stash/pkg/models"
) )
type FFProbeJSON struct { type FFProbeJSON struct {
@ -17,11 +17,11 @@ type FFProbeJSON struct {
Size string `json:"size"` Size string `json:"size"`
StartTime string `json:"start_time"` StartTime string `json:"start_time"`
Tags struct { Tags struct {
CompatibleBrands string `json:"compatible_brands"` CompatibleBrands string `json:"compatible_brands"`
CreationTime time.Time `json:"creation_time"` CreationTime models.JSONTime `json:"creation_time"`
Encoder string `json:"encoder"` Encoder string `json:"encoder"`
MajorBrand string `json:"major_brand"` MajorBrand string `json:"major_brand"`
MinorVersion string `json:"minor_version"` MinorVersion string `json:"minor_version"`
} `json:"tags"` } `json:"tags"`
} `json:"format"` } `json:"format"`
Streams []FFProbeStream `json:"streams"` Streams []FFProbeStream `json:"streams"`
@ -76,10 +76,10 @@ type FFProbeStream struct {
StartPts int `json:"start_pts"` StartPts int `json:"start_pts"`
StartTime string `json:"start_time"` StartTime string `json:"start_time"`
Tags struct { Tags struct {
CreationTime time.Time `json:"creation_time"` CreationTime models.JSONTime `json:"creation_time"`
HandlerName string `json:"handler_name"` HandlerName string `json:"handler_name"`
Language string `json:"language"` Language string `json:"language"`
Rotate string `json:"rotate"` Rotate string `json:"rotate"`
} `json:"tags"` } `json:"tags"`
TimeBase string `json:"time_base"` TimeBase string `json:"time_base"`
Width int `json:"width,omitempty"` Width int `json:"width,omitempty"`

View file

@ -3,24 +3,25 @@ package jsonschema
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/stashapp/stash/pkg/models"
"os" "os"
) )
type ScrapedItem struct { type ScrapedItem struct {
Title string `json:"title,omitempty"` Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
URL string `json:"url,omitempty"` URL string `json:"url,omitempty"`
Date string `json:"date,omitempty"` Date string `json:"date,omitempty"`
Rating string `json:"rating,omitempty"` Rating string `json:"rating,omitempty"`
Tags string `json:"tags,omitempty"` Tags string `json:"tags,omitempty"`
Models string `json:"models,omitempty"` Models string `json:"models,omitempty"`
Episode int `json:"episode,omitempty"` Episode int `json:"episode,omitempty"`
GalleryFilename string `json:"gallery_filename,omitempty"` GalleryFilename string `json:"gallery_filename,omitempty"`
GalleryURL string `json:"gallery_url,omitempty"` GalleryURL string `json:"gallery_url,omitempty"`
VideoFilename string `json:"video_filename,omitempty"` VideoFilename string `json:"video_filename,omitempty"`
VideoURL string `json:"video_url,omitempty"` VideoURL string `json:"video_url,omitempty"`
Studio string `json:"studio,omitempty"` Studio string `json:"studio,omitempty"`
UpdatedAt RailsTime `json:"updated_at,omitempty"` UpdatedAt models.JSONTime `json:"updated_at,omitempty"`
} }
func LoadScrapedFile(filePath string) ([]ScrapedItem, error) { 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 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 newScrapedItemJSON.UpdatedAt = updatedAt
t.Scraped = append(t.Scraped, newScrapedItemJSON) 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) { func ParseDateStringAsFormat(dateString string, format string) (string, error) {
t, e := ParseDateStringAsTime(dateString) t, e := ParseDateStringAsTime(dateString)
if t != nil { if e == nil {
return t.Format(format), e return t.Format(format), e
} }
return "", fmt.Errorf("ParseDateStringAsFormat failed: dateString <%s>, format <%s>", dateString, format) 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? // https://stackoverflow.com/a/20234207 WTF?
t, e := time.Parse(time.RFC3339, dateString) t, e := time.Parse(time.RFC3339, dateString)
if e == nil { if e == nil {
return &t, nil return t, nil
} }
t, e = time.Parse("2006-01-02", dateString) t, e = time.Parse("2006-01-02", dateString)
if e == nil { if e == nil {
return &t, nil return t, nil
} }
t, e = time.Parse("2006-01-02 15:04:05", dateString) t, e = time.Parse("2006-01-02 15:04:05", dateString)
if e == nil { if e == nil {
return &t, nil return t, nil
} }
t, e = time.Parse(railsTimeLayout, dateString) t, e = time.Parse(railsTimeLayout, dateString)
if e == nil { 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)
} }