@@ -5,6 +5,7 @@ import * as tar from 'tar-stream'
5
5
import { type Pack } from 'tar-stream'
6
6
import YAML from 'yaml'
7
7
import { type Readable } from 'stream'
8
+ import path from 'path'
8
9
9
10
export interface ActorProfileOptions {
10
11
actorProfile ?: any
@@ -186,12 +187,26 @@ export async function exportActorProfile({
186
187
export async function importActorProfile (
187
188
tarStream : Readable
188
189
) : Promise < Record < string , any > > {
190
+ console . log ( '🚀 Starting to process tar stream...' )
189
191
const extract = tar . extract ( )
190
192
const result : Record < string , any > = { }
191
193
192
194
return await new Promise ( ( resolve , reject ) => {
193
195
extract . on ( 'entry' , ( header , stream , next ) => {
194
- const fileName = header . name
196
+ // Normalize fileName to include only `activitypub/filename`
197
+ const originalFileName = header . name
198
+ const fileName = `activitypub/${ path . basename ( originalFileName ) } `
199
+
200
+ // Skip system-generated files
201
+ if (
202
+ fileName . startsWith ( 'activitypub/._' ) ||
203
+ fileName . endsWith ( '.DS_Store' )
204
+ ) {
205
+ console . warn ( `Skipping system-generated file: ${ fileName } ` )
206
+ next ( )
207
+ }
208
+
209
+ console . log ( `Processing file: ${ fileName } ` )
195
210
let content = ''
196
211
197
212
stream . on ( 'data' , ( chunk ) => {
@@ -202,31 +217,42 @@ export async function importActorProfile(
202
217
try {
203
218
if ( fileName . endsWith ( '.json' ) ) {
204
219
result [ fileName ] = JSON . parse ( content )
220
+ console . log ( 'Parsed JSON file successfully:' , fileName )
205
221
} else if ( fileName . endsWith ( '.yaml' ) || fileName . endsWith ( '.yml' ) ) {
206
222
result [ fileName ] = YAML . parse ( content )
207
223
} else if ( fileName . endsWith ( '.csv' ) ) {
208
224
result [ fileName ] = content
225
+ } else {
226
+ console . warn ( `Unsupported file type: ${ fileName } , skipping...` )
209
227
}
210
228
} catch ( error : any ) {
211
- reject ( new Error ( `Error processing file ${ fileName } : ${ error } ` ) )
229
+ console . error ( `Error processing file ${ fileName } :` , error . message )
230
+ } finally {
231
+ next ( ) // Always continue
212
232
}
213
- next ( )
214
233
} )
215
234
216
235
stream . on ( 'error' , ( error : any ) => {
217
- reject ( new Error ( `Stream error on file ${ fileName } : ${ error } ` ) )
236
+ console . error ( `Stream error on file ${ fileName } :` , error . message )
237
+ next ( ) // Continue even on stream error
218
238
} )
219
239
} )
220
240
221
241
extract . on ( 'finish' , ( ) => {
242
+ console . log ( 'All files processed successfully.' )
222
243
resolve ( result )
223
244
} )
224
245
225
246
extract . on ( 'error' , ( error ) => {
226
- reject ( new Error ( `Error during extraction: ${ error } ` ) )
247
+ console . error ( 'Error during tar extraction:' , error . message )
248
+ reject ( new Error ( 'Failed to extract tar file.' ) )
249
+ } )
250
+
251
+ tarStream . on ( 'error' , ( error ) => {
252
+ console . error ( 'Error in tar stream:' , error . message )
253
+ reject ( new Error ( 'Failed to process tar stream.' ) )
227
254
} )
228
255
229
- // Pipe the ReadableStream into the extractor
230
256
tarStream . pipe ( extract )
231
257
} )
232
258
}
0 commit comments