@@ -73,7 +73,9 @@ function start(config, onServerReady) {
73
73
74
74
app . use ( cookieSession ( {
75
75
secret : config . sessionSecret ,
76
- key : "code.pyret.org"
76
+ key : "code.pyret.org" ,
77
+
78
+ sameSite : 'lax'
77
79
} ) ) ;
78
80
app . use ( cookieParser ( ) ) ;
79
81
@@ -380,7 +382,6 @@ function start(config, onServerReady) {
380
382
var folderId = decodeURIComponent ( parsed . query [ "folderId" ] ) ;
381
383
382
384
lookForProjectOrCopyStructure ( serverClient , drive , folderId ) . then ( target => {
383
- console . log ( "target: " , target ) ;
384
385
res . redirect ( `/parley?folder=${ target . projectDir . id } ` ) ;
385
386
} ) . catch ( ( err ) => {
386
387
console . error ( err ) ;
@@ -432,13 +433,11 @@ function start(config, onServerReady) {
432
433
}
433
434
} )
434
435
. then ( copyResult => {
435
- console . log ( "New directory: " , copyResult ) ;
436
436
serverDrive . files . list ( {
437
437
key : config . google . serverApiKey ,
438
438
q : `"${ fileInfo . id } " in parents and not trashed` ,
439
439
fields : 'files(id, name, mimeType, modifiedTime, modifiedByMeTime, webContentLink, iconLink, thumbnailLink)' ,
440
440
} ) . then ( files => {
441
- console . log ( "Directory contents: " , files ) ;
442
441
// NOTE(joe): deliberately parallel
443
442
Promise . all ( files . data . files . map ( f => copyFileOrDir ( serverDrive , clientDrive , copyResult . data . id , f , serverEmailAddress ) ) )
444
443
. then ( copiedFiles => {
@@ -471,7 +470,6 @@ function start(config, onServerReady) {
471
470
}
472
471
} )
473
472
. then ( copiedFile => {
474
- console . log ( "File copied: " , copiedFile ) ;
475
473
resolve ( copiedFile . data ) ;
476
474
} )
477
475
} )
@@ -497,11 +495,9 @@ function start(config, onServerReady) {
497
495
clientDrive . files . list ( {
498
496
q : `properties has {key='${ PROJECT_BACKREF } ' and value='${ fileId } '} and trashed=false`
499
497
} ) . then ( files => {
500
- console . log ( "Files with key result: " , files ) ;
501
498
if ( files . data . files . length === 0 ) {
502
499
return serverDrive . files . get ( { fileId, key : config . google . serverApiKey } ) . then ( ( dirInfo ) => {
503
500
return copyFileOrDir ( serverDrive , clientDrive , false , dirInfo . data ) . then ( copied => {
504
- console . log ( "made a full copy of the directory" ) ;
505
501
resolve ( {
506
502
copied : true ,
507
503
projectDir : copied
@@ -510,7 +506,6 @@ function start(config, onServerReady) {
510
506
} ) ;
511
507
}
512
508
else {
513
- console . log ( "Directory existed, so not copying" ) ;
514
509
resolve ( {
515
510
copied : false ,
516
511
projectDir : files . data . files [ 0 ]
@@ -773,70 +768,111 @@ function start(config, onServerReady) {
773
768
return ret . promise ;
774
769
}
775
770
771
+ // NOTE(joe): this is a hack for making share URLs work on another server.
772
+ // The config variable here shouldn't be set to the same URL as the deployment
773
+ // URL (since this will just make self request). But since Google credentials
774
+ // lock in files to a particular server, and we want share links from CPO
775
+ // to work on our testing or other deployments, we need this.
776
+ // If set, we first try to fetch from the indicated shared server (usually
777
+ // this will be code.pyret.org).
778
+ // This in fact doesn't matter *that* much for embedding clients that embed
779
+ // code.pyret.org directly. However, for *other* deployments, or test
780
+ // deployments, or development copies, it saves a ton of headaches.
781
+ function sharedPrefetch ( url ) {
782
+ var ret = Q . defer ( ) ;
783
+ if ( config . sharedFetchServer ) {
784
+ let response = request ( { url : url } ) ;
785
+ response . on ( "error" , ( error ) => { ret . reject ( error ) ; } ) ;
786
+ response . on ( "response" , ( resp ) => ret . resolve ( response ) ) ;
787
+ }
788
+ else {
789
+ ret . reject ( "No fallback server configured" ) ;
790
+ }
791
+
792
+ return ret . promise ;
793
+ }
794
+
776
795
app . get ( "/shared-program-contents" , function ( req , res ) {
777
- var contents = getSharedContents ( req . query . sharedProgramId ) ;
778
- contents . fail ( function ( err ) {
779
- res . status ( 400 ) ;
780
- res . send ( "Unable to fetch shared file" ) ;
781
- res . end ( ) ;
782
- } ) ;
783
- contents . then ( function ( response ) {
784
- if ( ! response . headers [ 'content-type' ] === "text/plain" ) {
796
+ const requestURL = `${ config . sharedFetchServer } /shared-program-contents?sharedProgramId=${ req . query . sharedProgramId } ` ;
797
+ const shared = sharedPrefetch ( requestURL ) ;
798
+ shared . then ( ( resp ) => resp . pipe ( res ) ) ;
799
+ shared . fail ( ( ) => {
800
+ var contents = getSharedContents ( req . query . sharedProgramId ) ;
801
+ contents . fail ( function ( err ) {
785
802
res . status ( 400 ) ;
786
- res . send ( "Expected a text file, but got: " + response . headers [ "content-type" ] ) ;
803
+ res . send ( "Unable to fetch shared file" ) ;
787
804
res . end ( ) ;
788
- }
789
- else {
790
- response
791
- . on ( "response" , ( r ) => r . headers [ "content-disposition" ] = `inline; filename="${ req . query . sharedProgramId } "` )
792
- . pipe ( res ) ;
793
- }
805
+ } ) ;
806
+ contents . then ( function ( response ) {
807
+ if ( ! response . headers [ 'content-type' ] === "text/plain" ) {
808
+ res . status ( 400 ) ;
809
+ res . send ( "Expected a text file, but got: " + response . headers [ "content-type" ] ) ;
810
+ res . end ( ) ;
811
+ }
812
+ else {
813
+ response
814
+ . on ( "response" , ( r ) => r . headers [ "content-disposition" ] = `inline; filename="${ req . query . sharedProgramId } "` )
815
+ . pipe ( res ) ;
816
+ }
817
+ } ) ;
794
818
} ) ;
819
+
795
820
} ) ;
796
821
797
822
app . get ( "/shared-image-contents" , function ( req , res ) {
798
- var contents = getSharedContents ( req . query . sharedImageId ) ;
799
- contents . then ( function ( response ) {
800
- response
801
- . on ( "response" , ( r ) => r . headers [ "content-disposition" ] = `inline; filename="${ req . query . sharedImageId } "` )
802
- . pipe ( res ) ;
803
- } )
804
- . fail ( function ( err ) {
805
- res . status ( 400 ) ;
806
- res . send ( "Could not access shared file, or shared file was not an image." ) ;
807
- res . end ( ) ;
823
+ const requestURL = `${ config . sharedFetchServer } /shared-image-contents?sharedImageId=${ req . query . sharedImageId } ` ;
824
+ const shared = sharedPrefetch ( requestURL ) ;
825
+ shared . then ( ( resp ) => resp . pipe ( res ) ) ;
826
+ shared . fail ( ( ) => {
827
+ var contents = getSharedContents ( req . query . sharedImageId ) ;
828
+ contents . then ( function ( response ) {
829
+ response
830
+ . on ( "response" , ( r ) => r . headers [ "content-disposition" ] = `inline; filename="${ req . query . sharedImageId } "` )
831
+ . pipe ( res ) ;
832
+ } )
833
+ . fail ( function ( err ) {
834
+ res . status ( 400 ) ;
835
+ res . send ( "Could not access shared file, or shared file was not an image." ) ;
836
+ res . end ( ) ;
837
+ } ) ;
808
838
} ) ;
809
839
} ) ;
810
840
811
841
app . get ( "/shared-file" , function ( req , res ) {
812
- var sharedProgramId = req . query . sharedProgramId ;
813
- var both = fileAndToken ( sharedProgramId ) ;
814
- both . fail ( function ( err ) {
815
- console . error ( err ) ;
816
- res . status ( 404 ) . send ( "No share information found for " + sharedProgramId ) ;
817
- res . end ( ) ;
818
- } ) ;
819
- both . then ( function ( both ) {
820
- var prog = both [ 0 ] ;
821
- var refreshToken = both [ 1 ] ;
822
- auth . refreshAccess ( refreshToken , function ( err , newToken ) {
823
- if ( err ) { res . status ( 403 ) . send ( "Couldn't access shared file " + sharedProgramId ) ; res . end ( ) ; return ; }
824
- else {
825
- var drive = getDriveClient ( newToken , 'v2' ) ;
826
- drive . files . get ( { fileId : sharedProgramId } , function ( err , response ) {
827
- if ( err ) { res . status ( 400 ) . send ( "Couldn't access shared file " + sharedProgramId ) ; }
828
- else {
829
- res . send ( {
830
- id : response . data . id ,
831
- modifiedDate : response . data . modifiedDate ,
832
- title : response . data . title ,
833
- selfLink : response . data . selfLink
834
- } ) ;
835
- res . status ( 200 ) ;
836
- res . end ( ) ;
837
- }
838
- } ) ;
839
- }
842
+ const requestURL = `${ config . sharedFetchServer } /shared-file?sharedProgramId=${ req . query . sharedProgramId } ` ;
843
+ const shared = sharedPrefetch ( requestURL ) ;
844
+ shared . then ( ( resp ) => resp . pipe ( res ) ) ;
845
+ shared . fail ( ( e ) => {
846
+ console . error ( "Fallback failed: " , e ) ;
847
+ var sharedProgramId = req . query . sharedProgramId ;
848
+ var both = fileAndToken ( sharedProgramId ) ;
849
+ both . fail ( function ( err ) {
850
+ console . error ( err ) ;
851
+ res . status ( 404 ) . send ( "No share information found for " + sharedProgramId ) ;
852
+ res . end ( ) ;
853
+ } ) ;
854
+ both . then ( function ( both ) {
855
+ var prog = both [ 0 ] ;
856
+ var refreshToken = both [ 1 ] ;
857
+ auth . refreshAccess ( refreshToken , function ( err , newToken ) {
858
+ if ( err ) { res . status ( 403 ) . send ( "Couldn't access shared file " + sharedProgramId ) ; res . end ( ) ; return ; }
859
+ else {
860
+ var drive = getDriveClient ( newToken , 'v2' ) ;
861
+ drive . files . get ( { fileId : sharedProgramId } , function ( err , response ) {
862
+ if ( err ) { res . status ( 400 ) . send ( "Couldn't access shared file " + sharedProgramId ) ; }
863
+ else {
864
+ res . send ( {
865
+ id : response . data . id ,
866
+ modifiedDate : response . data . modifiedDate ,
867
+ title : response . data . title ,
868
+ selfLink : response . data . selfLink
869
+ } ) ;
870
+ res . status ( 200 ) ;
871
+ res . end ( ) ;
872
+ }
873
+ } ) ;
874
+ }
875
+ } ) ;
840
876
} ) ;
841
877
} ) ;
842
878
} ) ;
0 commit comments