# Apache Configuration for Large File Downloads
# Optimized for APK downloads up to 100MB

# Timeout settings for large file downloads
Timeout 600
KeepAliveTimeout 600

# Enable compression for non-binary content only
<LocationMatch "^(?!.*\.(apk|zip|exe|dmg))">
    SetOutputFilter DEFLATE
</LocationMatch>

# Disable compression for APK files to improve download performance
<LocationMatch "\.apk$">
    SetEnv no-gzip 1
    SetEnv no-brotli 1
</LocationMatch>

# APK file handling optimization
<FilesMatch "\.apk$">
    # Prevent caching of APK files
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
    
    # Set proper MIME type
    AddType application/vnd.android.package-archive .apk
    
    # Enable range requests for resume support
    Header set Accept-Ranges "bytes"
    
    # Keep connections alive for large downloads
    Header set Connection "keep-alive"
    Header set Keep-Alive "timeout=600, max=1"
</FilesMatch>

# Rate limiting for downloads (optional - adjust as needed)
<LocationMatch "/api/app-update\.php">
    # Allow up to 5 concurrent downloads per IP
    SetEnvIf Remote_Addr "^(.*)$" CLIENT_IP=$1
    BrowserMatchNoCase ".*" CLIENT_IP=$1
</LocationMatch>

# Installation Instructions:
# 1. Copy this file content to /etc/apache2/sites-available/download-optimization.conf
# 2. Enable the configuration: sudo a2ensite download-optimization
# 3. Enable required modules: sudo a2enmod headers expires
# 4. Restart Apache: sudo systemctl restart apache2