@@ -73,7 +73,9 @@ function start(config, onServerReady) {
7373
7474 app . use ( cookieSession ( {
7575 secret : config . sessionSecret ,
76- key : "code.pyret.org"
76+ key : "code.pyret.org" ,
77+
78+ sameSite : 'lax'
7779 } ) ) ;
7880 app . use ( cookieParser ( ) ) ;
7981
@@ -380,7 +382,6 @@ function start(config, onServerReady) {
380382 var folderId = decodeURIComponent ( parsed . query [ "folderId" ] ) ;
381383
382384 lookForProjectOrCopyStructure ( serverClient , drive , folderId ) . then ( target => {
383- console . log ( "target: " , target ) ;
384385 res . redirect ( `/parley?folder=${ target . projectDir . id } ` ) ;
385386 } ) . catch ( ( err ) => {
386387 console . error ( err ) ;
@@ -432,13 +433,11 @@ function start(config, onServerReady) {
432433 }
433434 } )
434435 . then ( copyResult => {
435- console . log ( "New directory: " , copyResult ) ;
436436 serverDrive . files . list ( {
437437 key : config . google . serverApiKey ,
438438 q : `"${ fileInfo . id } " in parents and not trashed` ,
439439 fields : 'files(id, name, mimeType, modifiedTime, modifiedByMeTime, webContentLink, iconLink, thumbnailLink)' ,
440440 } ) . then ( files => {
441- console . log ( "Directory contents: " , files ) ;
442441 // NOTE(joe): deliberately parallel
443442 Promise . all ( files . data . files . map ( f => copyFileOrDir ( serverDrive , clientDrive , copyResult . data . id , f , serverEmailAddress ) ) )
444443 . then ( copiedFiles => {
@@ -471,7 +470,6 @@ function start(config, onServerReady) {
471470 }
472471 } )
473472 . then ( copiedFile => {
474- console . log ( "File copied: " , copiedFile ) ;
475473 resolve ( copiedFile . data ) ;
476474 } )
477475 } )
@@ -497,11 +495,9 @@ function start(config, onServerReady) {
497495 clientDrive . files . list ( {
498496 q : `properties has {key='${ PROJECT_BACKREF } ' and value='${ fileId } '} and trashed=false`
499497 } ) . then ( files => {
500- console . log ( "Files with key result: " , files ) ;
501498 if ( files . data . files . length === 0 ) {
502499 return serverDrive . files . get ( { fileId, key : config . google . serverApiKey } ) . then ( ( dirInfo ) => {
503500 return copyFileOrDir ( serverDrive , clientDrive , false , dirInfo . data ) . then ( copied => {
504- console . log ( "made a full copy of the directory" ) ;
505501 resolve ( {
506502 copied : true ,
507503 projectDir : copied
@@ -510,7 +506,6 @@ function start(config, onServerReady) {
510506 } ) ;
511507 }
512508 else {
513- console . log ( "Directory existed, so not copying" ) ;
514509 resolve ( {
515510 copied : false ,
516511 projectDir : files . data . files [ 0 ]
@@ -773,70 +768,111 @@ function start(config, onServerReady) {
773768 return ret . promise ;
774769 }
775770
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+
776795 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 ) {
785802 res . status ( 400 ) ;
786- res . send ( "Expected a text file, but got: " + response . headers [ "content-type" ] ) ;
803+ res . send ( "Unable to fetch shared file" ) ;
787804 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+ } ) ;
794818 } ) ;
819+
795820 } ) ;
796821
797822 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+ } ) ;
808838 } ) ;
809839 } ) ;
810840
811841 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+ } ) ;
840876 } ) ;
841877 } ) ;
842878 } ) ;
0 commit comments