Skip to content

Commit

Permalink
fix attachement handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmatthis committed Jan 30, 2024
1 parent 25085b8 commit d08b241
Showing 1 changed file with 48 additions and 21 deletions.
69 changes: 48 additions & 21 deletions src/interfaces/discord/services/chats/discord-attachment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,56 @@ import * as stream from 'stream';
@Injectable()
export class DiscordAttachmentService {
private readonly logger = new Logger(DiscordAttachmentService.name);
constructor(private readonly _openaiAudioService: OpenaiAudioService) {}

private fileHandlerMap: Record<
string,
(tempFilePath: string, attachment: Attachment) => Promise<any>
>;

constructor(private readonly _openaiAudioService: OpenaiAudioService) {
this.fileHandlerMap = {
// Audio types
'.mp3': this.handleAudioAttachment,
'.wav': this.handleAudioAttachment,
'.ogg': this.handleAudioAttachment,
'.flac': this.handleAudioAttachment,
'.m4a': this.handleAudioAttachment,
'.aac': this.handleAudioAttachment,
'.wma': this.handleAudioAttachment,
// Video types
'.mp4': this.handleVideoAttachment,
'.avi': this.handleVideoAttachment,
'.mov': this.handleVideoAttachment,
'.wmv': this.handleVideoAttachment,
'.flv': this.handleVideoAttachment,
'.mkv': this.handleVideoAttachment,
'.webm': this.handleVideoAttachment,
'.mpg': this.handleVideoAttachment,
'.mpeg': this.handleVideoAttachment,
'.m4v': this.handleVideoAttachment,
// ... other file types
'.zip': this.handleZipAttachment,
// Default handler for text files and others
default: this.handleTextAttachment,
};
}

async handleAttachment(attachment: Attachment) {
const tempFilePath = '';
try {
const tempFilePath = await this._downloadAttachment(attachment);
const mimeType = mime.lookup(attachment.name);

if (mimeType?.startsWith('audio/')) {
return await this.handleAudioAttachment(tempFilePath, attachment);
} else if (mimeType?.startsWith('video/')) {
return await this.handleVideoAttachment(attachment);
} else if (path.extname(attachment.name).toLowerCase() === '.zip') {
return await this.handleZipAttachment(attachment);
} else {
// Default to handling as a text file if we don't recognize the file type
return await this.handleTextAttachment(tempFilePath, attachment);
}

// TODO - handle PDF, docx, and other complex text-type attachments
// TODO - handle image attachments -> would need add `OpenaiImageService` `to OpenaiModule`
// TODO - handle other attachments?
// TODO - parse text attachements into json if possible? i.e. .md (by heading/bullet point), .csv, .toml, .yaml, etc
const fileExtension = path.extname(attachment.name).toLowerCase();

const handler =
this.fileHandlerMap[fileExtension] || this.fileHandlerMap['default'];
return await handler.call(this, tempFilePath, attachment);

// TODOs remain the same
} catch (error) {
this.logger.error(`Error handling attachment: ${error}`);
return null;
} finally {
try {
// Clean up temp file, if it's still around
await fs.promises.unlink(tempFilePath);
} catch {}
}
Expand Down Expand Up @@ -112,7 +133,10 @@ export class DiscordAttachmentService {
}
}

private async handleVideoAttachment(attachment: Attachment) {
private async handleVideoAttachment(
tempFilePath: string,
attachment: Attachment,
) {
// Add Video processing logic here - basically, strip the audio and treat it as an audio attachment
this.logger.log('Processing video attachment:', attachment.name);
// Example return format (adjust according to your actual logic)
Expand All @@ -123,7 +147,10 @@ export class DiscordAttachmentService {
};
}

private async handleZipAttachment(attachment: Attachment) {
private async handleZipAttachment(
tempFilePath: string,
attachment: Attachment,
) {
// Add Zip processing logic here - basically, unzip it and process each internal file as a separate attachment
this.logger.log('Processing zip attachment:', attachment.name);
// Example return format (adjust according to your actual logic)
Expand Down

0 comments on commit d08b241

Please sign in to comment.