package system import ( "context" "os" "path/filepath" "testing" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gcfg" ) func TestLocalFileURLUsesConfiguredPublicURL(t *testing.T) { t.Setenv("IOTLCM_UPLOAD_PUBLIC_URL", "") previous := g.Cfg().GetAdapter() t.Cleanup(func() { g.Cfg().SetAdapter(previous) }) cases := []struct { name string root string base string file string want string }{ {"本地跨端预览", "resource/upload", "http://localhost:8010/upload/", "resource/upload/000000/user/file/photo.jpeg", "http://localhost:8010/upload/000000/user/file/photo.jpeg"}, {"独立部署前缀", "/data/app/upload", "https://files.example.com/app/upload", "/data/app/upload/000000/user/file/photo.jpeg", "https://files.example.com/app/upload/000000/user/file/photo.jpeg"}, {"Windows共享目录", "F:/shared/upload", "https://files.example.com/upload", "F:/shared/upload/000000/user/file/photo.jpeg", "https://files.example.com/upload/000000/user/file/photo.jpeg"}, } for _, testCase := range cases { t.Run(testCase.name, func(t *testing.T) { adapter, err := gcfg.NewAdapterContent( "system:\n upload:\n local:\n path: " + testCase.root + "\n publicUrl: " + testCase.base + "\n", ) if err != nil { t.Fatal(err) } g.Cfg().SetAdapter(adapter) got := NewSysOss().buildLocalFileURL(context.Background(), testCase.file) if got != testCase.want { t.Fatalf("url = %q, want %q", got, testCase.want) } }) } } func TestLocalFileURLUsesEnvironmentOverride(t *testing.T) { t.Setenv("IOTLCM_UPLOAD_PUBLIC_URL", "http://localhost:8010/upload") previous := g.Cfg().GetAdapter() t.Cleanup(func() { g.Cfg().SetAdapter(previous) }) adapter, err := gcfg.NewAdapterContent("system:\n upload:\n local:\n path: resource/upload\n publicUrl: https://files.example.com/upload\n") if err != nil { t.Fatal(err) } g.Cfg().SetAdapter(adapter) got := NewSysOss().buildLocalFileURL(context.Background(), "resource/upload/000000/photo.jpeg") if got != "http://localhost:8010/upload/000000/photo.jpeg" { t.Fatalf("environment override did not take precedence: %q", got) } } func TestResolveLocalOssPathFromProjectRoot(t *testing.T) { tempDir := t.TempDir() relativePath := filepath.FromSlash("resource/upload/000000/user/file/test.xlsx") realPath := filepath.Join(tempDir, "server", relativePath) if err := os.MkdirAll(filepath.Dir(realPath), 0o755); err != nil { t.Fatalf("mkdir test oss path: %v", err) } if err := os.WriteFile(realPath, []byte("xlsx"), 0o644); err != nil { t.Fatalf("write test oss file: %v", err) } oldWd, err := os.Getwd() if err != nil { t.Fatalf("get wd: %v", err) } if err = os.Chdir(tempDir); err != nil { t.Fatalf("chdir temp root: %v", err) } defer func() { if err := os.Chdir(oldWd); err != nil { t.Fatalf("restore wd: %v", err) } }() got, err := NewSysOss().resolveLocalOssPath(context.Background(), relativePath) if err != nil { t.Fatalf("resolve local oss path: %v", err) } if got != realPath { t.Fatalf("resolved path = %q, want %q", got, realPath) } } func TestAppOssDownloadContentTypeXlsx(t *testing.T) { got := appOssDownloadContentType("resource/upload/test.xlsx", "xlsx") if got != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" { t.Fatalf("content type = %q", got) } } func TestLocalFileURLByMappingWithProductionUploadPath(t *testing.T) { filePath := "/data/app/iotlcmadmin-web-svc/upload/000000/user/file/20260710/sim-card-renew/result/REN-result.xlsx" configValue := "/data/app/iotlcmadmin-web-svc/upload|http://example.com:8008/upload" got, ok := localFileURLByMapping(filePath, configValue) if !ok { t.Fatal("expected production upload path mapping to match") } want := "http://example.com:8008/upload/000000/user/file/20260710/sim-card-renew/result/REN-result.xlsx" if got != want { t.Fatalf("url = %q, want %q", got, want) } } func TestLocalFileURLByBaseWithProductionUploadPath(t *testing.T) { filePath := "/data/app/iotlcmadmin-web-svc/upload/000000/user/file/20260710/sim-card-renew/import/REN-import.xlsx" got, ok := localFileURLByBase(filePath, "/data/app/iotlcmadmin-web-svc/upload", "http://example.com:8008/upload") if !ok { t.Fatal("expected upload path base mapping to match") } want := "http://example.com:8008/upload/000000/user/file/20260710/sim-card-renew/import/REN-import.xlsx" if got != want { t.Fatalf("url = %q, want %q", got, want) } } func TestLocalFileURLByMappingWithDevelopmentUploadPath(t *testing.T) { got, ok := localFileURLByMapping( filepath.FromSlash("resource/upload/000000/user/file/test.xlsx"), "resource/upload|/upload", ) if !ok { t.Fatal("expected development upload path mapping to match") } if got != "/upload/000000/user/file/test.xlsx" { t.Fatalf("url = %q", got) } }