mirror of
https://github.com/Dvorinka/excalidraw-full.git
synced 2026-06-03 22:02:57 +00:00
更新项目架构与存储适配器,添加用户认证功能
本次提交包含以下主要更改: 1. 更新 `.gitignore` 文件,添加对 `node_modules` 和环境变量文件的忽略。 2. 修改 `.gitmodules` 文件,替换为新的子模块 `cloudflare-worker`。 3. 新增 `ARCHITECTURE.md` 和 `PROJECT_REFACTOR_PLAN.md` 文档,详细描述项目架构和改造计划。 4. 实现用户认证功能,添加 GitHub OAuth 处理逻辑,支持 JWT 生成与解析。 5. 引入新的存储接口 `CanvasStore`,并实现相应的存储逻辑,支持用户画布的增删改查。 6. 更新 `main.go` 文件,整合新的认证与存储逻辑,优化路由设置。 这些更改旨在提升项目的可扩展性与用户体验,支持多用户环境下的画布管理与存储。
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"excalidraw-complete/core"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/oklog/ulid/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type documentStore struct {
|
||||
basePath string // Directory where documents are stored.
|
||||
}
|
||||
|
||||
func NewDocumentStore(basePath string) core.DocumentStore {
|
||||
if err := os.MkdirAll(basePath, 0755); err != nil {
|
||||
log.Fatalf("failed to create base directory: %v", err)
|
||||
}
|
||||
|
||||
return &documentStore{basePath: basePath}
|
||||
}
|
||||
|
||||
func (s *documentStore) FindID(ctx context.Context, id string) (*core.Document, error) {
|
||||
filePath := filepath.Join(s.basePath, id)
|
||||
log := logrus.WithField("document_id", id)
|
||||
|
||||
log.WithField("file_path", filePath).Info("Retrieving document by ID")
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.WithField("error", "document not found").Warn("Document with specified ID not found")
|
||||
return nil, fmt.Errorf("document with id %s not found", id)
|
||||
}
|
||||
log.WithField("error", err).Error("Failed to retrieve document")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
document := core.Document{
|
||||
Data: *bytes.NewBuffer(data),
|
||||
}
|
||||
|
||||
log.Info("Document retrieved successfully")
|
||||
return &document, nil
|
||||
}
|
||||
|
||||
func (s *documentStore) Create(ctx context.Context, document *core.Document) (string, error) {
|
||||
id := ulid.Make().String()
|
||||
filePath := filepath.Join(s.basePath, id)
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"document_id": id,
|
||||
"file_path": filePath,
|
||||
})
|
||||
log.Info("Creating new document")
|
||||
|
||||
if err := os.WriteFile(filePath, document.Data.Bytes(), 0644); err != nil {
|
||||
log.WithField("error", err).Error("Failed to create document")
|
||||
return "", err
|
||||
}
|
||||
|
||||
log.Info("Document created successfully")
|
||||
return id, nil
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"excalidraw-complete/core"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/oklog/ulid/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type fsStore struct {
|
||||
basePath string
|
||||
}
|
||||
|
||||
// NewStore creates a new filesystem-based store.
|
||||
func NewStore(basePath string) *fsStore {
|
||||
if err := os.MkdirAll(basePath, 0755); err != nil {
|
||||
log.Fatalf("failed to create base directory: %v", err)
|
||||
}
|
||||
return &fsStore{basePath: basePath}
|
||||
}
|
||||
|
||||
// DocumentStore implementation for anonymous sharing
|
||||
func (s *fsStore) FindID(ctx context.Context, id string) (*core.Document, error) {
|
||||
filePath := filepath.Join(s.basePath, id)
|
||||
log := logrus.WithField("document_id", id)
|
||||
|
||||
log.WithField("file_path", filePath).Info("Retrieving document by ID")
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.WithField("error", "document not found").Warn("Document with specified ID not found")
|
||||
return nil, fmt.Errorf("document with id %s not found", id)
|
||||
}
|
||||
log.WithError(err).Error("Failed to retrieve document")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
document := core.Document{
|
||||
Data: *bytes.NewBuffer(data),
|
||||
}
|
||||
|
||||
log.Info("Document retrieved successfully")
|
||||
return &document, nil
|
||||
}
|
||||
|
||||
func (s *fsStore) Create(ctx context.Context, document *core.Document) (string, error) {
|
||||
id := ulid.Make().String()
|
||||
filePath := filepath.Join(s.basePath, id)
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"document_id": id,
|
||||
"file_path": filePath,
|
||||
})
|
||||
log.Info("Creating new document")
|
||||
|
||||
if err := os.WriteFile(filePath, document.Data.Bytes(), 0644); err != nil {
|
||||
log.WithError(err).Error("Failed to create document")
|
||||
return "", err
|
||||
}
|
||||
|
||||
log.Info("Document created successfully")
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// CanvasStore implementation for user-owned canvases
|
||||
func (s *fsStore) getUserCanvasPath(userID string) string {
|
||||
return filepath.Join(s.basePath, userID)
|
||||
}
|
||||
|
||||
func (s *fsStore) List(ctx context.Context, userID string) ([]*core.Canvas, error) {
|
||||
userPath := s.getUserCanvasPath(userID)
|
||||
log := logrus.WithField("user_id", userID).WithField("path", userPath)
|
||||
|
||||
files, err := os.ReadDir(userPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Info("User directory does not exist, returning empty list.")
|
||||
return []*core.Canvas{}, nil
|
||||
}
|
||||
log.WithError(err).Error("Failed to read user directory")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
canvases := make([]*core.Canvas, 0, len(files))
|
||||
for _, file := range files {
|
||||
if !file.IsDir() {
|
||||
info, err := file.Info()
|
||||
if err != nil {
|
||||
log.WithError(err).Warn("Failed to get file info, skipping file")
|
||||
continue
|
||||
}
|
||||
canvas := &core.Canvas{
|
||||
ID: file.Name(),
|
||||
UserID: userID,
|
||||
Name: file.Name(),
|
||||
UpdatedAt: info.ModTime(),
|
||||
}
|
||||
canvases = append(canvases, canvas)
|
||||
}
|
||||
}
|
||||
|
||||
log.Infof("Listed %d canvases", len(canvases))
|
||||
return canvases, nil
|
||||
}
|
||||
|
||||
func (s *fsStore) Get(ctx context.Context, userID, id string) (*core.Canvas, error) {
|
||||
userPath := s.getUserCanvasPath(userID)
|
||||
filePath := filepath.Join(userPath, id)
|
||||
log := logrus.WithFields(logrus.Fields{"user_id": userID, "canvas_id": id, "path": filePath})
|
||||
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Warn("Canvas file not found")
|
||||
return nil, fmt.Errorf("canvas %s not found", id)
|
||||
}
|
||||
log.WithError(err).Error("Failed to read canvas file")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to get file stats")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
canvas := &core.Canvas{
|
||||
ID: id,
|
||||
UserID: userID,
|
||||
Name: id,
|
||||
Data: data,
|
||||
UpdatedAt: info.ModTime(),
|
||||
}
|
||||
|
||||
log.Info("Canvas retrieved successfully")
|
||||
return canvas, nil
|
||||
}
|
||||
|
||||
func (s *fsStore) Save(ctx context.Context, canvas *core.Canvas) error {
|
||||
userPath := s.getUserCanvasPath(canvas.UserID)
|
||||
filePath := filepath.Join(userPath, canvas.ID)
|
||||
log := logrus.WithFields(logrus.Fields{"user_id": canvas.UserID, "canvas_id": canvas.ID, "path": filePath})
|
||||
|
||||
if err := os.MkdirAll(userPath, 0755); err != nil {
|
||||
log.WithError(err).Error("Failed to create user directory")
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("Saving canvas")
|
||||
err := os.WriteFile(filePath, canvas.Data, 0644)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to write canvas file")
|
||||
return err
|
||||
}
|
||||
|
||||
// Set modification time for consistency, though WriteFile usually does this.
|
||||
// We preserve created time logic in the storage layer if needed.
|
||||
now := time.Now()
|
||||
canvas.UpdatedAt = now
|
||||
|
||||
// A full implementation would handle CreatedAt by checking if the file exists first.
|
||||
// For this KV-like store, we'll just update ModTime via WriteFile.
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *fsStore) Delete(ctx context.Context, userID, id string) error {
|
||||
userPath := s.getUserCanvasPath(userID)
|
||||
filePath := filepath.Join(userPath, id)
|
||||
log := logrus.WithFields(logrus.Fields{"user_id": userID, "canvas_id": id, "path": filePath})
|
||||
|
||||
err := os.Remove(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Warn("Canvas file not found for deletion, considered successful.")
|
||||
return nil // If it doesn't exist, the goal is achieved.
|
||||
}
|
||||
log.WithError(err).Error("Failed to delete canvas file")
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("Canvas deleted successfully")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user