@@ -43,6 +43,13 @@ define(function (require, exports, module) {
4343 // state manager key, to save the download location of the image
4444 const IMAGE_DOWNLOAD_FOLDER_KEY = "imageGallery.downloadFolder" ;
4545
46+ const DOWNLOAD_EVENTS = {
47+ STARTED : 'downloadStarted' ,
48+ COMPLETED : 'downloadCompleted' ,
49+ CANCELLED : 'downloadCancelled' ,
50+ ERROR : 'downloadError'
51+ } ;
52+
4653 const KernalModeTrust = window . KernalModeTrust ;
4754 if ( ! KernalModeTrust ) {
4855 // integrated extensions will have access to kernal mode, but not external extensions
@@ -686,6 +693,32 @@ define(function (require, exports, module) {
686693 }
687694 }
688695
696+ function _sendDownloadStatusToBrowser ( eventType , data ) {
697+ const currLiveDoc = LiveDevMultiBrowser . getCurrentLiveDoc ( ) ;
698+ if ( currLiveDoc && currLiveDoc . protocol && currLiveDoc . protocol . evaluate ) {
699+ const dataJson = JSON . stringify ( data || { } ) ;
700+ const evalString = `_LD.handleDownloadEvent('${ eventType } ', ${ dataJson } )` ;
701+ currLiveDoc . protocol . evaluate ( evalString ) ;
702+ }
703+ }
704+
705+ function _handleDownloadError ( error , downloadId ) {
706+ console . error ( 'something went wrong while download the image. error:' , error ) ;
707+ if ( downloadId ) {
708+ _sendDownloadStatusToBrowser ( DOWNLOAD_EVENTS . ERROR , { downloadId : downloadId } ) ;
709+ }
710+ }
711+
712+ function _trackDownload ( downloadLocation ) {
713+ if ( ! downloadLocation ) {
714+ return ;
715+ }
716+ fetch ( `https://images.phcode.dev/api/images/download?download_location=${ encodeURIComponent ( downloadLocation ) } ` )
717+ . catch ( error => {
718+ console . error ( 'download tracking failed:' , error ) ;
719+ } ) ;
720+ }
721+
689722 /**
690723 * Helper function to update image src attribute and dismiss ribbon gallery
691724 *
@@ -717,16 +750,18 @@ define(function (require, exports, module) {
717750 * @param {Directory } projectRoot - the project root in which the image is to be saved
718751 */
719752 function _handleUseThisImageLocalFiles ( message , filename , projectRoot ) {
720- const { tagId, imageData } = message ;
753+ const { tagId, imageData, downloadLocation , downloadId } = message ;
721754
722755 const uint8Array = new Uint8Array ( imageData ) ;
723756 const targetPath = projectRoot . fullPath + filename ;
724757
725758 window . fs . writeFile ( targetPath , window . Filer . Buffer . from ( uint8Array ) ,
726759 { encoding : window . fs . BYTE_ARRAY_ENCODING } , ( err ) => {
727760 if ( err ) {
728- console . error ( 'Failed to save image:' , err ) ;
761+ _handleDownloadError ( err , downloadId ) ;
729762 } else {
763+ _trackDownload ( downloadLocation ) ;
764+ _sendDownloadStatusToBrowser ( DOWNLOAD_EVENTS . COMPLETED , { downloadId } ) ;
730765 _updateImageAndDismissRibbon ( tagId , targetPath , filename ) ;
731766 }
732767 } ) ;
@@ -739,7 +774,7 @@ define(function (require, exports, module) {
739774 * @param {Directory } projectRoot - the project root in which the image is to be saved
740775 */
741776 function _handleUseThisImageRemote ( message , filename , projectRoot ) {
742- const { imageUrl, tagId } = message ;
777+ const { imageUrl, tagId, downloadLocation , downloadId } = message ;
743778
744779 fetch ( imageUrl )
745780 . then ( response => {
@@ -755,14 +790,16 @@ define(function (require, exports, module) {
755790 window . fs . writeFile ( targetPath , window . Filer . Buffer . from ( uint8Array ) ,
756791 { encoding : window . fs . BYTE_ARRAY_ENCODING } , ( err ) => {
757792 if ( err ) {
758- console . error ( 'Failed to save image:' , err ) ;
793+ _handleDownloadError ( err , downloadId ) ;
759794 } else {
795+ _trackDownload ( downloadLocation ) ;
796+ _sendDownloadStatusToBrowser ( DOWNLOAD_EVENTS . COMPLETED , { downloadId } ) ;
760797 _updateImageAndDismissRibbon ( tagId , targetPath , filename ) ;
761798 }
762799 } ) ;
763800 } )
764801 . catch ( error => {
765- console . error ( 'Failed to fetch image:' , error ) ;
802+ _handleDownloadError ( error , downloadId ) ;
766803 } ) ;
767804 }
768805
@@ -779,6 +816,10 @@ define(function (require, exports, module) {
779816 return ;
780817 }
781818
819+ if ( message . downloadId ) {
820+ _sendDownloadStatusToBrowser ( DOWNLOAD_EVENTS . STARTED , { downloadId : message . downloadId } ) ;
821+ }
822+
782823 const filename = message . filename ;
783824 const extnName = message . extnName || "jpg" ;
784825
@@ -793,11 +834,17 @@ define(function (require, exports, module) {
793834 // the directory name that user wrote, first check if it exists or not
794835 // if it doesn't exist we create it and then download the image inside it
795836 targetDir . exists ( ( err , exists ) => {
796- if ( err ) { return ; }
837+ if ( err ) {
838+ _handleDownloadError ( err , message . downloadId ) ;
839+ return ;
840+ }
797841
798842 if ( ! exists ) {
799843 targetDir . create ( ( err ) => {
800- if ( err ) { return ; }
844+ if ( err ) {
845+ _handleDownloadError ( err , message . downloadId ) ;
846+ return ;
847+ }
801848 _downloadImageToDirectory ( message , filename , extnName , targetDir ) ;
802849 } ) ;
803850 } else {
@@ -1143,6 +1190,10 @@ define(function (require, exports, module) {
11431190 if ( message ) {
11441191 _downloadToFolder ( message , folderPath ) ;
11451192 }
1193+ } else {
1194+ if ( message && message . downloadId ) {
1195+ _sendDownloadStatusToBrowser ( DOWNLOAD_EVENTS . CANCELLED , { downloadId : message . downloadId } ) ;
1196+ }
11461197 }
11471198 dialog . close ( ) ;
11481199 } ) ;
@@ -1188,7 +1239,7 @@ define(function (require, exports, module) {
11881239 _handleUseThisImageRemote ( message , uniqueFilename , targetDir ) ;
11891240 }
11901241 } ) . catch ( error => {
1191- console . error ( 'Something went wrong when trying to use this image' , error ) ;
1242+ _handleDownloadError ( error , message . downloadId ) ;
11921243 } ) ;
11931244 }
11941245
0 commit comments