Skip to content

Commit

Permalink
add method that allows you to pass a mimetype and some content and it…
Browse files Browse the repository at this point in the history
… will attempt to use the right loader.
  • Loading branch information
HelgeSverre committed Dec 15, 2023
1 parent 88f147c commit cf0a575
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Text/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use HelgeSverre\Extractor\Text\Loaders\Web;
use HelgeSverre\Extractor\Text\Loaders\Word;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;

Expand All @@ -38,6 +39,39 @@ public function create(string $type): TextLoader
};
}

public function fromMime(string $mime, mixed $content): ?TextContent
{
return match (true) {
blank($content) => null,
Str::contains($mime, 'image') => rescue(
callback: fn () => $this->textract($content),
rescue: $this->textractUsingS3Upload($content)
),
Str::contains($mime, 'pdf') => rescue(
callback: fn () => $this->pdf($content),
rescue: $this->textractUsingS3Upload($content)
),
Str::contains($mime, ['xml', 'html']) => $this->html($content),
Str::contains($mime, 'text/plain') => $this->text($content),
Str::contains($mime, 'text/rtf') => $this->rtf($content),

// Not commonly used, but let's use it anyways.
Str::contains($mime, 'text/x-uri') => $this->web($content),

// Stolen from: https://stackoverflow.com/questions/4212861/what-is-a-correct-mime-type-for-docx-pptx-etc
in_array($mime, [
'application/msword',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'application/vnd.ms-word.document.macroEnabled.12',
'application/vnd.ms-word.template.macroEnabled.12',
]) => $this->word($content),

default => $this->textractUsingS3Upload($content)
};
}

// Convenience Methods
public function html(mixed $data): ?TextContent
{
Expand Down

0 comments on commit cf0a575

Please sign in to comment.