@@ -4,15 +4,14 @@ import * as fs from 'fs-extra';
44import * as os from 'os' ;
55import * as path from 'path' ;
66import { Agent } from 'http' ;
7- import * as tmp from 'tmp' ;
87import { AddressInfo , Server , Socket } from 'node:net' ;
98import { PromiseSocket } from 'promise-socket' ;
109import fetch from 'node-fetch' ;
1110import { commands , StatusBarItem , Uri , ViewColumn , Webview , window , workspace , env , WebviewPanelOnDidChangeViewStateEvent , WebviewPanel } from 'vscode' ;
1211
1312import { runTextInTerm } from './rTerminal' ;
1413import { FSWatcher } from 'fs-extra' ;
15- import { config , createWaiterForInvoker , readContent , setContext , UriIcon } from './util' ;
14+ import { config , createTempDir2 , createTempFile , createWaiterForInvoker , readContent , setContext , UriIcon } from './util' ;
1615import { purgeAddinPickerItems , dispatchRStudioAPICall } from './rstudioapi' ;
1716
1817import { IRequest } from './liveShare/shareSession' ;
@@ -383,12 +382,29 @@ export function openExternalBrowser(): void {
383382 }
384383}
385384
386- export async function showWebView ( file : string , title : string , viewer : string | boolean ) : Promise < void > {
385+ export async function showWebView ( file : string , files_content_base64 : Record < string , string > | undefined ,
386+ title : string , viewer : string | boolean ) : Promise < void > {
387387 console . info ( `[showWebView] file: ${ file } , viewer: ${ viewer . toString ( ) } ` ) ;
388388 if ( viewer === false ) {
389389 void env . openExternal ( Uri . file ( file ) ) ;
390390 } else {
391- const dir = path . dirname ( file ) ;
391+ let dir : string ;
392+ if ( files_content_base64 !== undefined ) {
393+ dir = ( await createTempDir2 ( ) ) . path ;
394+ const subdirs = new Set ( Object . keys ( files_content_base64 ) . map ( ( relativePath ) => path . dirname ( relativePath ) ) ) ;
395+ subdirs . delete ( '' ) ;
396+ subdirs . delete ( '.' ) ;
397+ await Promise . all (
398+ Array . from ( subdirs ) . map ( ( subdir ) => fs . mkdir ( path . join ( dir , subdir ) , { recursive : true } ) )
399+ ) ;
400+ await Promise . all ( Object . entries ( files_content_base64 ) . map ( async ( [ realtivePath , contentBase64 ] ) => {
401+ const arrayData = Buffer . from ( contentBase64 , 'base64' ) ;
402+ return fs . writeFile ( path . join ( dir , realtivePath ) , arrayData ) ;
403+ } ) ) ;
404+ file = path . join ( dir , file ) ;
405+ } else {
406+ dir = path . dirname ( file ) ;
407+ }
392408 const webviewDir = extensionContext . asAbsolutePath ( 'html/session/webview/' ) ;
393409 const panel = window . createWebviewPanel ( 'webview' , title ,
394410 {
@@ -945,18 +961,6 @@ function startIncomingRequestServer(sessionStatusBarItem: StatusBarItem) {
945961 return server ;
946962}
947963
948- const create_tmp_file : ( options : tmp . FileOptions ) => Promise < { name : string , fd : number , removeCallback : ( ) => void } > =
949- ( options ) => new Promise ( ( resolve , reject ) => {
950- tmp . file ( options , ( err , name , fd , removeCallback ) => {
951- if ( err ) {
952- reject ( err ) ;
953- } else {
954- resolve ( { name, fd, removeCallback } ) ;
955- }
956- } ) ;
957- }
958- ) ;
959-
960964export async function processRequest ( request : ISessionRequest , socket : Socket | null , sessionStatusBarItem : StatusBarItem ) {
961965 switch ( request . command ) {
962966 case 'help' : {
@@ -1033,7 +1037,7 @@ export async function processRequest(request: ISessionRequest, socket: Socket |
10331037 }
10341038 case 'webview' : {
10351039 if ( request . file && request . title && request . viewer !== undefined ) {
1036- await showWebView ( request . file , request . title , request . viewer ) ;
1040+ await showWebView ( request . file , request . files_content_base64 , request . title , request . viewer ) ;
10371041 }
10381042 break ;
10391043 }
@@ -1051,7 +1055,7 @@ export async function processRequest(request: ISessionRequest, socket: Socket |
10511055 }
10521056
10531057 if ( request . plot_base64 ) {
1054- const { name : filePath , fd } = await create_tmp_file ( { postfix : '.png' } ) ;
1058+ const { name : filePath , fd } = await createTempFile ( { postfix : '.png' } ) ;
10551059 const arrayData = Buffer . from ( request . plot_base64 , 'base64' ) ;
10561060 await fs . writeFile ( fd , arrayData ) ;
10571061 showPlot ( filePath ) ;
0 commit comments