| name | file-to-base64 |
| description | Convert files (PDF, images) to Base64 encoding with MIME type detection for API attachments. Use when you need to prepare file attachments for the FreeAgent API or any other API that requires Base64-encoded files. |
File to Base64 Converter
This skill converts files to Base64-encoded strings and detects their MIME types, which is essential for attaching files to FreeAgent expenses, timeslips, and other API operations that require file uploads.
When to Use This Skill
Use this skill when:
- Preparing attachments for FreeAgent API calls (expenses, timeslips)
- Converting PDFs or images to Base64 format
- Detecting MIME types for file uploads
- Validating file formats and sizes for API requirements
Supported File Formats
FreeAgent Supported Formats
- PDF (
.pdf) - application/pdf
- PNG (
.png) - image/png
- JPEG (
.jpg, .jpeg) - image/jpeg
- GIF (
.gif) - image/gif
Additional Common Formats (for reference)
- Text files (
.txt) - text/plain
- Word documents (
.doc, .docx)
- Excel files (
.xls, .xlsx)
File Size Limits
- FreeAgent API: Maximum 5MB per file
How to Use
1. Convert a Single File
base64 /path/to/file.pdf
2. Check File Size
stat -f%z /path/to/file.pdf
stat -c%s /path/to/file.pdf
3. Validate File Before Conversion
test -r /path/to/file.pdf && echo "File is readable" || echo "Cannot read file"
basename /path/to/file.pdf | sed 's/.*\.//'
Example Workflow
Prepare an Attachment for FreeAgent Expense
FILE_PATH="/path/to/receipt.pdf"
test -f "$FILE_PATH" || exit 1
FILE_SIZE=$(stat -f%z "$FILE_PATH")
if [ $FILE_SIZE -gt 5242880 ]; then
echo "Error: File size exceeds 5MB limit"
exit 1
fi
FILE_NAME=$(basename "$FILE_PATH")
EXT="${FILE_PATH##*.}"
case "$EXT" in
pdf) MIME_TYPE="application/pdf" ;;
png) MIME_TYPE="image/png" ;;
jpg|jpeg) MIME_TYPE="image/jpeg" ;;
gif) MIME_TYPE="image/gif" ;;
*) echo "Unsupported format"; exit 1 ;;
esac
BASE64_DATA=$(base64 "$FILE_PATH")
echo "Attachment prepared:"
echo "- file_name: $FILE_NAME"
echo "- content_type: $MIME_TYPE"
echo "- data: [Base64 string - ${#BASE64_DATA} characters]"
Multiple Files
for FILE in /path/to/receipts/*.pdf; do
if [ -f "$FILE" ]; then
SIZE=$(stat -f%z "$FILE")
if [ $SIZE -le 5242880 ]; then
BASE64=$(base64 "$FILE")
echo "File: $(basename "$FILE") - Ready"
else
echo "File: $(basename "$FILE") - Too large"
fi
fi
done
MIME Type Reference
| Extension | MIME Type |
|---|
| .pdf | application/pdf |
| .png | image/png |
| .jpg, .jpeg | image/jpeg |
| .gif | image/gif |
| .txt | text/plain |
| .doc | application/msword |
| .docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
| .xls | application/vnd.ms-excel |
| .xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
Error Handling
Common Issues
-
File not found
test -f "$FILE_PATH" || echo "Error: File does not exist"
-
File too large
SIZE=$(stat -f%z "$FILE_PATH")
MAX_SIZE=5242880
[ $SIZE -gt $MAX_SIZE ] && echo "Error: File exceeds 5MB limit"
-
Unsupported format
EXT="${FILE_PATH##*.}"
case "$EXT" in
pdf|png|jpg|jpeg|gif) echo "Supported" ;;
*) echo "Error: Unsupported format. Use: pdf, png, jpg, jpeg, gif" ;;
esac
-
Permission denied
test -r "$FILE_PATH" || echo "Error: Cannot read file (check permissions)"
Tips for Large Files
- Compress images before converting (use ImageMagick, etc.)
- For PDFs, ensure they're optimized/compressed
- Consider resizing images to reasonable dimensions
- Remove unnecessary metadata from images
Platform Differences
macOS vs Linux Commands
| Task | macOS | Linux |
|---|
| File size | stat -f%z file | stat -c%s file |
| Base64 encode | base64 file | base64 file |
| Base64 (no wrap) | base64 file | base64 -w 0 file |
Integration with FreeAgent API
When using this skill to prepare attachments for FreeAgent:
- Convert file to Base64 using this skill
- Detect MIME type from extension
- Include in API payload as:
{
"attachment": {
"data": "base64_encoded_string",
"file_name": "receipt.pdf",
"content_type": "application/pdf",
"description": "Receipt for office supplies"
}
}
Notes
- Base64 encoding increases file size by approximately 33%
- Ensure you have enough memory for large file conversions
- For very large batches, consider processing files in chunks
- Always validate files before attempting conversion