@@ -103,6 +103,8 @@ const progress = ref(0);
103103const uploaded = ref (false );
104104const uploadedSize = ref (0 );
105105
106+ const downloadFileUrl = ref (' ' );
107+
106108watch (() => uploaded , (value ) => {
107109 emit (' update:emptiness' , ! value );
108110});
@@ -118,9 +120,50 @@ function uploadGeneratedImage(imgBlob) {
118120 });
119121}
120122
121- onMounted (() => {
123+ onMounted (async () => {
122124 const previewColumnName = ` previewUrl_${props .meta .pluginInstanceId } ` ;
123- if (props .record [previewColumnName ]) {
125+ let queryValues;
126+ try {
127+ queryValues = JSON .parse (atob (route .query .values as string ));
128+ } catch (e ) {
129+ queryValues = {};
130+ }
131+
132+ if (queryValues [props .meta .pathColumnName ]) {
133+ downloadFileUrl .value = queryValues [props .meta .pathColumnName ];
134+
135+ const resp = await callAdminForthApi ({
136+ path: ` /plugin/${props .meta .pluginInstanceId }/get-file-download-url ` ,
137+ method: ' POST' ,
138+ body: {
139+ filePath: queryValues [props .meta .pathColumnName ]
140+ },
141+ });
142+ if (resp .error ) {
143+ adminforth .alert ({
144+ message: t (' Error getting file url for field {field}:' , { field: props .meta .pathColumnName }),
145+ variant: ' danger'
146+ });
147+ return ;
148+ }
149+ const filename = resp .url .split (' /' ).pop ()?.split (' ?' )[0 ] || ` file ` ;
150+ const filenameParts = filename .split (' .' );
151+ const extension = filenameParts .length > 1 ? filenameParts .pop () : ' ' ;
152+ const nameWithoutExt = filenameParts .join (' .' );
153+ const newFileName = extension
154+ ? ` ${nameWithoutExt }_copy_${Date .now ()}.${extension } `
155+ : ` ${filename }_copy_${Date .now ()} ` ;
156+
157+
158+ const res = await fetch (resp .url );
159+ const fileBlob = await res .blob ();
160+ const file = new File ([fileBlob ], newFileName , { type: fileBlob .type });
161+ onFileChange ({
162+ target: {
163+ files: [file ],
164+ },
165+ });
166+ } else if (props .record [previewColumnName ]) {
124167 imgPreview .value = props .record [previewColumnName ];
125168 uploaded .value = true ;
126169 emit (' update:emptiness' , false );
0 commit comments