Convert files (PDF, images) to Base64 encoding with MIME type detection for API attachments...
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.
Use this skill when:
.pdf) - application/pdf.png) - image/png.jpg, .jpeg) - image/jpeg.gif) - image/gif.txt) - text/plain.doc, .docx).xls, .xlsx)# Read the file and convert to Base64
base64 /path/to/file.pdf
# Get MIME type from extension
# For .pdf -> application/pdf
# For .png -> image/png
# For .jpg/.jpeg -> image/jpeg
# For .gif -> image/gif
# Check file size in bytes
stat -f%z /path/to/file.pdf # macOS
stat -c%s /path/to/file.pdf # Linux
# Convert to MB for comparison
# 5MB = 5242880 bytes
# Check if file exists and is readable
test -r /path/to/file.pdf && echo "File is readable" || echo "Cannot read file"
# Get file extension
basename /path/to/file.pdf | sed 's/.*\.//'
# 1. Validate file exists
FILE_PATH="/path/to/receipt.pdf"
test -f "$FILE_PATH" || exit 1
# 2. Check file size (must be < 5MB = 5242880 bytes)
FILE_SIZE=$(stat -f%z "$FILE_PATH")
if [ $FILE_SIZE -gt 5242880 ]; then
echo "Error: File size exceeds 5MB limit"
exit 1
fi
# 3. Get filename
FILE_NAME=$(basename "$FILE_PATH")
# 4. Determine MIME type from extension
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
# 5. Convert to Base64
BASE64_DATA=$(base64 "$FILE_PATH")
# 6. Output in format ready for API
echo "Attachment prepared:"
echo "- file_name: $FILE_NAME"
echo "- content_type: $MIME_TYPE"
echo "- data: [Base64 string - ${#BASE64_DATA} characters]"
# Process 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
| Extension | MIME Type |
|---|---|
| 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 |
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 # 5MB
[ $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)"
| 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 |
When using this skill to prepare attachments for FreeAgent:
{
"attachment": {
"data": "base64_encoded_string",
"file_name": "receipt.pdf",
"content_type": "application/pdf",
"description": "Receipt for office supplies"
}
}