@@ -114,38 +114,47 @@ export async function newFolder(vs: typeof vscode, contextItem: ContentItem) {
114114 * @param contextItem - The tree view context item.
115115 */
116116export async function download ( vs : typeof vscode , contextItem : ContentItem ) {
117- if ( contextItem . type !== vs . FileType . File ) {
118- return ;
119- }
117+ let outcome = Outcome . OUTCOME_CANCELLED ;
118+ let downloadedBytes = 0 ;
119+ try {
120+ if ( contextItem . type !== vs . FileType . File ) {
121+ return ;
122+ }
120123
121- const fileName = contextItem . uri . path . split ( '/' ) . pop ( ) ?? 'file' ;
122- const targetUri = await vs . window . showSaveDialog ( {
123- defaultUri : vs . Uri . file ( fileName ) ,
124- title : 'Download File' ,
125- } ) ;
124+ const fileName = contextItem . uri . path . split ( '/' ) . pop ( ) ?? 'file' ;
125+ const targetUri = await vs . window . showSaveDialog ( {
126+ defaultUri : vs . Uri . file ( fileName ) ,
127+ title : 'Download File' ,
128+ } ) ;
126129
127- if ( ! targetUri ) {
128- return ;
129- }
130+ if ( ! targetUri ) {
131+ return ;
132+ }
130133
131- await vs . window . withProgress (
132- {
133- location : vs . ProgressLocation . Notification ,
134- title : `Downloading ${ fileName } ...` ,
135- cancellable : false ,
136- } ,
137- async ( ) => {
138- try {
139- const content = await vs . workspace . fs . readFile ( contextItem . uri ) ;
140- await vs . workspace . fs . writeFile ( targetUri , content ) ;
141- } catch ( err : unknown ) {
142- const msg = err instanceof Error ? err . message : 'unknown error' ;
143- void vs . window . showErrorMessage (
144- `Failed to download ${ fileName } : ${ msg } ` ,
145- ) ;
146- }
147- } ,
148- ) ;
134+ await vs . window . withProgress (
135+ {
136+ location : vs . ProgressLocation . Notification ,
137+ title : `Downloading ${ fileName } ...` ,
138+ cancellable : false ,
139+ } ,
140+ async ( ) => {
141+ try {
142+ const content = await vs . workspace . fs . readFile ( contextItem . uri ) ;
143+ await vs . workspace . fs . writeFile ( targetUri , content ) ;
144+ downloadedBytes = content . byteLength ;
145+ outcome = Outcome . OUTCOME_SUCCEEDED ;
146+ } catch ( err : unknown ) {
147+ const msg = err instanceof Error ? err . message : 'unknown error' ;
148+ void vs . window . showErrorMessage (
149+ `Failed to download ${ fileName } : ${ msg } ` ,
150+ ) ;
151+ outcome = Outcome . OUTCOME_FAILED ;
152+ }
153+ } ,
154+ ) ;
155+ } finally {
156+ telemetry . logDownload ( outcome , downloadedBytes ) ;
157+ }
149158}
150159
151160/**
0 commit comments