Make dropbox work with the photos sample (#2745)

* dropbox/find-photos: encode auth token in photo URLs so they can
work in UI.

* Remove requirement for datePublished from photo-index

Dropbox doesn't have a publish date

* fix test

* review comments

* npm test
This commit is contained in:
Aaron Boodman
2016-10-21 14:49:03 -07:00
committed by GitHub
parent cf296b48ca
commit fd7c1cc14d
3 changed files with 48 additions and 32 deletions
+23 -12
View File
@@ -6,6 +6,7 @@ package main
import (
"fmt"
"math"
"os"
"path"
"sync"
@@ -77,20 +78,24 @@ func index() (win bool) {
"h": types.NumberType,
})
photoType := types.MakeStructTypeFromFields("Photo", types.FieldMap{
"sizes": types.MakeMapType(sizeType, types.StringType),
"title": types.StringType,
"datePublished": dateType,
"dateUpdated": dateType,
"id": types.StringType,
"sizes": types.MakeMapType(sizeType, types.StringType),
})
withTags := types.MakeStructTypeFromFields("", types.FieldMap{
"tags": types.MakeSetType(types.StringType),
})
withFaces := types.MakeStructTypeFromFields("", types.FieldMap{
"faces": types.MakeSetType(faceType),
})
withDateTaken := types.MakeStructTypeFromFields("", types.FieldMap{
"dateTaken": dateType,
})
withFaces := types.MakeStructTypeFromFields("", types.FieldMap{
"faces": types.MakeSetType(faceType),
withDatePublished := types.MakeStructTypeFromFields("", types.FieldMap{
"datePublished": dateType,
})
withDateUpdated := types.MakeStructTypeFromFields("", types.FieldMap{
"dateUpdated": dateType,
})
byDate := types.NewGraphBuilder(db, types.MapKind, true)
@@ -106,16 +111,22 @@ func index() (win bool) {
if types.IsSubtype(photoType, cv.Type()) {
s := cv.(types.Struct)
// Prefer to sort by the actual date the photo was taken, but if it's not
// available, use the date it was published instead.
ds := s.Get("datePublished")
// None of the date fields are required, but they are usually available.
var ds types.Value
if types.IsSubtype(withDateTaken, cv.Type()) {
ds = s.Get("dateTaken")
} else if types.IsSubtype(withDatePublished, cv.Type()) {
ds = s.Get("datePublished")
} else if types.IsSubtype(withDateUpdated, cv.Type()) {
ds = s.Get("dateUpdated")
}
// Sort by most recent by negating the timestamp.
d := ds.(types.Struct).Get("nsSinceEpoch").(types.Number)
d = types.Number(-float64(d))
d := types.Number(float64(math.MaxFloat64))
if ds != nil {
// Sort by most recent by negating the timestamp.
d = ds.(types.Struct).Get("nsSinceEpoch").(types.Number)
d = types.Number(-float64(d))
}
// Index by date
byDate.SetInsert([]types.Value{d}, cv)
+2
View File
@@ -37,6 +37,7 @@ func (s *testSuite) TestWin() {
}
type Photo struct {
Id string
Title string
Tags types.Set
Faces types.Set
@@ -72,6 +73,7 @@ func (s *testSuite) TestWin() {
getPhoto := func(n int) Photo {
return Photo{
Id: fmt.Sprintf("photo%d", n),
Title: fmt.Sprintf("photo %d", n),
Tags: getTags(n),
Sizes: map[struct{ Width, Height int }]string{
+23 -20
View File
@@ -23,11 +23,14 @@ import {
const args = argv
.usage(
'Indexes Photo objects out of slurped Dropbox metadata.\n\n' +
'Note that the created objects have download URLs that are ' +
'authenticated by Dropbox. You can request them like:\n\n' +
'curl -H \'Authorization: Bearer <access token>\' <url>\n\n' +
'Usage: node . <in-object> <out-dataset>')
'Indexes Photo objects out of slurped Dropbox metadata.\n' +
'See dropbox/slurp for how to get an access token.\n\n' +
'Usage: node . --access-token=<token> <in-object> <out-dataset>')
.option('access-token', {
describe: 'Dropbox oauth access token',
type: 'string',
demand: true,
})
.demand(2)
.argv;
@@ -131,34 +134,34 @@ function getSizes(input: Object): Map<Struct, string> {
if (resized.scale > 1) {
return null;
}
const args = {
path: input.id,
format: 'jpeg',
size: `w${width}h${height}`,
};
const url = `${contentHost}files/get_thumbnail?arg=` +
encodeURIComponent(JSON.stringify(args));
return [newStruct('', {width: resized.width, height: resized.height}), url];
return [
newStruct('', {width: resized.width, height: resized.height}),
getURL('files/get_thumbnail', {
path: input.id,
format: 'jpeg',
size: `w${width}h${height}`,
}),
];
});
const args = {
path: input.id,
};
const url = `${contentHost}files/download?arg=` +
encodeURIComponent(JSON.stringify(args));
kv.push([
newStruct('', {
width: orig.width,
height: orig.height,
}),
url,
getURL('files/download', {path: input.id}),
]);
// $FlowIssue: Does not understand that filter removes all null values.
return new Map(kv.filter(kv => kv));
}
function getURL(path: string, dbArgs: Object): string {
const dbArgStr = encodeURIComponent(JSON.stringify(dbArgs));
return `${contentHost}${path}?arg=${dbArgStr}&` +
`authorization=Bearer%20${args['access-token']}`;
}
function newDate(iso: string): Struct {
return newStruct('Date', {
nsSinceEpoch: new Date(Date.parse(iso)).getTime() * nanosPerMilli,