Skip to content

Conversation

@solingerz
Copy link

@solingerz solingerz commented Oct 15, 2025

This commit improves code quality and adds file sharing functionality with proper UTF-8 handling.

1. Authentication Refactoring

Extract duplicate authentication logic into a centralized utility module for better maintainability.

New file: utils/auth.ts

  • AuthConfig interface for consistent auth configuration
  • requireAuth() function with public read mode support
  • requireAuthSimple() for APIs requiring strict authentication
  • PUBLIC_READ_METHODS constant (GET, HEAD, PROPFIND)
  • Proper WWW-Authenticate header handling

Refactored files:

  • functions/api/share.ts
  • functions/webdav/[[path]].ts
  • src/Main.tsx
  • src/app/share.ts

Benefits:

  • Eliminates code duplication (DRY principle)
  • Consistent authentication behavior across all endpoints
  • Easier to maintain and test
  • Better separation of concerns

2. UTF-8 Charset Handling

Fix character encoding issues for text files served via WebDAV.

Changes in functions/webdav/get.ts:

  • Added isTextFile() to detect text files by MIME type and extension
  • Added addUtf8Charset() to append charset=utf-8 to Content-Type headers
  • Added addHtmlCharset() to inject into HTML files
  • Supports common text formats: .txt, .html, .css, .js, .json, .xml, .md, .log, .csv, .ts, .jsx, .tsx, .vue, .py, .java, .c, .cpp, .h, .hpp, .php, .rb, .go, .rs, .swift, .kt, .scala

Changes in src/TextPadDrawer.tsx:

  • Updated Blob and File creation to explicitly specify charset=utf-8

Impact:

  • Ensures proper UTF-8 encoding for all text files
  • Prevents character encoding issues in browsers
  • Improves compatibility with international characters

3. File Sharing Feature

Implement secure file sharing with token-based pre-authorization.

Features:

  • Token-based authentication using Cloudflare KV storage
  • Pre-authorized URLs that bypass normal authentication
  • Secure random token generation with automatic TTL

New files:

  • functions/api/share.ts: API endpoint for creating share tokens
  • functions/s/[token].ts: Token validation and file serving handler
  • src/app/share.ts: Frontend sharing logic and UI components

Integration:

  • Added share button to file manager UI (src/Main.tsx)
  • Copy-to-clipboard functionality for share links
  • Proper error handling and user feedback

Technical details:

  • Uses Cloudflare KV for token storage with TTL support
  • Generates cryptographically secure random tokens
  • Validates tokens and expiration before serving files
  • Integrates with existing authentication system

Files Changed:

  • functions/api/share.ts (new)
  • functions/s/[token].ts (new)
  • functions/webdav/get.ts (modified)
  • functions/webdav/[[path]].ts (modified)
  • src/Main.tsx (modified)
  • src/TextPadDrawer.tsx (modified)
  • src/app/share.ts (new)
  • utils/auth.ts (new)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant