implemented last modified

This commit is contained in:
Djeeberjr 2021-08-06 19:25:07 +02:00
parent da5d597b3f
commit 31e4586d68
3 changed files with 62 additions and 14 deletions

View File

@ -73,11 +73,12 @@ func getFilesBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
// TODO: how to handle? // TODO: how to handle?
} else if !strings.HasSuffix(obj.Key, "/") { } else if !strings.HasSuffix(obj.Key, "/") {
files = append(files, File{ files = append(files, File{
ID: obj.Key, ID: obj.Key,
Name: filepath.Base(obj.Key), Name: filepath.Base(obj.Key),
Size: obj.Size, Size: obj.Size,
ContentType: obj.ContentType, ContentType: obj.ContentType,
ETag: obj.ETag, ETag: obj.ETag,
LastModified: obj.LastModified,
}) })
} }
} }
@ -112,10 +113,11 @@ func getFileBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
} else { } else {
results = append(results, &dataloader.Result{ results = append(results, &dataloader.Result{
Data: &File{ Data: &File{
ID: obj.Key, ID: obj.Key,
Size: obj.Size, Size: obj.Size,
ContentType: obj.ContentType, ContentType: obj.ContentType,
ETag: obj.ETag, ETag: obj.ETag,
LastModified: obj.LastModified,
}, },
Error: nil, Error: nil,
}) })

View File

@ -4,9 +4,11 @@ import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/graph-gophers/dataloader" "github.com/graph-gophers/dataloader"
"github.com/graphql-go/graphql" "github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
) )
var graphqlDirType *graphql.Object var graphqlDirType *graphql.Object
@ -14,6 +16,38 @@ var graphqlFileType *graphql.Object
// graphqlTypes create all graphql types and stores the in the global variables // graphqlTypes create all graphql types and stores the in the global variables
func graphqlTypes() { func graphqlTypes() {
var dateTimeType = graphql.NewScalar(graphql.ScalarConfig{
Name: "DateTime",
Description: "DateTime is a DateTime in ISO 8601 format",
Serialize: func(value interface{}) interface{} {
switch value := value.(type) {
case time.Time:
return value.Format(time.RFC3339)
}
return "INVALID"
},
ParseValue: func(value interface{}) interface{} {
switch tvalue := value.(type) {
case string:
if tval, err := time.Parse(time.RFC3339, tvalue); err != nil {
return nil
} else {
return tval
}
}
return nil
},
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return valueAST.Value
}
return nil
},
})
graphqlDirType = graphql.NewObject(graphql.ObjectConfig{ graphqlDirType = graphql.NewObject(graphql.ObjectConfig{
Name: "Directory", Name: "Directory",
Description: "Represents a directory", Description: "Represents a directory",
@ -87,6 +121,17 @@ func graphqlTypes() {
return file.ETag, nil return file.ETag, nil
}, },
}, },
"lastModified": &graphql.Field{
Type: dateTimeType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
file, err := loadFile(p)
if err != nil {
return nil, err
}
return file.LastModified, nil
},
},
"parent": &graphql.Field{ "parent": &graphql.Field{
Type: graphqlDirType, Type: graphqlDirType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) { Resolve: func(p graphql.ResolveParams) (interface{}, error) {

View File

@ -22,11 +22,12 @@ type AppConfig struct {
// File represents a file with its metadata // File represents a file with its metadata
type File struct { type File struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Size int64 `json:"size"` Size int64 `json:"size"`
ContentType string `json:"contentType"` ContentType string `json:"contentType"`
ETag string `json:"etag"` ETag string `json:"etag"`
LastModified time.Time `json:"lastModified"`
} }
// Directory represents a directory with its metadata // Directory represents a directory with its metadata