diff --git a/.github/workflows/_buildx.yml b/.github/workflows/_buildx.yml index 4677faceb..224c674e1 100644 --- a/.github/workflows/_buildx.yml +++ b/.github/workflows/_buildx.yml @@ -23,10 +23,10 @@ jobs: mv binaries/shiori_linux_amd64_v1 binaries/shiori_linux_amd64 gzip -d -S binaries/.gz__ -r . chmod 755 binaries/shiori_linux_*/shiori - - name: Buildx - working-directory: .github/workflows/docker + - name: Prepare master push tags + if: ${{ startsWith(github.ref, 'refs/tags/v') }} run: | - echo "${{ secrets.GITHUB_TOKEN }}" | docker login -u "${{ github.repository_owner }}" --password-stdin ghcr.io + echo "tag_flags=--tag ${{ github.ref }}" >> $GITHUB_ENV REPO=ghcr.io/${{ github.repository }} TAG=$(git describe --tags) if [ -z "$(git tag --points-at HEAD)" ] @@ -35,5 +35,19 @@ jobs: else TAG2="latest" fi + echo "tag_flags=--tag $REPO:$TAG --tag $REPO:$TAG2" >> $GITHUB_ENV + + - name: Prepare pull request tags + if: github.event_name == 'pull_request' + run: | + echo "tag_flags=--tag ${{ github.ref }}" >> $GITHUB_ENV + REPO=ghcr.io/${{ github.repository }} + echo "tag_flags=--tag $REPO:pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV + + - name: Buildx + working-directory: .github/workflows/docker + run: | + set -x + echo "${{ secrets.GITHUB_TOKEN }}" | docker login -u "${{ github.repository_owner }}" --password-stdin ghcr.io docker buildx create --use --name builder - docker buildx build -f Dockerfile.ci --platform=linux/amd64,arm64,linux/arm/v7 --push --output=type=registry --tag $REPO:$TAG --tag $REPO:$TAG2 . + docker buildx build -f Dockerfile.ci --platform=linux/amd64,arm64,linux/arm/v7 --push ${{ env.tag_flags }} . diff --git a/.github/workflows/_delete-registry-tag.yml b/.github/workflows/_delete-registry-tag.yml new file mode 100644 index 000000000..665090ea9 --- /dev/null +++ b/.github/workflows/_delete-registry-tag.yml @@ -0,0 +1,14 @@ +name: Delete registry tag + +on: workflow_call + +jobs: + purge-image: + name: Delete tag + runs-on: ubuntu-latest + steps: + - uses: chipkent/action-cleanup-package@1316a66015b82d745b57acbb6c570f2bb1d108f9 # v1.0.3 + with: + package-name: ${{ github.event.repository.name }} + tag: ${{ env.TAG_NAME }} + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/docker/Dockerfile.ci b/.github/workflows/docker/Dockerfile.ci index 44120f63c..31c206aff 100644 --- a/.github/workflows/docker/Dockerfile.ci +++ b/.github/workflows/docker/Dockerfile.ci @@ -8,4 +8,4 @@ WORKDIR /shiori EXPOSE 8080 ENV SHIORI_DIR=/shiori ENTRYPOINT ["/usr/bin/shiori"] -CMD ["serve"] \ No newline at end of file +CMD ["server"] diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index cf9213027..5818fa905 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -2,7 +2,8 @@ name: 'Pull Request' on: pull_request: - branches: master + branches: + - master concurrency: group: ci-tests-${{ github.ref }}-1 @@ -13,3 +14,9 @@ jobs: uses: ./.github/workflows/_golangci-lint.yml call-test: uses: ./.github/workflows/_test.yml + call-gorelease: + needs: [call-lint, call-test] + uses: ./.github/workflows/_gorelease.yml + call-buildx: + needs: call-gorelease + uses: ./.github/workflows/_buildx.yml diff --git a/.github/workflows/pull_request_closed.yml b/.github/workflows/pull_request_closed.yml new file mode 100644 index 000000000..2cb0f27ee --- /dev/null +++ b/.github/workflows/pull_request_closed.yml @@ -0,0 +1,12 @@ +name: 'Clean up Docker images from PR' + +on: + pull_request: + types: + - closed + +jobs: + delete-tag: + uses: ./.github/workflows/_delete-registry-tag.yml + with: + TAG_NAME: pr-${{ github.event.pull_request.number }} diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 6f024f03f..ef9967a42 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -15,6 +15,7 @@ jobs: call-test: uses: ./.github/workflows/_test.yml call-gorelease: + needs: [call-lint, call-test] uses: ./.github/workflows/_gorelease.yml call-buildx: needs: call-gorelease diff --git a/.github/workflows/version_bump.yml b/.github/workflows/version_bump.yml index 115f8cab5..725f2156a 100644 --- a/.github/workflows/version_bump.yml +++ b/.github/workflows/version_bump.yml @@ -6,6 +6,11 @@ on: version: description: "Version to bump to, example: v1.5.2" required: true + ref: + description: "Ref to release from" + required: true + type: string + default: master jobs: tag-release: @@ -18,7 +23,7 @@ jobs: uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3.4.0 with: fetch-depth: 0 - ref: master + ref: ${{ inputs.ref }} - name: Tag release run: | git config user.email "${{github.repository_owner}}@users.noreply.github.com" diff --git a/Dockerfile b/Dockerfile index 613dd4efc..09f640d95 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,4 +16,4 @@ WORKDIR /shiori EXPOSE 8080 ENV SHIORI_DIR /shiori/ ENTRYPOINT ["/usr/bin/shiori"] -CMD ["serve"] +CMD ["server"] diff --git a/Dockerfile.compose b/Dockerfile.compose index 7d7da2ab5..f6d077cc2 100644 --- a/Dockerfile.compose +++ b/Dockerfile.compose @@ -3,4 +3,4 @@ FROM docker.io/golang:1.19-alpine3.16 WORKDIR /src/shiori ENTRYPOINT ["go", "run", "main.go"] -CMD ["serve"] +CMD ["server"] diff --git a/README.md b/README.md index 02c0bb217..231e6579c 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![IC](https://github.com/go-shiori/shiori/actions/workflows/push.yml/badge.svg?branch=master)](https://github.com/go-shiori/shiori/actions/workflows/push.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/go-shiori/shiori)](https://goreportcard.com/report/github.com/go-shiori/shiori) [![#shiori@libera.chat](https://img.shields.io/badge/irc-%23shiori-orange)](https://web.libera.chat/#shiori) -[![#shiori-general:matrix.org](https://img.shields.io/badge/matrix-%23shiori-orange)](https://matrix.to/#/#shiori-general:matrix.org) +[![#shiori-general:matrix.org](https://img.shields.io/badge/matrix-%23shiori-orange)](https://matrix.to/#/#shiori:matrix.org) [![Containers](https://img.shields.io/static/v1?label=Container&message=Images&color=1488C6&logo=docker)](https://github.com/go-shiori/shiori/pkgs/container/shiori) **Check out our latest [Announcements](https://github.com/go-shiori/shiori/discussions/categories/announcements)** diff --git a/docs/Storage.md b/docs/Storage.md new file mode 100644 index 000000000..5b1377daf --- /dev/null +++ b/docs/Storage.md @@ -0,0 +1,14 @@ +# Storage + +Shiori requires a folder to store several pieces of data, such as the bookmark archives, thumbnails, ebooks, and others. If the database engine used is sqlite, then the database file will also be stored in this folder. + +You can specify the storage folder by using `--storage-dir` or `--portable` flags when running Shiori. + +If none specified, Shiori will try to find the correct app folder for your OS. + +For example: +- In Windows, Shiori will use `%APPDATA%`. +- In Linux, it will use `$XDG_CONFIG_HOME` or `$HOME/.local/share` if `$XDG_CONFIG_HOME` is not set. +- In macOS, it will use `$HOME/Library/Application Support`. + +> For more and up to date information about app folder discovery check [muesli/go-app-paths](https://github.com/muesli/go-app-paths) diff --git a/docs/index.md b/docs/index.md index eb6fee64c..295d60024 100644 --- a/docs/index.md +++ b/docs/index.md @@ -4,11 +4,12 @@ Shiori is a simple bookmarks manager written in Go language. Intended as a simpl ## Resources -- [API](./API.md) +- [API](./API.md) (Deprecated) - [APIv1](./APIv1.md) ([What is this?](https://github.com/go-shiori/shiori/issues/640)) - [Contributing](./Contribute.md) - [Configuration](./Configuration.md) - [FAQ](./Frequently-Asked-Question.md) - [Installation](./Installation.md) - [Screenshots](./screenshots/) +- [Storage](./Storage.md) - [Usage](./Usage.md) diff --git a/internal/cmd/add.go b/internal/cmd/add.go index 80ccab9e1..2c20fe34e 100644 --- a/internal/cmd/add.go +++ b/internal/cmd/add.go @@ -29,6 +29,8 @@ func addCmd() *cobra.Command { } func addHandler(cmd *cobra.Command, args []string) { + cfg, deps := initShiori(cmd.Context(), cmd) + // Read flag and arguments url := args[0] title, _ := cmd.Flags().GetString("title") @@ -70,7 +72,7 @@ func addHandler(cmd *cobra.Command, args []string) { } // Save bookmark to database - books, err := db.SaveBookmarks(cmd.Context(), true, book) + books, err := deps.Database.SaveBookmarks(cmd.Context(), true, book) if err != nil { cError.Printf("Failed to save bookmark: %v\n", err) os.Exit(1) @@ -90,7 +92,7 @@ func addHandler(cmd *cobra.Command, args []string) { if err == nil && content != nil { request := core.ProcessRequest{ - DataDir: dataDir, + DataDir: cfg.Storage.DataDir, Bookmark: book, Content: content, ContentType: contentType, @@ -112,7 +114,7 @@ func addHandler(cmd *cobra.Command, args []string) { } // Save bookmark to database - _, err = db.SaveBookmarks(cmd.Context(), false, book) + _, err = deps.Database.SaveBookmarks(cmd.Context(), false, book) if err != nil { cError.Printf("Failed to save bookmark with content: %v\n", err) os.Exit(1) diff --git a/internal/cmd/check.go b/internal/cmd/check.go index 8d0ae4a97..702dc632f 100644 --- a/internal/cmd/check.go +++ b/internal/cmd/check.go @@ -29,6 +29,8 @@ func checkCmd() *cobra.Command { } func checkHandler(cmd *cobra.Command, args []string) { + _, deps := initShiori(cmd.Context(), cmd) + // Parse flags skipConfirm, _ := cmd.Flags().GetBool("yes") @@ -53,7 +55,7 @@ func checkHandler(cmd *cobra.Command, args []string) { // Fetch bookmarks from database filterOptions := database.GetBookmarksOptions{IDs: ids} - bookmarks, err := db.GetBookmarks(cmd.Context(), filterOptions) + bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), filterOptions) if err != nil { cError.Printf("Failed to get bookmarks: %v\n", err) os.Exit(1) diff --git a/internal/cmd/delete.go b/internal/cmd/delete.go index fce67a633..2ab032477 100644 --- a/internal/cmd/delete.go +++ b/internal/cmd/delete.go @@ -29,6 +29,8 @@ func deleteCmd() *cobra.Command { } func deleteHandler(cmd *cobra.Command, args []string) { + cfg, deps := initShiori(cmd.Context(), cmd) + // Parse flags skipConfirm, _ := cmd.Flags().GetBool("yes") @@ -52,7 +54,7 @@ func deleteHandler(cmd *cobra.Command, args []string) { } // Delete bookmarks from database - err = db.DeleteBookmarks(cmd.Context(), ids...) + err = deps.Database.DeleteBookmarks(cmd.Context(), ids...) if err != nil { cError.Printf("Failed to delete bookmarks: %v\n", err) os.Exit(1) @@ -60,15 +62,15 @@ func deleteHandler(cmd *cobra.Command, args []string) { // Delete thumbnail image and archives from local disk if len(ids) == 0 { - thumbDir := fp.Join(dataDir, "thumb") - archiveDir := fp.Join(dataDir, "archive") + thumbDir := fp.Join(cfg.Storage.DataDir, "thumb") + archiveDir := fp.Join(cfg.Storage.DataDir, "archive") os.RemoveAll(thumbDir) os.RemoveAll(archiveDir) } else { for _, id := range ids { strID := strconv.Itoa(id) - imgPath := fp.Join(dataDir, "thumb", strID) - archivePath := fp.Join(dataDir, "archive", strID) + imgPath := fp.Join(cfg.Storage.DataDir, "thumb", strID) + archivePath := fp.Join(cfg.Storage.DataDir, "archive", strID) os.Remove(imgPath) os.Remove(archivePath) diff --git a/internal/cmd/export.go b/internal/cmd/export.go index 0aec7be0e..67070a7b5 100644 --- a/internal/cmd/export.go +++ b/internal/cmd/export.go @@ -24,8 +24,10 @@ func exportCmd() *cobra.Command { } func exportHandler(cmd *cobra.Command, args []string) { + _, deps := initShiori(cmd.Context(), cmd) + // Fetch bookmarks from database - bookmarks, err := db.GetBookmarks(cmd.Context(), database.GetBookmarksOptions{}) + bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), database.GetBookmarksOptions{}) if err != nil { cError.Printf("Failed to get bookmarks: %v\n", err) os.Exit(1) diff --git a/internal/cmd/import.go b/internal/cmd/import.go index a623b2b6f..48665fc04 100644 --- a/internal/cmd/import.go +++ b/internal/cmd/import.go @@ -29,6 +29,8 @@ func importCmd() *cobra.Command { } func importHandler(cmd *cobra.Command, args []string) { + _, deps := initShiori(cmd.Context(), cmd) + // Parse flags generateTag := cmd.Flags().Changed("generate-tag") @@ -104,7 +106,7 @@ func importHandler(cmd *cobra.Command, args []string) { return } - _, exist, err := db.GetBookmark(cmd.Context(), 0, url) + _, exist, err := deps.Database.GetBookmark(cmd.Context(), 0, url) if err != nil && !errors.Is(err, sql.ErrNoRows) { cError.Printf("Skip %s: Get Bookmark fail, %v", url, err) return @@ -145,7 +147,7 @@ func importHandler(cmd *cobra.Command, args []string) { }) // Save bookmark to database - bookmarks, err = db.SaveBookmarks(cmd.Context(), true, bookmarks...) + bookmarks, err = deps.Database.SaveBookmarks(cmd.Context(), true, bookmarks...) if err != nil { cError.Printf("Failed to save bookmarks: %v\n", err) os.Exit(1) diff --git a/internal/cmd/open.go b/internal/cmd/open.go index 12ab56763..d7c595da7 100644 --- a/internal/cmd/open.go +++ b/internal/cmd/open.go @@ -36,6 +36,8 @@ func openCmd() *cobra.Command { } func openHandler(cmd *cobra.Command, args []string) { + cfg, deps := initShiori(cmd.Context(), cmd) + // Parse flags skipConfirm, _ := cmd.Flags().GetBool("yes") archiveMode, _ := cmd.Flags().GetBool("archive") @@ -73,7 +75,7 @@ func openHandler(cmd *cobra.Command, args []string) { WithContent: true, } - bookmarks, err := db.GetBookmarks(cmd.Context(), getOptions) + bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), getOptions) if err != nil { cError.Printf("Failed to get bookmarks: %v\n", err) os.Exit(1) @@ -130,7 +132,7 @@ func openHandler(cmd *cobra.Command, args []string) { // Open archive id := strconv.Itoa(bookmarks[0].ID) - archivePath := fp.Join(dataDir, "archive", id) + archivePath := fp.Join(cfg.Storage.DataDir, "archive", id) archive, err := warc.Open(archivePath) if err != nil { diff --git a/internal/cmd/pocket.go b/internal/cmd/pocket.go index 4471d2118..a16ad0e9b 100644 --- a/internal/cmd/pocket.go +++ b/internal/cmd/pocket.go @@ -25,6 +25,8 @@ func pocketCmd() *cobra.Command { } func pocketHandler(cmd *cobra.Command, args []string) { + _, deps := initShiori(cmd.Context(), cmd) + // Open pocket's file srcFile, err := os.Open(args[0]) if err != nil { @@ -70,7 +72,7 @@ func pocketHandler(cmd *cobra.Command, args []string) { return } - _, exist, err := db.GetBookmark(cmd.Context(), 0, url) + _, exist, err := deps.Database.GetBookmark(cmd.Context(), 0, url) if err != nil { cError.Printf("Skip %s: Get Bookmark fail, %v", url, err) return @@ -103,7 +105,7 @@ func pocketHandler(cmd *cobra.Command, args []string) { }) // Save bookmark to database - bookmarks, err = db.SaveBookmarks(cmd.Context(), true, bookmarks...) + bookmarks, err = deps.Database.SaveBookmarks(cmd.Context(), true, bookmarks...) if err != nil { cError.Printf("Failed to save bookmarks: %v\n", err) os.Exit(1) diff --git a/internal/cmd/print.go b/internal/cmd/print.go index bd802d580..c12db8b3e 100644 --- a/internal/cmd/print.go +++ b/internal/cmd/print.go @@ -32,6 +32,8 @@ func printCmd() *cobra.Command { } func printHandler(cmd *cobra.Command, args []string) { + _, deps := initShiori(cmd.Context(), cmd) + // Read flags tags, _ := cmd.Flags().GetStringSlice("tags") keyword, _ := cmd.Flags().GetString("search") @@ -61,7 +63,7 @@ func printHandler(cmd *cobra.Command, args []string) { OrderMethod: orderMethod, } - bookmarks, err := db.GetBookmarks(cmd.Context(), searchOptions) + bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), searchOptions) if err != nil { cError.Printf("Failed to get bookmarks: %v\n", err) return diff --git a/internal/cmd/root-dev.go b/internal/cmd/root-dev.go deleted file mode 100644 index 7d4d626ba..000000000 --- a/internal/cmd/root-dev.go +++ /dev/null @@ -1,7 +0,0 @@ -// +build dev - -package cmd - -func init() { - developmentMode = true -} diff --git a/internal/cmd/root.go b/internal/cmd/root.go index d59773401..a360450fc 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -6,35 +6,29 @@ import ( fp "path/filepath" "time" + "github.com/go-shiori/shiori/internal/config" "github.com/go-shiori/shiori/internal/database" + "github.com/go-shiori/shiori/internal/domains" "github.com/go-shiori/shiori/internal/model" - apppaths "github.com/muesli/go-app-paths" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "golang.org/x/net/context" ) -var ( - db database.DB - dataDir string - developmentMode bool - logLevel string - logCaller bool -) - // ShioriCmd returns the root command for shiori func ShioriCmd() *cobra.Command { - logger := logrus.New() - rootCmd := &cobra.Command{ Use: "shiori", Short: "Simple command-line bookmark manager built with Go", } - rootCmd.PersistentPreRun = preRunRootHandler rootCmd.PersistentFlags().Bool("portable", false, "run shiori in portable mode") - rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", logrus.InfoLevel.String(), "set logrus loglevel") - rootCmd.PersistentFlags().BoolVar(&logCaller, "log-caller", false, "logrus report caller or not") + rootCmd.PersistentFlags().String("storage-directory", "", "path to store shiori data") + rootCmd.MarkFlagsMutuallyExclusive("portable", "storage-directory") + + rootCmd.PersistentFlags().String("log-level", logrus.InfoLevel.String(), "set logrus loglevel") + rootCmd.PersistentFlags().Bool("log-caller", false, "logrus report caller or not") + rootCmd.AddCommand( addCmd(), printCmd(), @@ -46,104 +40,105 @@ func ShioriCmd() *cobra.Command { pocketCmd(), serveCmd(), checkCmd(), - newVersionCommand(logger), - newServerCommand(logger), + newVersionCommand(), + newServerCommand(), ) return rootCmd } -func preRunRootHandler(cmd *cobra.Command, args []string) { - // init logrus - logrus.SetReportCaller(logCaller) - logrus.SetFormatter(&logrus.TextFormatter{ +func initShiori(ctx context.Context, cmd *cobra.Command) (*config.Config, *config.Dependencies) { + logger := logrus.New() + + portableMode, _ := cmd.Flags().GetBool("portable") + logLevel, _ := cmd.Flags().GetString("log-level") + logCaller, _ := cmd.Flags().GetBool("log-caller") + storageDirectory, _ := cmd.Flags().GetString("storage-directory") + + logger.SetReportCaller(logCaller) + logger.SetFormatter(&logrus.TextFormatter{ FullTimestamp: true, TimestampFormat: time.RFC3339, CallerPrettyfier: SFCallerPrettyfier, }) if lvl, err := logrus.ParseLevel(logLevel); err != nil { - cError.Printf("Failed to set log level: %v\n", err) + logger.WithError(err).Panic("failed to set log level") } else { - logrus.SetLevel(lvl) + logger.SetLevel(lvl) } - // Read flag - var err error - portableMode, _ := cmd.Flags().GetBool("portable") + cfg := config.ParseServerConfiguration(ctx, logger) - // Get and create data dir - dataDir, err = getDataDir(portableMode) - if err != nil { - cError.Printf("Failed to get data dir: %v\n", err) - os.Exit(1) + if storageDirectory != "" && cfg.Storage.DataDir != "" { + logger.Warn("--storage-directory is set, overriding SHIORI_DIR.") + cfg.Storage.DataDir = storageDirectory } - err = os.MkdirAll(dataDir, model.DataDirPerm) + cfg.SetDefaults(logger, portableMode) + + err := os.MkdirAll(cfg.Storage.DataDir, model.DataDirPerm) if err != nil { - cError.Printf("Failed to create data dir: %v\n", err) - os.Exit(1) + logger.WithError(err).Fatal("error creating data directory") } - // Open database - db, err = openDatabase(cmd.Context()) + db, err := openDatabase(ctx, cfg.Database.DBMS, cfg.Database.URL) if err != nil { - cError.Printf("Failed to open database: %v\n", err) - os.Exit(1) + logger.WithError(err).Fatal("error opening database") } // Migrate if err := db.Migrate(); err != nil { - cError.Printf("Error running migration: %s\n", err) - os.Exit(1) + logger.WithError(err).Fatalf("Error running migration") } -} -func getDataDir(portableMode bool) (string, error) { - // If in portable mode, uses directory of executable - if portableMode { - exePath, err := os.Executable() - if err != nil { - return "", err - } - - exeDir := fp.Dir(exePath) - return fp.Join(exeDir, "shiori-data"), nil + if cfg.Development { + logger.Warn("Development mode is ENABLED, this will enable some helpers for local development, unsuitable for production environments") } - if developmentMode { - return "dev-data", nil - } + dependencies := config.NewDependencies(logger, db, cfg) + dependencies.Domains.Auth = domains.NewAccountsDomain(logger, cfg.Http.SecretKey, db) + dependencies.Domains.Archiver = domains.NewArchiverDomain(logger, cfg.Storage.DataDir) - // Try to look at environment variables - dataDir, found := os.LookupEnv("SHIORI_DIR") - if found { - return dataDir, nil + // Workaround: Get accounts to make sure at least one is present in the database. + // If there's no accounts in the database, create the shiori/gopher account the legacy api + // hardcoded in the login handler. + accounts, err := db.GetAccounts(cmd.Context(), database.GetAccountsOptions{}) + if err != nil { + cError.Printf("Failed to get owner account: %v\n", err) + os.Exit(1) } - // Try to use platform specific app path - userScope := apppaths.NewScope(apppaths.User, "shiori") - dataDir, err := userScope.DataPath("") - if err == nil { - return dataDir, nil + if len(accounts) == 0 { + account := model.Account{ + Username: "shiori", + Password: "gopher", + Owner: true, + } + + if err := db.SaveAccount(cmd.Context(), account); err != nil { + logger.WithError(err).Fatal("error ensuring owner account") + } } - // When all fail, use current working directory - return ".", nil + return cfg, dependencies } -func openDatabase(ctx context.Context) (database.DB, error) { - switch dbms, _ := os.LookupEnv("SHIORI_DBMS"); dbms { - case "mysql": +func openDatabase(ctx context.Context, dbms, dbURL string) (database.DB, error) { + if dbURL != "" { + return database.Connect(ctx, dbURL) + } + if dbms == "mysql" { return openMySQLDatabase(ctx) - case "postgresql": + } + if dbms == "postgresql" { return openPostgreSQLDatabase(ctx) - default: - return openSQLiteDatabase(ctx) } + return openSQLiteDatabase(ctx) } func openSQLiteDatabase(ctx context.Context) (database.DB, error) { + dataDir := os.Getenv("SHIORI_DIR") dbPath := fp.Join(dataDir, "shiori.db") return database.OpenSQLiteDatabase(ctx, dbPath) } diff --git a/internal/cmd/serve.go b/internal/cmd/serve.go index 347d83556..c711e6679 100644 --- a/internal/cmd/serve.go +++ b/internal/cmd/serve.go @@ -1,10 +1,6 @@ package cmd import ( - "strings" - - "github.com/go-shiori/shiori/internal/webserver" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -15,7 +11,8 @@ func serveCmd() *cobra.Command { Long: "Run a simple and performant web server which " + "serves the site for managing bookmarks. If --port " + "flag is not used, it will use port 8080 by default.", - Run: serveHandler, + Deprecated: "use server instead", + Run: newServerCommandHandler(), } cmd.Flags().IntP("port", "p", 8080, "Port used by the server") @@ -25,39 +22,3 @@ func serveCmd() *cobra.Command { return cmd } - -func serveHandler(cmd *cobra.Command, args []string) { - // Get flags value - port, _ := cmd.Flags().GetInt("port") - address, _ := cmd.Flags().GetString("address") - rootPath, _ := cmd.Flags().GetString("webroot") - log, _ := cmd.Flags().GetBool("log") - - // Validate root path - if rootPath == "" { - rootPath = "/" - } - - if !strings.HasPrefix(rootPath, "/") { - rootPath = "/" + rootPath - } - - if !strings.HasSuffix(rootPath, "/") { - rootPath += "/" - } - - // Start server - serverConfig := webserver.Config{ - DB: db, - DataDir: dataDir, - ServerAddress: address, - ServerPort: port, - RootPath: rootPath, - Log: log, - } - - err := webserver.ServeApp(serverConfig) - if err != nil { - logrus.Fatalf("Server error: %v\n", err) - } -} diff --git a/internal/cmd/server.go b/internal/cmd/server.go index 5395a5134..7637ee5ea 100644 --- a/internal/cmd/server.go +++ b/internal/cmd/server.go @@ -4,55 +4,51 @@ import ( "context" "strings" - "github.com/go-shiori/shiori/internal/config" - "github.com/go-shiori/shiori/internal/domains" "github.com/go-shiori/shiori/internal/http" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) -func newServerCommand(logger *logrus.Logger) *cobra.Command { +func newServerCommand() *cobra.Command { cmd := &cobra.Command{ Use: "server", - Short: "Run the Shiori webserver [alpha]", - Long: "Runs the new Shiori webserver with new API definitions. [alpha]", - Run: newServerCommandHandler(logger), + Short: "Starts the Shiori webserver", + Long: "Serves the Shiori web interface and API.", + Run: newServerCommandHandler(), } cmd.Flags().IntP("port", "p", 8080, "Port used by the server") cmd.Flags().StringP("address", "a", "", "Address the server listens to") cmd.Flags().StringP("webroot", "r", "/", "Root path that used by server") - cmd.Flags().Bool("log", true, "Print out a non-standard access log") + cmd.Flags().Bool("access-log", true, "Print out a non-standard access log") + cmd.Flags().Bool("serve-web-ui", true, "Serve static files from the webroot path") + cmd.Flags().String("secret-key", "", "Secret key used for encrypting session data") return cmd } -func newServerCommandHandler(logger *logrus.Logger) func(cmd *cobra.Command, args []string) { +func newServerCommandHandler() func(cmd *cobra.Command, args []string) { return func(cmd *cobra.Command, args []string) { - logger.Warn("This server is still in alpha, use it at your own risk. For more information check https://github.com/go-shiori/shiori/issues/640") - ctx := context.Background() - database, err := openDatabase(ctx) - if err != nil { - logger.WithError(err).Fatal("error opening database") - } - - cfg := config.ParseServerConfiguration(ctx, logger) - - if cfg.Development { - logger.Warn("Development mode is ENABLED, this will enable some helpers for local development, unsuitable for production environments") - } - - dependencies := config.NewDependencies(logger, database, cfg) - dependencies.Domains.Auth = domains.NewAccountsDomain(logger, cfg.Http.SecretKey, database) - dependencies.Domains.Archiver = domains.NewArchiverDomain(logger, cfg.Http.Storage.DataDir) - - // Get flags value + // Get flags values port, _ := cmd.Flags().GetInt("port") address, _ := cmd.Flags().GetString("address") rootPath, _ := cmd.Flags().GetString("webroot") - accessLog, _ := cmd.Flags().GetBool("log") + accessLog, _ := cmd.Flags().GetBool("access-log") + serveWebUI, _ := cmd.Flags().GetBool("serve-web-ui") + secretKey, _ := cmd.Flags().GetString("secret-key") + + cfg, dependencies := initShiori(ctx, cmd) + + // Check HTTP configuration + // For now it will just log to the console, but in the future it will be fatal. The only required + // setting for now is the secret key. + if errs, isValid := cfg.Http.IsValid(); !isValid { + dependencies.Log.Error("Found some errors in configuration.For now server will start but this will be fatal in the future.") + for _, err := range errs { + dependencies.Log.WithError(err).Error("found invalid configuration") + } + } // Validate root path if rootPath == "" { @@ -72,13 +68,15 @@ func newServerCommandHandler(logger *logrus.Logger) func(cmd *cobra.Command, arg cfg.Http.Address = address + ":" cfg.Http.RootPath = rootPath cfg.Http.AccessLog = accessLog + cfg.Http.ServeWebUI = serveWebUI + cfg.Http.SecretKey = secretKey - server := http.NewHttpServer(logger, cfg.Http, dependencies).Setup(cfg.Http, dependencies) + server := http.NewHttpServer(dependencies.Log).Setup(cfg, dependencies) if err := server.Start(ctx); err != nil { - logger.WithError(err).Fatal("error starting server") + dependencies.Log.WithError(err).Fatal("error starting server") } - logger.WithField("addr", address).Debug("started http server") + dependencies.Log.WithField("addr", address).Debug("started http server") server.WaitStop(ctx) } diff --git a/internal/cmd/update.go b/internal/cmd/update.go index 9690bee49..67cf7ab4a 100644 --- a/internal/cmd/update.go +++ b/internal/cmd/update.go @@ -41,6 +41,8 @@ func updateCmd() *cobra.Command { } func updateHandler(cmd *cobra.Command, args []string) { + cfg, deps := initShiori(cmd.Context(), cmd) + // Parse flags url, _ := cmd.Flags().GetString("url") title, _ := cmd.Flags().GetString("title") @@ -94,7 +96,7 @@ func updateHandler(cmd *cobra.Command, args []string) { IDs: ids, } - bookmarks, err := db.GetBookmarks(cmd.Context(), filterOptions) + bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), filterOptions) if err != nil { cError.Printf("Failed to get bookmarks: %v\n", err) os.Exit(1) @@ -164,7 +166,7 @@ func updateHandler(cmd *cobra.Command, args []string) { } request := core.ProcessRequest{ - DataDir: dataDir, + DataDir: cfg.Storage.DataDir, Bookmark: book, Content: content, ContentType: contentType, @@ -285,7 +287,7 @@ func updateHandler(cmd *cobra.Command, args []string) { } // Save bookmarks to database - bookmarks, err = db.SaveBookmarks(cmd.Context(), false, bookmarks...) + bookmarks, err = deps.Database.SaveBookmarks(cmd.Context(), false, bookmarks...) if err != nil { cError.Printf("Failed to save bookmark: %v\n", err) os.Exit(1) diff --git a/internal/cmd/version.go b/internal/cmd/version.go index da422bca5..5de356b0f 100644 --- a/internal/cmd/version.go +++ b/internal/cmd/version.go @@ -2,21 +2,20 @@ package cmd import ( "github.com/go-shiori/shiori/internal/model" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) -func newVersionCommand(logger *logrus.Logger) *cobra.Command { +func newVersionCommand() *cobra.Command { cmd := &cobra.Command{ Use: "version", Short: "Output the shiori version", - Run: newVersionCommandHandler(logger), + Run: newVersionCommandHandler(), } return cmd } -func newVersionCommandHandler(logger *logrus.Logger) func(cmd *cobra.Command, args []string) { +func newVersionCommandHandler() func(cmd *cobra.Command, args []string) { return func(cmd *cobra.Command, args []string) { cmd.Printf("Shiori version %s (build %s) at %s\n", model.Version, model.Commit, model.Date) } diff --git a/internal/config/config.go b/internal/config/config.go index 984df3f53..336ba5b76 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,7 +3,9 @@ package config import ( "bufio" "context" + "fmt" "os" + "path/filepath" "strings" "time" @@ -41,12 +43,13 @@ func readDotEnv(logger *logrus.Logger) map[string]string { } type HttpConfig struct { - Enabled bool `env:"HTTP_ENABLED,default=True"` - Port int `env:"HTTP_PORT,default=8080"` - Address string `env:"HTTP_ADDRESS,default=:"` - RootPath string `env:"HTTP_ROOT_PATH,default=/"` - AccessLog bool `env:"HTTP_ACCESS_LOG,default=True"` - SecretKey string `env:"HTTP_SECRET_KEY"` + Enabled bool `env:"HTTP_ENABLED,default=True"` + Port int `env:"HTTP_PORT,default=8080"` + Address string `env:"HTTP_ADDRESS,default=:"` + RootPath string `env:"HTTP_ROOT_PATH,default=/"` + AccessLog bool `env:"HTTP_ACCESS_LOG,default=True"` + ServeWebUI bool `env:"HTTP_SERVE_WEB_UI,default=True"` + SecretKey string `env:"HTTP_SECRET_KEY"` // Fiber Specific BodyLimit int `env:"HTTP_BODY_LIMIT,default=1024"` ReadTimeout time.Duration `env:"HTTP_READ_TIMEOUT,default=10s"` @@ -54,31 +57,52 @@ type HttpConfig struct { IDLETimeout time.Duration `env:"HTTP_IDLE_TIMEOUT,default=10s"` DisableKeepAlive bool `env:"HTTP_DISABLE_KEEP_ALIVE,default=true"` DisablePreParseMultipartForm bool `env:"HTTP_DISABLE_PARSE_MULTIPART_FORM,default=true"` - Routes struct { - Bookmark struct { - Path string `env:"ROUTES_BOOKMARK_PATH,default=/bookmark"` - } - Frontend struct { - Path string `env:"ROUTES_STATIC_PATH,default=/"` - MaxAge time.Duration `env:"ROUTES_STATIC_MAX_AGE,default=720h"` - } - System struct { - Path string `env:"ROUTES_SYSTEM_PATH,default=/system"` - } - API struct { - Path string `env:"ROUTE_API_PATH,default=/api/v1"` - } - } - Storage struct { - DataDir string `env:"DATA_DIR"` - } +} + +type DatabaseConfig struct { + DBMS string `env:"DBMS"` // Deprecated + // DBMS requires more environment variables. Check the database package for more information. + URL string `env:"DATABASE_URL"` +} + +type StorageConfig struct { + DataDir string `env:"DIR"` // Using DIR to be backwards compatible with the old config } type Config struct { Hostname string `env:"HOSTNAME,required"` - Development bool `env:"DEVELOPMENT,default=false"` + Development bool `env:"DEVELOPMENT,default=False"` + Database *DatabaseConfig + Storage *StorageConfig // LogLevel string `env:"LOG_LEVEL,default=info"` - Http HttpConfig + Http *HttpConfig +} + +// IsValid checks if the configuration is valid +func (c HttpConfig) IsValid() (errs []error, isValid bool) { + if c.SecretKey == "" { + errs = append(errs, fmt.Errorf("SHIORI_HTTP_SECRET_KEY is required")) + } + + return errs, len(errs) == 0 +} + +// SetDefaults sets the default values for the configuration +func (c Config) SetDefaults(logger *logrus.Logger, portableMode bool) { + // Set the default storage directory if not set, setting also the database url for + // sqlite3 if that engine is used + if c.Storage.DataDir == "" { + var err error + c.Storage.DataDir, err = getStorageDirectory(portableMode) + if err != nil { + logger.WithError(err).Fatal("couldn't determine the data directory") + } + } + + // Set default database url if not set + if c.Database.DBMS == "" && c.Database.URL == "" { + c.Database.URL = fmt.Sprintf("sqlite:///%s", filepath.Join(c.Storage.DataDir, "shiori.db")) + } } func ParseServerConfiguration(ctx context.Context, logger *logrus.Logger) *Config { diff --git a/internal/config/storage.go b/internal/config/storage.go new file mode 100644 index 000000000..de2c4dbea --- /dev/null +++ b/internal/config/storage.go @@ -0,0 +1,31 @@ +package config + +import ( + "fmt" + "os" + "path/filepath" + + gap "github.com/muesli/go-app-paths" +) + +func getStorageDirectory(portableMode bool) (string, error) { + // If in portable mode, uses directory of executable + if portableMode { + exePath, err := os.Executable() + if err != nil { + return "", err + } + + exeDir := filepath.Dir(exePath) + return filepath.Join(exeDir, "shiori-data"), nil + } + + // Try to use platform specific app path + userScope := gap.NewScope(gap.User, "shiori") + dataDir, err := userScope.DataPath("") + if err == nil { + return dataDir, nil + } + + return "", fmt.Errorf("couldn't determine the data directory") +} diff --git a/internal/database/database.go b/internal/database/database.go index 45de936d0..24375946b 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -3,7 +3,9 @@ package database import ( "context" "embed" + "fmt" "log" + "net/url" "github.com/go-shiori/shiori/internal/model" "github.com/jmoiron/sqlx" @@ -43,6 +45,25 @@ type GetAccountsOptions struct { Owner bool } +// Connect connects to database based on submitted database URL. +func Connect(ctx context.Context, dbURL string) (DB, error) { + dbU, err := url.Parse(dbURL) + if err != nil { + return nil, errors.Wrap(err, "failed to parse database URL") + } + + switch dbU.Scheme { + case "mysql": + return OpenMySQLDatabase(ctx, dbURL) + case "postgres": + return OpenPGDatabase(ctx, dbURL) + case "sqlite": + return OpenSQLiteDatabase(ctx, dbU.Path[1:]) + } + + return nil, fmt.Errorf("unsupported database scheme: %s", dbU.Scheme) +} + // DB is interface for accessing and manipulating data in database. type DB interface { // Migrate runs migrations for this database diff --git a/internal/database/sqlite.go b/internal/database/sqlite.go index 4605cdbca..2576f2da5 100644 --- a/internal/database/sqlite.go +++ b/internal/database/sqlite.go @@ -42,7 +42,7 @@ func OpenSQLiteDatabase(ctx context.Context, databasePath string) (sqliteDB *SQL } sqliteDB = &SQLiteDatabase{dbbase: dbbase{*db}} - return sqliteDB, err + return sqliteDB, nil } // Migrate runs migrations for this database engine diff --git a/internal/domains/accounts.go b/internal/domains/accounts.go index 1a2559ee7..132fa1b27 100644 --- a/internal/domains/accounts.go +++ b/internal/domains/accounts.go @@ -66,7 +66,7 @@ func (d *AccountsDomain) GetAccountFromCredentials(ctx context.Context, username func (d *AccountsDomain) CreateTokenForAccount(account *model.Account, expiration time.Time) (string, error) { claims := jwt.MapClaims{ - "account": account, + "account": account.ToDTO(), "exp": expiration.UTC().Unix(), } diff --git a/internal/http/frontend/content.html b/internal/http/frontend/content.html deleted file mode 100644 index 4bdee7983..000000000 --- a/internal/http/frontend/content.html +++ /dev/null @@ -1,90 +0,0 @@ - - - - - - $$.Book.Title$$ - Shiori - Bookmarks Manager - - - - - - - - - - - - - - - - - - - - -
- -
- $$html .Book.HTML$$ -
-
- - - - - \ No newline at end of file diff --git a/internal/http/frontend/css/archive.css b/internal/http/frontend/css/archive.css deleted file mode 100644 index 3529559fe..000000000 --- a/internal/http/frontend/css/archive.css +++ /dev/null @@ -1 +0,0 @@ -:root{--main:#F44336;--border:#E5E5E5;--colorLink:#999;--archiveHeaderBg:rgba(255,255,255,0.95)}@media (prefers-color-scheme:dark){:root{--border:#191919;--archiveHeaderBg:rgba(41,41,41,0.95)}}#shiori-archive-header{top:0;left:0;right:0;height:60px;position:fixed;padding:0 16px;display:-webkit-box;display:flex;-webkit-box-orient:horizontal;-webkit-box-direction:normal;flex-flow:row wrap;-webkit-box-align:center;align-items:center;font-size:16px;border-bottom:1px solid var(--border);background-color:var(--archiveHeaderBg);z-index:9999999999}#shiori-archive-header *{border-width:0;box-sizing:border-box;font-family:"Source Sans Pro",sans-serif;margin:0;padding:0}#shiori-archive-header>*:not(:last-child){margin-right:8px}#shiori-archive-header>.spacer{-webkit-box-flex:1;flex:1}#shiori-archive-header #shiori-logo{font-size:2em;font-weight:100;color:var(--main)}#shiori-archive-header #shiori-logo span{margin-right:8px}#shiori-archive-header a{display:block;color:var(--colorLink);text-decoration:underline}#shiori-archive-header a:hover,#shiori-archive-header a:focus{color:var(--main)}@media (max-width:600px){#shiori-archive-header{font-size:14px;height:50px}#shiori-archive-header #shiori-logo{font-size:1.5em}} \ No newline at end of file diff --git a/internal/http/frontend/embed.go b/internal/http/frontend/embed.go deleted file mode 100644 index 2d127e148..000000000 --- a/internal/http/frontend/embed.go +++ /dev/null @@ -1,6 +0,0 @@ -package frontend - -import "embed" - -//go:embed * -var Assets embed.FS diff --git a/internal/http/frontend/index.html b/internal/http/frontend/index.html deleted file mode 100644 index d3f35d40a..000000000 --- a/internal/http/frontend/index.html +++ /dev/null @@ -1,195 +0,0 @@ - - - - - - Shiori - Bookmarks Manager - - - - - - - - - - - - - - - - - - - - - -
-
- - - -
- - - -
- - - - -
- - - - - diff --git a/internal/http/frontend/js/component/bookmark.js b/internal/http/frontend/js/component/bookmark.js deleted file mode 100644 index aa016a976..000000000 --- a/internal/http/frontend/js/component/bookmark.js +++ /dev/null @@ -1,117 +0,0 @@ -var template = ` -
- - - - -

{{title}} - - - -

-

{{excerpt}}

-

{{id}}

-
-
- {{tag.name}} -
-
-
- - {{hostnameURL}} - - -
-
`; - -export default { - template: template, - props: { - id: Number, - url: String, - title: String, - excerpt: String, - public: Number, - imageURL: String, - hasContent: Boolean, - hasArchive: Boolean, - index: Number, - showId: Boolean, - editMode: Boolean, - listMode: Boolean, - hideThumbnail: Boolean, - hideExcerpt: Boolean, - selected: Boolean, - menuVisible: Boolean, - tags: { - type: Array, - default() { - return [] - } - } - }, - computed: { - mainURL() { - if (this.hasContent) { - return new URL(`bookmark/${this.id}/content`, document.baseURI); - } else if (this.hasArchive) { - return new URL(`bookmark/${this.id}/archive`, document.baseURI); - } else { - return this.url; - } - }, - hostnameURL() { - var url = new URL(this.url); - return url.hostname.replace(/^www\./, ""); - }, - thumbnailVisible() { - return this.imageURL !== "" && - !this.hideThumbnail; - }, - excerptVisible() { - return this.excerpt !== "" && - !this.thumbnailVisible && - !this.hideExcerpt; - }, - thumbnailStyleURL() { - return { - backgroundImage: `url("${this.imageURL}")` - } - }, - eventItem() { - return { - id: this.id, - index: this.index, - } - } - }, - methods: { - tagClicked(name, event) { - this.$emit("tag-clicked", name, event); - }, - selectBookmark() { - this.$emit("select", this.eventItem); - }, - editBookmark() { - this.$emit("edit", this.eventItem); - }, - deleteBookmark() { - this.$emit("delete", this.eventItem); - }, - updateBookmark() { - this.$emit("update", this.eventItem); - } - } -} diff --git a/internal/http/frontend/js/page/base.js b/internal/http/frontend/js/page/base.js deleted file mode 100644 index 3d357a784..000000000 --- a/internal/http/frontend/js/page/base.js +++ /dev/null @@ -1,114 +0,0 @@ -export default { - props: { - activeAccount: { - type: Object, - default() { - return { - id: 0, - username: "", - owner: false, - } - } - }, - appOptions: { - type: Object, - default() { - return { - showId: false, - listMode: false, - nightMode: false, - hideThumbnail: false, - hideExcerpt: false, - - keepMetadata: false, - useArchive: false, - makePublic: false, - }; - } - } - }, - data() { - return { - dialog: {} - } - }, - methods: { - defaultDialog() { - return { - visible: false, - loading: false, - title: '', - content: '', - fields: [], - showLabel: false, - mainText: 'Yes', - secondText: '', - mainClick: () => { - this.dialog.visible = false; - }, - secondClick: () => { - this.dialog.visible = false; - }, - escPressed: () => { - if (!this.loading) this.dialog.visible = false; - } - } - }, - showDialog(cfg) { - var base = this.defaultDialog(); - base.visible = true; - if (cfg.loading) base.loading = cfg.loading; - if (cfg.title) base.title = cfg.title; - if (cfg.content) base.content = cfg.content; - if (cfg.fields) base.fields = cfg.fields; - if (cfg.showLabel) base.showLabel = cfg.showLabel; - if (cfg.mainText) base.mainText = cfg.mainText; - if (cfg.secondText) base.secondText = cfg.secondText; - if (cfg.mainClick) base.mainClick = cfg.mainClick; - if (cfg.secondClick) base.secondClick = cfg.secondClick; - if (cfg.escPressed) base.escPressed = cfg.escPressed; - this.dialog = base; - }, - async getErrorMessage(err) { - switch (err.constructor) { - case Error: - return err.message; - case Response: - var text = await err.text(); - return `${text} (${err.status})`; - default: - return err; - } - }, - isSessionError(err) { - switch (err.toString().replace(/\(\d+\)/g, "").trim().toLowerCase()) { - case "session is not exist": - case "session has been expired": - return true - default: - return false; - } - }, - showErrorDialog(msg) { - var sessionError = this.isSessionError(msg), - dialogContent = sessionError ? "Session has expired, please login again." : msg; - - this.showDialog({ - visible: true, - title: 'Error', - content: dialogContent, - mainText: 'OK', - mainClick: () => { - this.dialog.visible = false; - if (sessionError) { - var loginUrl = new Url("login", document.baseURI); - loginUrl.query.dst = window.location.href; - - document.cookie = `session-id=; Path=${new URL(document.baseURI).pathname}; Expires=Thu, 01 Jan 1970 00:00:00 GMT;`; - location.href = loginUrl.toString(); - } - } - }); - }, - } -} \ No newline at end of file diff --git a/internal/http/frontend/js/page/home.js b/internal/http/frontend/js/page/home.js deleted file mode 100644 index 33a0faf22..000000000 --- a/internal/http/frontend/js/page/home.js +++ /dev/null @@ -1,836 +0,0 @@ -var template = ` -
- - -

No saved bookmarks yet :(

-
- - - - - - -
-
- - (all tagged) - (all untagged) - - #{{tag.name}}{{tag.nBookmarks}} - - - -
` - -import paginationBox from "../component/pagination.js"; -import bookmarkItem from "../component/bookmark.js"; -import customDialog from "../component/dialog.js"; -import basePage from "./base.js"; - -export default { - template: template, - mixins: [basePage], - components: { - bookmarkItem, - paginationBox, - customDialog - }, - data() { - return { - loading: false, - editMode: false, - selection: [], - - search: "", - page: 0, - maxPage: 0, - bookmarks: [], - tags: [], - - dialogTags: { - visible: false, - editMode: false, - title: 'Existing Tags', - mainText: 'OK', - secondText: 'Rename Tags', - mainClick: () => { - if (this.dialogTags.editMode) { - this.dialogTags.editMode = false; - } else { - this.dialogTags.visible = false; - } - }, - secondClick: () => { - this.dialogTags.editMode = true; - }, - escPressed: () => { - this.dialogTags.visible = false; - this.dialogTags.editMode = false; - } - }, - } - }, - computed: { - listIsEmpty() { - return this.bookmarks.length <= 0; - } - }, - watch: { - "dialogTags.editMode"(editMode) { - if (editMode) { - this.dialogTags.title = "Rename Tags"; - this.dialogTags.mainText = "Cancel"; - this.dialogTags.secondText = ""; - } else { - this.dialogTags.title = "Existing Tags"; - this.dialogTags.mainText = "OK"; - this.dialogTags.secondText = "Rename Tags"; - } - } - }, - methods: { - reloadData() { - if (this.loading) return; - this.page = 1; - this.search = ""; - this.loadData(true, true); - }, - loadData(saveState, fetchTags) { - if (this.loading) return; - - // Set default args - saveState = (typeof saveState === "boolean") ? saveState : true; - fetchTags = (typeof fetchTags === "boolean") ? fetchTags : false; - - // Parse search query - var keyword = this.search, - rxExcludeTagA = /(^|\s)-tag:["']([^"']+)["']/i, // -tag:"with space" - rxExcludeTagB = /(^|\s)-tag:(\S+)/i, // -tag:without-space - rxIncludeTagA = /(^|\s)tag:["']([^"']+)["']/i, // tag:"with space" - rxIncludeTagB = /(^|\s)tag:(\S+)/i, // tag:without-space - tags = [], - excludedTags = [], - rxResult; - - // Get excluded tag first, while also removing it from keyword - while (rxResult = rxExcludeTagA.exec(keyword)) { - keyword = keyword.replace(rxResult[0], ""); - excludedTags.push(rxResult[2]); - } - - while (rxResult = rxExcludeTagB.exec(keyword)) { - keyword = keyword.replace(rxResult[0], ""); - excludedTags.push(rxResult[2]); - } - - // Get included tags - while (rxResult = rxIncludeTagA.exec(keyword)) { - keyword = keyword.replace(rxResult[0], ""); - tags.push(rxResult[2]); - } - - while (rxResult = rxIncludeTagB.exec(keyword)) { - keyword = keyword.replace(rxResult[0], ""); - tags.push(rxResult[2]); - } - - // Trim keyword - keyword = keyword.trim().replace(/\s+/g, " "); - - // Prepare URL for API - var url = new URL("api/v1/bookmarks", document.baseURI); - url.search = new URLSearchParams({ - keyword: keyword, - tags: tags.join(","), - exclude: excludedTags.join(","), - page: this.page - }); - - // Fetch data from API - var skipFetchTags = Error("skip fetching tags"); - - this.loading = true; - fetch(url, {headers: {'Content-Type': 'application/json'}}) - .then(response => { - if (!response.ok) throw response; - return response.json(); - }) - .then(json => { - // Set data - this.page = json.page; - this.maxPage = json.maxPage; - this.bookmarks = json.message; - - // Save state and change URL if needed - if (saveState) { - var history = { - activePage: "page-home", - search: this.search, - page: this.page - }; - - var url = new Url(document.baseURI); - url.hash = "home"; - url.clearQuery(); - if (this.page > 1) url.query.page = this.page; - if (this.search !== "") url.query.search = this.search; - - window.history.pushState(history, "page-home", url); - } - - // Fetch tags if requested - if (fetchTags) { - return fetch(new URL("api/v1/tags", document.baseURI), {headers: {'Content-Type': 'application/json'}}); - } else { - this.loading = false; - throw skipFetchTags; - } - }) - .then(response => { - if (!response.ok) throw response; - return response.json(); - }) - .then(json => { - this.tags = json.message; - this.loading = false; - }) - .catch(err => { - this.loading = false; - - if (err !== skipFetchTags) { - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - } - }); - }, - searchBookmarks() { - this.page = 1; - this.loadData(); - }, - changePage(page) { - this.page = page; - this.$refs.bookmarksGrid.scrollTop = 0; - this.loadData(); - }, - toggleEditMode() { - this.selection = []; - this.editMode = !this.editMode; - }, - toggleSelection(item) { - var idx = this.selection.findIndex(el => el.id === item.id); - if (idx === -1) this.selection.push(item); - else this.selection.splice(idx, 1); - }, - isSelected(bookId) { - return this.selection.findIndex(el => el.id === bookId) > -1; - }, - dialogTagClicked(event, tag) { - if (!this.dialogTags.editMode) { - this.filterTag(tag.name, event.altKey); - } else { - this.dialogTags.visible = false; - this.showDialogRenameTag(tag); - } - }, - bookmarkTagClicked(event, tagName) { - this.filterTag(tagName, event.altKey); - }, - filterTag(tagName, excludeMode) { - // Set default parameter - excludeMode = (typeof excludeMode === "boolean") ? excludeMode : false; - - if (this.dialogTags.editMode) { - return; - } - - if (tagName === "*") { - this.search = excludeMode ? "-tag:*" : "tag:*"; - this.page = 1; - this.loadData(); - return; - } - - var rxSpace = /\s+/g, - includeTag = rxSpace.test(tagName) ? `tag:"${tagName}"` : `tag:${tagName}`, - excludeTag = "-" + includeTag, - rxIncludeTag = new RegExp(`(^|\\s)${includeTag}`, "ig"), - rxExcludeTag = new RegExp(`(^|\\s)${excludeTag}`, "ig"), - search = this.search; - - search = search.replace("-tag:*", ""); - search = search.replace("tag:*", ""); - search = search.trim(); - - if (excludeMode) { - if (rxExcludeTag.test(search)) { - return; - } - - if (rxIncludeTag.test(search)) { - this.search = search.replace(rxIncludeTag, "$1" + excludeTag); - } else { - search += ` ${excludeTag}`; - this.search = search.trim(); - } - } else { - if (rxIncludeTag.test(search)) { - return; - } - - if (rxExcludeTag.test(search)) { - this.search = search.replace(rxExcludeTag, "$1" + includeTag); - } else { - search += ` ${includeTag}`; - this.search = search.trim(); - } - } - - this.page = 1; - this.loadData(); - }, - showDialogAdd() { - this.showDialog({ - title: "New Bookmark", - content: "Create a new bookmark", - fields: [{ - name: "url", - label: "Url, start with http://...", - }, { - name: "title", - label: "Custom title (optional)" - }, { - name: "excerpt", - label: "Custom excerpt (optional)", - type: "area" - }, { - name: "tags", - label: "Comma separated tags (optional)", - separator: ",", - dictionary: this.tags.map(tag => tag.name) - }, { - name: "createArchive", - label: "Create archive", - type: "check", - value: this.appOptions.useArchive, - }, { - name: "makePublic", - label: "Make archive publicly available", - type: "check", - value: this.appOptions.makePublic, - }], - mainText: "OK", - secondText: "Cancel", - mainClick: (data) => { - // Make sure URL is not empty - if (data.url.trim() === "") { - this.showErrorDialog("URL must not empty"); - return; - } - - // Prepare tags - var tags = data.tags - .toLowerCase() - .replace(/\s+/g, " ") - .split(/\s*,\s*/g) - .filter(tag => tag.trim() !== "") - .map(tag => { - return { - name: tag.trim() - }; - }); - - // Send data - var data = { - url: data.url.trim(), - title: data.title.trim(), - excerpt: data.excerpt.trim(), - public: data.makePublic ? 1 : 0, - tags: tags, - createArchive: data.createArchive, - }; - - this.dialog.loading = true; - fetch(new URL("api/v1/bookmarks", document.baseURI), { - method: "post", - body: JSON.stringify(data), - headers: { "Content-Type": "application/json" } - }).then(response => { - if (!response.ok) throw response; - return response.json(); - }).then(json => { - this.dialog.loading = false; - this.dialog.visible = false; - this.bookmarks.splice(0, 0, json); - }).catch(err => { - this.dialog.loading = false; - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - }); - } - }); - }, - showDialogEdit(item) { - // Check the item - if (typeof item !== "object") return; - - var id = (typeof item.id === "number") ? item.id : 0, - index = (typeof item.index === "number") ? item.index : -1; - - if (id < 1 || index < 0) return; - - // Get the existing bookmark value - var book = JSON.parse(JSON.stringify(this.bookmarks[index])), - strTags = book.tags.map(tag => tag.name).join(", "); - - this.showDialog({ - title: "Edit Bookmark", - content: "Edit the bookmark's data", - showLabel: true, - fields: [{ - name: "url", - label: "Url", - value: book.url, - }, { - name: "title", - label: "Title", - value: book.title, - }, { - name: "excerpt", - label: "Excerpt", - type: "area", - value: book.excerpt, - }, { - name: "tags", - label: "Tags", - value: strTags, - separator: ",", - dictionary: this.tags.map(tag => tag.name) - }, { - name: "makePublic", - label: "Make archive publicly available", - type: "check", - value: book.public >= 1, - }], - mainText: "OK", - secondText: "Cancel", - mainClick: (data) => { - // Validate input - if (data.title.trim() === "") return; - - // Prepare tags - var tags = data.tags - .toLowerCase() - .replace(/\s+/g, " ") - .split(/\s*,\s*/g) - .filter(tag => tag.trim() !== "") - .map(tag => { - return { - name: tag.trim() - }; - }); - - // Set new data - book.url = data.url.trim(); - book.title = data.title.trim(); - book.excerpt = data.excerpt.trim(); - book.public = data.makePublic ? 1 : 0; - book.tags = tags; - - // Send data - this.dialog.loading = true; - fetch(new URL("api/v1/bookmarks", document.baseURI), { - method: "put", - body: JSON.stringify(book), - headers: { "Content-Type": "application/json" } - }).then(response => { - if (!response.ok) throw response; - return response.json(); - }).then(json => { - this.dialog.loading = false; - this.dialog.visible = false; - this.bookmarks.splice(index, 1, json); - }).catch(err => { - this.dialog.loading = false; - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - }); - } - }); - }, - showDialogDelete(items) { - // Check and filter items - if (typeof items !== "object") return; - if (!Array.isArray(items)) items = [items]; - - items = items.filter(item => { - var id = (typeof item.id === "number") ? item.id : 0, - index = (typeof item.index === "number") ? item.index : -1; - - return id > 0 && index > -1; - }); - - if (items.length === 0) return; - - // Split ids and indices - var ids = items.map(item => item.id), - indices = items.map(item => item.index).sort((a, b) => b - a); - - // Create title and content - var title = "Delete Bookmarks", - content = "Delete the selected bookmarks ? This action is irreversible."; - - if (items.length === 1) { - title = "Delete Bookmark"; - content = "Are you sure ? This action is irreversible."; - } - - // Show dialog - this.showDialog({ - title: title, - content: content, - mainText: "Yes", - secondText: "No", - mainClick: () => { - this.dialog.loading = true; - fetch(new URL("api/v1/bookmarks/" + ids, document.baseURI), { - method: "delete", - headers: { "Content-Type": "application/json" }, - }).then(response => { - if (!response.ok) throw response; - return response; - }).then(() => { - this.selection = []; - this.editMode = false; - this.dialog.loading = false; - this.dialog.visible = false; - indices.forEach(index => this.bookmarks.splice(index, 1)) - - if (this.bookmarks.length < 20) { - this.loadData(false); - } - }).catch(err => { - this.selection = []; - this.editMode = false; - this.dialog.loading = false; - - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - }); - } - }); - }, - showDialogUpdateCache(items) { - // Check and filter items - if (typeof items !== "object") return; - if (!Array.isArray(items)) items = [items]; - - items = items.filter(item => { - var id = (typeof item.id === "number") ? item.id : 0, - index = (typeof item.index === "number") ? item.index : -1; - - return id > 0 && index > -1; - }); - - if (items.length === 0) return; - - // Show dialog - var ids = items.map(item => item.id); - - this.showDialog({ - title: "Update Cache", - content: "Update cache for selected bookmarks ? This action is irreversible.", - fields: [{ - name: "keepMetadata", - label: "Keep the old title and excerpt", - type: "check", - value: this.appOptions.keepMetadata, - }, { - name: "createArchive", - label: "Update archive as well", - type: "check", - value: this.appOptions.useArchive, - }], - mainText: "Yes", - secondText: "No", - mainClick: (data) => { - var data = { - ids: ids, - createArchive: data.createArchive, - keepMetadata: data.keepMetadata, - }; - - this.dialog.loading = true; - fetch(new URL("api/v1/cache", document.baseURI), { - method: "put", - body: JSON.stringify(data), - headers: { "Content-Type": "application/json" }, - }).then(response => { - if (!response.ok) throw response; - return response.json(); - }).then(json => { - this.selection = []; - this.editMode = false; - this.dialog.loading = false; - this.dialog.visible = false; - - json.forEach(book => { - var item = items.find(el => el.id === book.id); - this.bookmarks.splice(item.index, 1, book); - }); - }).catch(err => { - this.selection = []; - this.editMode = false; - this.dialog.loading = false; - - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - }); - } - }); - }, - showDialogAddTags(items) { - // Check and filter items - if (typeof items !== "object") return; - if (!Array.isArray(items)) items = [items]; - - items = items.filter(item => { - var id = (typeof item.id === "number") ? item.id : 0, - index = (typeof item.index === "number") ? item.index : -1; - - return id > 0 && index > -1; - }); - - if (items.length === 0) return; - - // Show dialog - this.showDialog({ - title: "Add New Tags", - content: "Add new tags to selected bookmarks", - fields: [{ - name: "tags", - label: "Comma separated tags", - value: "", - separator: ",", - dictionary: this.tags.map(tag => tag.name) - }], - mainText: 'OK', - secondText: 'Cancel', - mainClick: (data) => { - // Validate input - var tags = data.tags - .toLowerCase() - .replace(/\s+/g, ' ') - .split(/\s*,\s*/g) - .filter(tag => tag.trim() !== '') - .map(tag => { - return { - name: tag.trim() - }; - }); - - if (tags.length === 0) return; - - // Send data - var request = { - ids: items.map(item => item.id), - tags: tags - } - - this.dialog.loading = true; - fetch(new URL("api/v1/bookmarks/tags", document.baseURI), { - method: "put", - body: JSON.stringify(request), - headers: { "Content-Type": "application/json" }, - }).then(response => { - if (!response.ok) throw response; - return response.json(); - }).then(json => { - this.selection = []; - this.editMode = false; - this.dialog.loading = false; - this.dialog.visible = false; - - json.forEach(book => { - var item = items.find(el => el.id === book.id); - this.bookmarks.splice(item.index, 1, book); - }); - }).catch(err => { - this.selection = []; - this.editMode = false; - this.dialog.loading = false; - - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - }); - } - }); - }, - showDialogTags() { - this.dialogTags.visible = true; - this.dialogTags.editMode = false; - this.dialogTags.secondText = this.activeAccount.owner ? "Rename Tags" : ""; - }, - showDialogRenameTag(tag) { - this.showDialog({ - title: "Rename Tag", - content: `Change the name for tag "#${tag.name}"`, - fields: [{ - name: "newName", - label: "New tag name", - value: tag.name, - }], - mainText: "OK", - secondText: "Cancel", - secondClick: () => { - this.dialog.visible = false; - this.dialogTags.visible = true; - }, - escPressed: () => { - this.dialog.visible = false; - this.dialogTags.visible = true; - }, - mainClick: (data) => { - // Save the old query - var rxSpace = /\s+/g, - oldTagQuery = rxSpace.test(tag.name) ? `"#${tag.name}"` : `#${tag.name}`, - newTagQuery = rxSpace.test(data.newName) ? `"#${data.newName}"` : `#${data.newName}`; - - // Send data - var newData = { - id: tag.id, - name: data.newName, - }; - - this.dialog.loading = true; - fetch(new URL("api/tag", document.baseURI), { - method: "PUT", - body: JSON.stringify(newData), - headers: { "Content-Type": "application/json" }, - }).then(response => { - if (!response.ok) throw response; - return response.json(); - }).then(() => { - tag.name = data.newName; - - this.dialog.loading = false; - this.dialog.visible = false; - this.dialogTags.visible = true; - this.dialogTags.editMode = false; - this.tags.sort((a, b) => { - var aName = a.name.toLowerCase(), - bName = b.name.toLowerCase(); - - if (aName < bName) return -1; - else if (aName > bName) return 1; - else return 0; - }); - - if (this.search.includes(oldTagQuery)) { - this.search = this.search.replace(oldTagQuery, newTagQuery); - this.loadData(); - } - }).catch(err => { - this.dialog.loading = false; - this.dialogTags.visible = false; - this.dialogTags.editMode = false; - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - }); - }, - }); - }, - }, - mounted() { - // Prepare history state watcher - var stateWatcher = (e) => { - var state = e.state || {}, - activePage = state.activePage || "page-home", - search = state.search || "", - page = state.page || 1; - - if (activePage !== "page-home") return; - - this.page = page; - this.search = search; - this.loadData(false); - } - - window.addEventListener('popstate', stateWatcher); - this.$once('hook:beforeDestroy', () => { - window.removeEventListener('popstate', stateWatcher); - }) - - // Set initial parameter - var url = new Url; - this.search = url.query.search || ""; - this.page = url.query.page || 1; - - this.loadData(false, true); - } -} diff --git a/internal/http/frontend/js/page/setting.js b/internal/http/frontend/js/page/setting.js deleted file mode 100644 index 82be3e8aa..000000000 --- a/internal/http/frontend/js/page/setting.js +++ /dev/null @@ -1,304 +0,0 @@ -var template = ` -
-

Settings

-
-
- Display - - - - - -
-
- Bookmarks - - - -
-
- Accounts -
    -
  • No accounts registered
  • -
  • -

    {{account.username}} - -

    - - - - - - -
  • -
- -
-
-
- -
`; - -import customDialog from "../component/dialog.js"; -import basePage from "./base.js"; - -export default { - template: template, - mixins: [basePage], - components: { - customDialog - }, - data() { - return { - loading: false, - accounts: [] - } - }, - methods: { - saveSetting() { - this.$emit("setting-changed", { - showId: this.appOptions.showId, - listMode: this.appOptions.listMode, - hideThumbnail: this.appOptions.hideThumbnail, - hideExcerpt: this.appOptions.hideExcerpt, - nightMode: this.appOptions.nightMode, - keepMetadata: this.appOptions.keepMetadata, - useArchive: this.appOptions.useArchive, - makePublic: this.appOptions.makePublic, - }); - }, - loadAccounts() { - if (this.loading) return; - - this.loading = true; - fetch(new URL("api/v1/accounts", document.baseURI), {headers: {'Content-Type': 'application/json'}}) - .then(response => { - if (!response.ok) throw response; - return response.json(); - }) - .then(json => { - this.loading = false; - this.accounts = json; - }) - .catch(err => { - this.loading = false; - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - }); - }, - showDialogNewAccount() { - this.showDialog({ - title: "New Account", - content: "Input new account's data :", - fields: [{ - name: "username", - label: "Username", - value: "", - }, { - name: "password", - label: "Password", - type: "password", - value: "", - }, { - name: "repeat", - label: "Repeat password", - type: "password", - value: "", - }, { - name: "visitor", - label: "This account is for visitor", - type: "check", - value: false, - }], - mainText: "OK", - secondText: "Cancel", - mainClick: (data) => { - if (data.username === "") { - this.showErrorDialog("Username must not empty"); - return; - } - - if (data.password === "") { - this.showErrorDialog("Password must not empty"); - return; - } - - if (data.password !== data.repeat) { - this.showErrorDialog("Password does not match"); - return; - } - - var request = { - username: data.username, - password: data.password, - owner: !data.visitor, - } - - this.dialog.loading = true; - fetch(new URL("api/v1/accounts", document.baseURI), { - method: "post", - body: JSON.stringify(request), - headers: { - "Content-Type": "application/json", - } - }).then(response => { - if (!response.ok) throw response; - return response; - }).then(() => { - this.dialog.loading = false; - this.dialog.visible = false; - - this.accounts.push({ username: data.username, owner: !data.visitor }); - this.accounts.sort((a, b) => { - var nameA = a.username.toLowerCase(), - nameB = b.username.toLowerCase(); - - if (nameA < nameB) { - return -1; - } - - if (nameA > nameB) { - return 1; - } - - return 0; - }); - }).catch(err => { - this.dialog.loading = false; - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - }); - } - }); - }, - showDialogChangePassword(account) { - this.showDialog({ - title: "Change Password", - content: "Input new password :", - fields: [{ - name: "oldPassword", - label: "Old password", - type: "password", - value: "", - }, { - name: "password", - label: "New password", - type: "password", - value: "", - }, { - name: "repeat", - label: "Repeat password", - type: "password", - value: "", - }], - mainText: "OK", - secondText: "Cancel", - mainClick: (data) => { - if (data.oldPassword === "") { - this.showErrorDialog("Old password must not empty"); - return; - } - - if (data.password === "") { - this.showErrorDialog("New password must not empty"); - return; - } - - if (data.password !== data.repeat) { - this.showErrorDialog("Password does not match"); - return; - } - - var request = { - username: account.username, - oldPassword: data.oldPassword, - newPassword: data.password, - owner: account.owner, - } - - this.dialog.loading = true; - fetch(new URL("api/v1/accounts", document.baseURI), { - method: "put", - body: JSON.stringify(request), - headers: { - "Content-Type": "application/json", - }, - }).then(response => { - if (!response.ok) throw response; - return response; - }).then(() => { - this.dialog.loading = false; - this.dialog.visible = false; - }).catch(err => { - this.dialog.loading = false; - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - }); - } - }); - }, - showDialogDeleteAccount(account, idx) { - this.showDialog({ - title: "Delete Account", - content: `Delete account "${account.username}" ?`, - mainText: "Yes", - secondText: "No", - mainClick: () => { - this.dialog.loading = true; - fetch(`/api/v1/accounts`, { - method: "delete", - body: JSON.stringify([account.username]), - headers: { - "Content-Type": "application/json", - }, - }).then(response => { - if (!response.ok) throw response; - return response; - }).then(() => { - this.dialog.loading = false; - this.dialog.visible = false; - this.accounts.splice(idx, 1); - }).catch(err => { - this.dialog.loading = false; - this.getErrorMessage(err).then(msg => { - this.showErrorDialog(msg); - }) - }); - } - }); - }, - }, - mounted() { - this.loadAccounts(); - } -} diff --git a/internal/http/frontend/less/archive.less b/internal/http/frontend/less/archive.less deleted file mode 100644 index 599a521cb..000000000 --- a/internal/http/frontend/less/archive.less +++ /dev/null @@ -1,73 +0,0 @@ -:root { - --main : #F44336; - --border : #E5E5E5; - --colorLink : #999; - --archiveHeaderBg: rgba(255, 255, 255, 0.95); - - @media (prefers-color-scheme: dark) { - --border : #191919; - --archiveHeaderBg: rgba(41, 41, 41, 0.95); - } -} - -#shiori-archive-header { - top : 0; - left : 0; - right : 0; - height : 60px; - position : fixed; - padding : 0 16px; - display : flex; - flex-flow : row wrap; - align-items : center; - font-size : 16px; - border-bottom : 1px solid var(--border); - background-color: var(--archiveHeaderBg); - z-index : 9999999999; - - * { - border-width: 0; - box-sizing : border-box; - font-family : "Source Sans Pro", sans-serif; - margin : 0; - padding : 0; - } - - >*:not(:last-child) { - margin-right: 8px; - } - - >.spacer { - flex: 1; - } - - #shiori-logo { - font-size : 2em; - font-weight: 100; - color : var(--main); - - span { - margin-right: 8px; - } - } - - a { - display : block; - color : var(--colorLink); - text-decoration: underline; - - &:hover, - &:focus { - color: var(--main); - } - } - - @media (max-width: 600px) { - font-size: 14px; - height : 50px; - - #shiori-logo { - font-size: 1.5em; - } - } -} \ No newline at end of file diff --git a/internal/http/frontend/login.html b/internal/http/frontend/login.html deleted file mode 100644 index 3e17b0da3..000000000 --- a/internal/http/frontend/login.html +++ /dev/null @@ -1,139 +0,0 @@ - - - - - - Login - Shiori - - - - - - - - - - - - - - - - - - - -
-

{{error}}

-
-
-
- -

simple bookmark manager

-
-
- - - - - -
- -
-
-
- - - - - diff --git a/internal/http/routes/api/v1/api.go b/internal/http/routes/api/v1/api.go index e70b39506..65f8bd563 100644 --- a/internal/http/routes/api/v1/api.go +++ b/internal/http/routes/api/v1/api.go @@ -9,17 +9,14 @@ import ( ) type APIRoutes struct { - logger *logrus.Logger - deps *config.Dependencies + logger *logrus.Logger + deps *config.Dependencies + loginHandler model.LegacyLoginHandler } func (r *APIRoutes) Setup(g *gin.RouterGroup) model.Routes { - if r.deps.Config.Development { - r.handle(g, "/debug", NewDebugPIRoutes(r.logger, r.deps)) - } - // Account API handles authentication in each route - r.handle(g, "/auth", NewAuthAPIRoutes(r.logger, r.deps)) + r.handle(g, "/auth", NewAuthAPIRoutes(r.logger, r.deps, r.loginHandler)) // From here on, all routes require authentication g.Use(middleware.AuthenticationRequired()) @@ -34,9 +31,10 @@ func (s *APIRoutes) handle(g *gin.RouterGroup, path string, routes model.Routes) routes.Setup(group) } -func NewAPIRoutes(logger *logrus.Logger, deps *config.Dependencies) *APIRoutes { +func NewAPIRoutes(logger *logrus.Logger, deps *config.Dependencies, loginHandler model.LegacyLoginHandler) *APIRoutes { return &APIRoutes{ - logger: logger, - deps: deps, + logger: logger, + deps: deps, + loginHandler: loginHandler, } } diff --git a/internal/http/routes/api/v1/auth.go b/internal/http/routes/api/v1/auth.go index 0a1152e02..a7403fabf 100644 --- a/internal/http/routes/api/v1/auth.go +++ b/internal/http/routes/api/v1/auth.go @@ -14,8 +14,9 @@ import ( ) type AuthAPIRoutes struct { - logger *logrus.Logger - deps *config.Dependencies + logger *logrus.Logger + deps *config.Dependencies + legacyLoginHandler model.LegacyLoginHandler } func (r *AuthAPIRoutes) Setup(group *gin.RouterGroup) model.Routes { @@ -25,10 +26,6 @@ func (r *AuthAPIRoutes) Setup(group *gin.RouterGroup) model.Routes { return r } -func (r *AuthAPIRoutes) setCookie(c *gin.Context, token string, expiration time.Time) { - c.SetCookie("auth", token, int(expiration.Unix()), "/", "", !r.deps.Config.Development, false) -} - type loginRequestPayload struct { Username string `json:"username" validate:"required"` Password string `json:"password" validate:"required"` @@ -46,7 +43,9 @@ func (p *loginRequestPayload) IsValid() error { } type loginResponseMessage struct { - Token string `json:"token"` + Token string `json:"token"` + SessionID string `json:"session"` // Deprecated, used only for legacy APIs + Expiration int64 `json:"expires"` // Deprecated, used only for legacy APIs } // loginHandler godoc @@ -87,12 +86,18 @@ func (r *AuthAPIRoutes) loginHandler(c *gin.Context) { return } - responseMessage := loginResponseMessage{ - Token: token, + sessionID, err := r.legacyLoginHandler(*account, time.Hour*24*30) + if err != nil { + r.logger.WithError(err).Error("failed execute legacy login handler") + response.SendInternalServerError(c) + return } - // TODO: move cookie logic to frontend routes - r.setCookie(c, token, expiration) + responseMessage := loginResponseMessage{ + Token: token, + SessionID: sessionID, + Expiration: expiration.Unix(), + } response.Send(c, http.StatusOK, responseMessage) } @@ -124,8 +129,6 @@ func (r *AuthAPIRoutes) refreshHandler(c *gin.Context) { Token: token, } - r.setCookie(c, token, expiration) - response.Send(c, http.StatusAccepted, responseMessage) } @@ -147,9 +150,10 @@ func (r *AuthAPIRoutes) meHandler(c *gin.Context) { response.Send(c, http.StatusOK, ctx.GetAccount()) } -func NewAuthAPIRoutes(logger *logrus.Logger, deps *config.Dependencies) *AuthAPIRoutes { +func NewAuthAPIRoutes(logger *logrus.Logger, deps *config.Dependencies, loginHandler model.LegacyLoginHandler) *AuthAPIRoutes { return &AuthAPIRoutes{ - logger: logger, - deps: deps, + logger: logger, + deps: deps, + legacyLoginHandler: loginHandler, } } diff --git a/internal/http/routes/api/v1/auth_test.go b/internal/http/routes/api/v1/auth_test.go index e74c16f6b..3b2ad1c73 100644 --- a/internal/http/routes/api/v1/auth_test.go +++ b/internal/http/routes/api/v1/auth_test.go @@ -15,6 +15,10 @@ import ( "github.com/stretchr/testify/require" ) +func noopLegacyLoginHandler(_ model.Account, _ time.Duration) (string, error) { + return "", nil +} + func TestAccountsRoute(t *testing.T) { logger := logrus.New() ctx := context.TODO() @@ -22,7 +26,7 @@ func TestAccountsRoute(t *testing.T) { t.Run("login invalid", func(t *testing.T) { g := testutil.NewGin() _, deps := testutil.GetTestConfigurationAndDependencies(t, ctx, logger) - router := NewAuthAPIRoutes(logger, deps) + router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler) router.Setup(g.Group("/")) w := httptest.NewRecorder() body := []byte(`{"username": "gopher"}`) @@ -35,7 +39,7 @@ func TestAccountsRoute(t *testing.T) { t.Run("login incorrect", func(t *testing.T) { g := testutil.NewGin() _, deps := testutil.GetTestConfigurationAndDependencies(t, ctx, logger) - router := NewAuthAPIRoutes(logger, deps) + router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler) router.Setup(g.Group("/")) w := httptest.NewRecorder() body := []byte(`{"username": "gopher", "password": "shiori"}`) @@ -48,7 +52,7 @@ func TestAccountsRoute(t *testing.T) { t.Run("login correct", func(t *testing.T) { g := testutil.NewGin() _, deps := testutil.GetTestConfigurationAndDependencies(t, ctx, logger) - router := NewAuthAPIRoutes(logger, deps) + router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler) router.Setup(g.Group("/")) // Create an account manually to test @@ -73,7 +77,7 @@ func TestAccountsRoute(t *testing.T) { g := testutil.NewGin() g.Use(middleware.AuthMiddleware(deps)) - router := NewAuthAPIRoutes(logger, deps) + router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler) router.Setup(g.Group("/")) // Create an account manually to test @@ -101,7 +105,7 @@ func TestAccountsRoute(t *testing.T) { g := testutil.NewGin() g.Use(middleware.AuthMiddleware(deps)) - router := NewAuthAPIRoutes(logger, deps) + router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler) router.Setup(g.Group("/")) req := httptest.NewRequest("GET", "/me", nil) @@ -155,7 +159,7 @@ func TestRefreshHandler(t *testing.T) { g := testutil.NewGin() _, deps := testutil.GetTestConfigurationAndDependencies(t, ctx, logger) - router := NewAuthAPIRoutes(logger, deps) + router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler) g.Use(middleware.AuthMiddleware(deps)) // Requires AuthMiddleware to manipulate context router.Setup(g.Group("/")) diff --git a/internal/http/routes/api/v1/debug.go b/internal/http/routes/api/v1/debug.go deleted file mode 100644 index 4a60a2da7..000000000 --- a/internal/http/routes/api/v1/debug.go +++ /dev/null @@ -1,41 +0,0 @@ -package api_v1 - -import ( - "github.com/gin-gonic/gin" - "github.com/go-shiori/shiori/internal/config" - "github.com/go-shiori/shiori/internal/http/response" - "github.com/go-shiori/shiori/internal/model" - "github.com/sirupsen/logrus" -) - -type DebugAPIRoutes struct { - logger *logrus.Logger - deps *config.Dependencies -} - -func (r *DebugAPIRoutes) Setup(group *gin.RouterGroup) model.Routes { - group.GET("/create_user", r.createUserHandler) - return r -} - -func (r *DebugAPIRoutes) createUserHandler(c *gin.Context) { - account := model.Account{ - Username: "shiori", - Password: "gopher", - Owner: true, - } - - if err := r.deps.Database.SaveAccount(c, account); err != nil { - response.SendError(c, 500, err.Error()) - return - } - - response.Send(c, 201, account) -} - -func NewDebugPIRoutes(logger *logrus.Logger, deps *config.Dependencies) *DebugAPIRoutes { - return &DebugAPIRoutes{ - logger: logger, - deps: deps, - } -} diff --git a/internal/http/routes/frontend.go b/internal/http/routes/frontend.go index 3c234dc7a..c963c6ce9 100644 --- a/internal/http/routes/frontend.go +++ b/internal/http/routes/frontend.go @@ -2,45 +2,82 @@ package routes import ( "embed" + "html/template" "net/http" - "time" + "path/filepath" "github.com/gin-contrib/gzip" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" "github.com/go-shiori/shiori/internal/config" - "github.com/go-shiori/shiori/internal/http/frontend" + views "github.com/go-shiori/shiori/internal/view" "github.com/sirupsen/logrus" ) -type frontendFS struct { +type assetsFS struct { http.FileSystem + logger *logrus.Logger } -func (fs frontendFS) Exists(prefix string, path string) bool { +func (fs assetsFS) Exists(prefix string, path string) bool { _, err := fs.Open(path) + if err != nil { + logrus.WithError(err).WithField("path", path).WithField("prefix", prefix).Error("requested frontend file not found") + } return err == nil } -func NewFrontendFS(fs embed.FS) static.ServeFileSystem { - return frontendFS{ +func (fs assetsFS) Open(name string) (http.File, error) { + f, err := fs.FileSystem.Open(filepath.Join("assets", name)) + if err != nil { + logrus.WithError(err).WithField("path", name).Error("requested frontend file not found") + } + return f, err +} + +func newAssetsFS(logger *logrus.Logger, fs embed.FS) static.ServeFileSystem { + return assetsFS{ + logger: logger, FileSystem: http.FS(fs), } } type FrontendRoutes struct { logger *logrus.Logger - maxAge time.Duration + cfg *config.Config +} + +func (r *FrontendRoutes) loadTemplates(e *gin.Engine) { + tmpl, err := template.New("html").Delims("$$", "$$").ParseFS(views.Templates, "*.html") + if err != nil { + r.logger.WithError(err).Error("Failed to parse templates") + return + } + e.SetHTMLTemplate(tmpl) } func (r *FrontendRoutes) Setup(e *gin.Engine) { - e.Use(gzip.Gzip(gzip.DefaultCompression)) - e.Use(static.Serve("/", NewFrontendFS(frontend.Assets))) + group := e.Group("/") + e.Delims("$$", "$$") + r.loadTemplates(e) + // e.LoadHTMLGlob("internal/view/*.html") + group.Use(gzip.Gzip(gzip.DefaultCompression)) + group.GET("/login", func(ctx *gin.Context) { + ctx.HTML(http.StatusOK, "login.html", gin.H{ + "RootPath": r.cfg.Http.RootPath, + }) + }) + group.GET("/", func(ctx *gin.Context) { + ctx.HTML(http.StatusOK, "index.html", gin.H{ + "RootPath": r.cfg.Http.RootPath, + }) + }) + e.StaticFS("/assets", newAssetsFS(r.logger, views.Assets)) } -func NewFrontendRoutes(logger *logrus.Logger, cfg config.HttpConfig) *FrontendRoutes { +func NewFrontendRoutes(logger *logrus.Logger, cfg *config.Config) *FrontendRoutes { return &FrontendRoutes{ logger: logger, - maxAge: cfg.Routes.Frontend.MaxAge, + cfg: cfg, } } diff --git a/internal/http/routes/frontend_test.go b/internal/http/routes/frontend_test.go index 7e442520c..8db1913bf 100644 --- a/internal/http/routes/frontend_test.go +++ b/internal/http/routes/frontend_test.go @@ -1,12 +1,13 @@ package routes import ( + "context" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" - "github.com/go-shiori/shiori/internal/config" + "github.com/go-shiori/shiori/internal/testutil" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" ) @@ -14,8 +15,10 @@ import ( func TestFrontendRoutes(t *testing.T) { logger := logrus.New() + cfg, _ := testutil.GetTestConfigurationAndDependencies(t, context.Background(), logger) + g := gin.Default() - router := NewFrontendRoutes(logger, config.HttpConfig{}) + router := NewFrontendRoutes(logger, cfg) router.Setup(g) t.Run("/", func(t *testing.T) { @@ -25,16 +28,16 @@ func TestFrontendRoutes(t *testing.T) { require.Equal(t, 200, w.Code) }) - t.Run("/login.html", func(t *testing.T) { + t.Run("/login", func(t *testing.T) { w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/login.html", nil) + req, _ := http.NewRequest("GET", "/login", nil) g.ServeHTTP(w, req) require.Equal(t, 200, w.Code) }) t.Run("/css/stylesheet.css", func(t *testing.T) { w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/css/stylesheet.css", nil) + req, _ := http.NewRequest("GET", "/assets/css/stylesheet.css", nil) g.ServeHTTP(w, req) require.Equal(t, 200, w.Code) }) diff --git a/internal/http/routes/legacy.go b/internal/http/routes/legacy.go new file mode 100644 index 000000000..7b7bea271 --- /dev/null +++ b/internal/http/routes/legacy.go @@ -0,0 +1,134 @@ +package routes + +import ( + "net/http" + "time" + + "github.com/gin-gonic/gin" + "github.com/go-shiori/shiori/internal/config" + "github.com/go-shiori/shiori/internal/model" + "github.com/go-shiori/shiori/internal/webserver" + "github.com/gofrs/uuid" + "github.com/julienschmidt/httprouter" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +type LegacyAPIRoutes struct { + logger *logrus.Logger + cfg *config.Config + deps *config.Dependencies + legacyHandler *webserver.Handler +} + +func (r *LegacyAPIRoutes) convertHttprouteParams(params gin.Params) httprouter.Params { + routerParams := httprouter.Params{} + for _, p := range params { + if p.Key == "filepath" { + r.logger.WithField("value", p.Value).Error("filepath") + } + routerParams = append(routerParams, httprouter.Param{ + Key: p.Key, + Value: p.Value, + }) + } + return routerParams +} + +func (r *LegacyAPIRoutes) handle(handler func(w http.ResponseWriter, r *http.Request, ps httprouter.Params)) gin.HandlerFunc { + return func(ctx *gin.Context) { + handler(ctx.Writer, ctx.Request, r.convertHttprouteParams(ctx.Params)) + } +} + +func (r *LegacyAPIRoutes) HandleLogin(account model.Account, expTime time.Duration) (string, error) { + // Create session ID + sessionID, err := uuid.NewV4() + if err != nil { + return "", errors.Wrap(err, "failed to create session ID") + } + + // Save session ID to cache + strSessionID := sessionID.String() + r.legacyHandler.SessionCache.Set(strSessionID, account, expTime) + + return strSessionID, nil +} + +func (r *LegacyAPIRoutes) HandleLogout(c *gin.Context) error { + sessionID := r.legacyHandler.GetSessionID(c.Request) + r.legacyHandler.SessionCache.Delete(sessionID) + return nil +} + +func (r *LegacyAPIRoutes) Setup(g *gin.Engine) { + r.legacyHandler = webserver.GetLegacyHandler(webserver.Config{ + DB: r.deps.Database, + DataDir: r.cfg.Storage.DataDir, + RootPath: r.cfg.Http.RootPath, + Log: false, // Already done by gin + }) + r.legacyHandler.PrepareSessionCache() + r.legacyHandler.PrepareTemplates() + + legacyGroup := g.Group("/") + + // Use a custom recovery handler to expose the errors that the frontend catch to redirect to + // the login page and display messages. + // This will be improved in the new API. + legacyGroup.Use(gin.CustomRecovery(func(c *gin.Context, err any) { + c.Data(http.StatusInternalServerError, "text/plain", []byte(err.(error).Error())) + })) + + legacyGroup.POST("/api/logout", r.handle(r.legacyHandler.ApiLogout)) + + // router.GET(jp("/bookmark/:id/thumb"), withLogging(hdl.serveThumbnailImage)) + legacyGroup.GET("/bookmark/:id/thumb", r.handle(r.legacyHandler.ServeThumbnailImage)) + // router.GET(jp("/bookmark/:id/content"), withLogging(hdl.serveBookmarkContent)) + legacyGroup.GET("/bookmark/:id/content", r.handle(r.legacyHandler.ServeBookmarkContent)) + // router.GET(jp("/bookmark/:id/ebook"), withLogging(hdl.serveBookmarkEbook)) + legacyGroup.GET("/bookmark/:id/ebook", r.handle(r.legacyHandler.ServeBookmarkEbook)) + // router.GET(jp("/bookmark/:id/archive/*filepath"), withLogging(hdl.serveBookmarkArchive)) + legacyGroup.GET("/bookmark/:id/archive/*filepath", r.handle(r.legacyHandler.ServeBookmarkArchive)) + // legacyGroup.GET("/bookmark/:id/archive/", r.handle(r.legacyHandler.ServeBookmarkArchive)) + + // router.GET(jp("/api/tags"), withLogging(hdl.apiGetTags)) + legacyGroup.GET("/api/tags", r.handle(r.legacyHandler.ApiGetTags)) + // router.PUT(jp("/api/tag"), withLogging(hdl.apiRenameTag)) + legacyGroup.PUT("/api/tags", r.handle(r.legacyHandler.ApiRenameTag)) + // router.GET(jp("/api/bookmarks"), withLogging(hdl.apiGetBookmarks)) + legacyGroup.GET("/api/bookmarks", r.handle(r.legacyHandler.ApiGetBookmarks)) + // router.POST(jp("/api/bookmarks"), withLogging(hdl.apiInsertBookmark)) + legacyGroup.POST("/api/bookmarks", r.handle(r.legacyHandler.ApiInsertBookmark)) + // router.DELETE(jp("/api/bookmarks"), withLogging(hdl.apiDeleteBookmark)) + legacyGroup.DELETE("/api/bookmarks", r.handle(r.legacyHandler.ApiDeleteBookmark)) + // router.PUT(jp("/api/bookmarks"), withLogging(hdl.apiUpdateBookmark)) + legacyGroup.PUT("/api/bookmarks", r.handle(r.legacyHandler.ApiUpdateBookmark)) + // router.PUT(jp("/api/cache"), withLogging(hdl.apiUpdateCache)) + legacyGroup.PUT("/api/cache", r.handle(r.legacyHandler.ApiUpdateCache)) + // router.PUT(jp("/api/ebook"), withLogging(hdl.apiDownloadEbook)) + legacyGroup.PUT("/api/ebook", r.handle(r.legacyHandler.ApiDownloadEbook)) + // router.PUT(jp("/api/bookmarks/tags"), withLogging(hdl.apiUpdateBookmarkTags)) + legacyGroup.PUT("/api/bookmarks/tags", r.handle(r.legacyHandler.ApiUpdateBookmarkTags)) + // router.POST(jp("/api/bookmarks/ext"), withLogging(hdl.apiInsertViaExtension)) + legacyGroup.POST("/api/bookmarks/ext", r.handle(r.legacyHandler.ApiInsertViaExtension)) + // router.DELETE(jp("/api/bookmarks/ext"), withLogging(hdl.apiDeleteViaExtension)) + legacyGroup.DELETE("/api/bookmarks/ext", r.handle(r.legacyHandler.ApiDeleteViaExtension)) + + // router.GET(jp("/api/accounts"), withLogging(hdl.apiGetAccounts)) + legacyGroup.GET("/api/accounts", r.handle(r.legacyHandler.ApiGetAccounts)) + // router.PUT(jp("/api/accounts"), withLogging(hdl.apiUpdateAccount)) + legacyGroup.PUT("/api/accounts", r.handle(r.legacyHandler.ApiUpdateAccount)) + // router.POST(jp("/api/accounts"), withLogging(hdl.apiInsertAccount)) + legacyGroup.POST("/api/accounts", r.handle(r.legacyHandler.ApiInsertAccount)) + // router.DELETE(jp("/api/accounts"), withLogging(hdl.apiDeleteAccount)) + legacyGroup.DELETE("/api/accounts", r.handle(r.legacyHandler.ApiDeleteAccount)) +} + +func NewLegacyAPIRoutes(logger *logrus.Logger, deps *config.Dependencies, cfg *config.Config) *LegacyAPIRoutes { + return &LegacyAPIRoutes{ + logger: logger, + cfg: cfg, + deps: deps, + } +} diff --git a/internal/http/server.go b/internal/http/server.go index c03563f98..ac7412ab3 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -25,26 +25,40 @@ type HttpServer struct { logger *logrus.Logger } -func (s *HttpServer) Setup(cfg config.HttpConfig, deps *config.Dependencies) *HttpServer { +func (s *HttpServer) Setup(cfg *config.Config, deps *config.Dependencies) *HttpServer { + if !cfg.Development { + gin.SetMode(gin.ReleaseMode) + } + + s.engine = gin.New() + + s.engine.Use(requestid.New()) + + if cfg.Http.AccessLog { + s.engine.Use(ginlogrus.Logger(deps.Log)) + } + s.engine.Use( - requestid.New(), - ginlogrus.Logger(deps.Log), middleware.AuthMiddleware(deps), gin.Recovery(), ) - if !deps.Config.Development { - gin.SetMode(gin.ReleaseMode) + if cfg.Http.ServeWebUI { + routes.NewFrontendRoutes(s.logger, cfg).Setup(s.engine) } + // LegacyRoutes will be here until we migrate everything from internal/webserver to this new + // package. + legacyRoutes := routes.NewLegacyAPIRoutes(s.logger, deps, cfg) + legacyRoutes.Setup(s.engine) + s.handle("/system", routes.NewSystemRoutes(s.logger)) - s.handle("/bookmark", routes.NewBookmarkRoutes(s.logger, deps)) - s.handle("/api/v1", api_v1.NewAPIRoutes(s.logger, deps)) + // s.handle("/bookmark", routes.NewBookmarkRoutes(s.logger, deps)) + s.handle("/api/v1", api_v1.NewAPIRoutes(s.logger, deps, legacyRoutes.HandleLogin)) s.handle("/swagger", routes.NewSwaggerAPIRoutes(s.logger)) - routes.NewFrontendRoutes(s.logger, cfg).Setup(s.engine) s.http.Handler = s.engine - s.http.Addr = fmt.Sprintf("%s%d", cfg.Address, cfg.Port) + s.http.Addr = fmt.Sprintf("%s%d", cfg.Http.Address, cfg.Http.Port) return s } @@ -81,10 +95,9 @@ func (s *HttpServer) WaitStop(ctx context.Context) { } } -func NewHttpServer(logger *logrus.Logger, cfg config.HttpConfig, dependencies *config.Dependencies) *HttpServer { +func NewHttpServer(logger *logrus.Logger) *HttpServer { return &HttpServer{ logger: logger, http: &http.Server{}, - engine: gin.New(), } } diff --git a/internal/model/account.go b/internal/model/account.go new file mode 100644 index 000000000..ab5ddee20 --- /dev/null +++ b/internal/model/account.go @@ -0,0 +1,25 @@ +package model + +// Account is the database model for account. +type Account struct { + ID int `db:"id" json:"id"` + Username string `db:"username" json:"username"` + Password string `db:"password" json:"password,omitempty"` + Owner bool `db:"owner" json:"owner"` +} + +// ToDTO converts Account to AccountDTO. +func (a Account) ToDTO() AccountDTO { + return AccountDTO{ + ID: a.ID, + Username: a.Username, + Owner: a.Owner, + } +} + +// AccountDTO is data transfer object for Account. +type AccountDTO struct { + ID int `json:"id"` + Username string `json:"username"` + Owner bool `json:"owner"` +} diff --git a/internal/model/legacy.go b/internal/model/legacy.go new file mode 100644 index 000000000..4042537cd --- /dev/null +++ b/internal/model/legacy.go @@ -0,0 +1,5 @@ +package model + +import "time" + +type LegacyLoginHandler func(account Account, expTime time.Duration) (string, error) diff --git a/internal/model/model.go b/internal/model/model.go index 8b65d5d9a..f3d739e4c 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -27,11 +27,3 @@ type Bookmark struct { CreateArchive bool `json:"createArchive"` CreateEbook bool `json:"createEbook"` } - -// Account is person that allowed to access web interface. -type Account struct { - ID int `db:"id" json:"id"` - Username string `db:"username" json:"username"` - Password string `db:"password" json:"password,omitempty"` - Owner bool `db:"owner" json:"owner"` -} diff --git a/internal/testutil/shiori.go b/internal/testutil/shiori.go index 3285c718e..eb97e2850 100644 --- a/internal/testutil/shiori.go +++ b/internal/testutil/shiori.go @@ -26,12 +26,12 @@ func GetTestConfigurationAndDependencies(t *testing.T, ctx context.Context, logg require.NoError(t, err) require.NoError(t, db.Migrate()) - cfg.Http.Storage.DataDir = tempDir + cfg.Storage.DataDir = tempDir deps := config.NewDependencies(logger, db, cfg) deps.Database = db deps.Domains.Auth = domains.NewAccountsDomain(logger, cfg.Http.SecretKey, db) - deps.Domains.Archiver = domains.NewArchiverDomain(logger, cfg.Http.Storage.DataDir) + deps.Domains.Archiver = domains.NewArchiverDomain(logger, cfg.Storage.DataDir) return cfg, deps } diff --git a/internal/view/css/archive.css b/internal/view/assets/css/archive.css similarity index 100% rename from internal/view/css/archive.css rename to internal/view/assets/css/archive.css diff --git a/internal/http/frontend/css/bookmark-item.css b/internal/view/assets/css/bookmark-item.css similarity index 100% rename from internal/http/frontend/css/bookmark-item.css rename to internal/view/assets/css/bookmark-item.css diff --git a/internal/http/frontend/css/custom-dialog.css b/internal/view/assets/css/custom-dialog.css similarity index 100% rename from internal/http/frontend/css/custom-dialog.css rename to internal/view/assets/css/custom-dialog.css diff --git a/internal/http/frontend/css/fontawesome.min.css b/internal/view/assets/css/fontawesome.min.css similarity index 100% rename from internal/http/frontend/css/fontawesome.min.css rename to internal/view/assets/css/fontawesome.min.css diff --git a/internal/http/frontend/css/fonts/fa-brands-400.eot b/internal/view/assets/css/fonts/fa-brands-400.eot similarity index 100% rename from internal/http/frontend/css/fonts/fa-brands-400.eot rename to internal/view/assets/css/fonts/fa-brands-400.eot diff --git a/internal/http/frontend/css/fonts/fa-brands-400.svg b/internal/view/assets/css/fonts/fa-brands-400.svg similarity index 100% rename from internal/http/frontend/css/fonts/fa-brands-400.svg rename to internal/view/assets/css/fonts/fa-brands-400.svg diff --git a/internal/http/frontend/css/fonts/fa-brands-400.ttf b/internal/view/assets/css/fonts/fa-brands-400.ttf similarity index 100% rename from internal/http/frontend/css/fonts/fa-brands-400.ttf rename to internal/view/assets/css/fonts/fa-brands-400.ttf diff --git a/internal/http/frontend/css/fonts/fa-brands-400.woff b/internal/view/assets/css/fonts/fa-brands-400.woff similarity index 100% rename from internal/http/frontend/css/fonts/fa-brands-400.woff rename to internal/view/assets/css/fonts/fa-brands-400.woff diff --git a/internal/http/frontend/css/fonts/fa-brands-400.woff2 b/internal/view/assets/css/fonts/fa-brands-400.woff2 similarity index 100% rename from internal/http/frontend/css/fonts/fa-brands-400.woff2 rename to internal/view/assets/css/fonts/fa-brands-400.woff2 diff --git a/internal/http/frontend/css/fonts/fa-regular-400.eot b/internal/view/assets/css/fonts/fa-regular-400.eot similarity index 100% rename from internal/http/frontend/css/fonts/fa-regular-400.eot rename to internal/view/assets/css/fonts/fa-regular-400.eot diff --git a/internal/http/frontend/css/fonts/fa-regular-400.svg b/internal/view/assets/css/fonts/fa-regular-400.svg similarity index 100% rename from internal/http/frontend/css/fonts/fa-regular-400.svg rename to internal/view/assets/css/fonts/fa-regular-400.svg diff --git a/internal/http/frontend/css/fonts/fa-regular-400.ttf b/internal/view/assets/css/fonts/fa-regular-400.ttf similarity index 100% rename from internal/http/frontend/css/fonts/fa-regular-400.ttf rename to internal/view/assets/css/fonts/fa-regular-400.ttf diff --git a/internal/http/frontend/css/fonts/fa-regular-400.woff b/internal/view/assets/css/fonts/fa-regular-400.woff similarity index 100% rename from internal/http/frontend/css/fonts/fa-regular-400.woff rename to internal/view/assets/css/fonts/fa-regular-400.woff diff --git a/internal/http/frontend/css/fonts/fa-regular-400.woff2 b/internal/view/assets/css/fonts/fa-regular-400.woff2 similarity index 100% rename from internal/http/frontend/css/fonts/fa-regular-400.woff2 rename to internal/view/assets/css/fonts/fa-regular-400.woff2 diff --git a/internal/http/frontend/css/fonts/fa-solid-900.eot b/internal/view/assets/css/fonts/fa-solid-900.eot similarity index 100% rename from internal/http/frontend/css/fonts/fa-solid-900.eot rename to internal/view/assets/css/fonts/fa-solid-900.eot diff --git a/internal/http/frontend/css/fonts/fa-solid-900.svg b/internal/view/assets/css/fonts/fa-solid-900.svg similarity index 100% rename from internal/http/frontend/css/fonts/fa-solid-900.svg rename to internal/view/assets/css/fonts/fa-solid-900.svg diff --git a/internal/http/frontend/css/fonts/fa-solid-900.ttf b/internal/view/assets/css/fonts/fa-solid-900.ttf similarity index 100% rename from internal/http/frontend/css/fonts/fa-solid-900.ttf rename to internal/view/assets/css/fonts/fa-solid-900.ttf diff --git a/internal/http/frontend/css/fonts/fa-solid-900.woff b/internal/view/assets/css/fonts/fa-solid-900.woff similarity index 100% rename from internal/http/frontend/css/fonts/fa-solid-900.woff rename to internal/view/assets/css/fonts/fa-solid-900.woff diff --git a/internal/http/frontend/css/fonts/fa-solid-900.woff2 b/internal/view/assets/css/fonts/fa-solid-900.woff2 similarity index 100% rename from internal/http/frontend/css/fonts/fa-solid-900.woff2 rename to internal/view/assets/css/fonts/fa-solid-900.woff2 diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-200.woff b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-200.woff similarity index 100% rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-200.woff rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-200.woff diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-200.woff2 b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-200.woff2 similarity index 100% rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-200.woff2 rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-200.woff2 diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-600.woff b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-600.woff similarity index 100% rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-600.woff rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-600.woff diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-600.woff2 b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-600.woff2 similarity index 100% rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-600.woff2 rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-600.woff2 diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-700.woff b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-700.woff similarity index 100% rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-700.woff rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-700.woff diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-700.woff2 b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-700.woff2 similarity index 100% rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-700.woff2 rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-700.woff2 diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-regular.woff b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-regular.woff similarity index 100% rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-regular.woff rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-regular.woff diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-regular.woff2 b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-regular.woff2 similarity index 100% rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-regular.woff2 rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-regular.woff2 diff --git a/internal/http/frontend/css/source-sans-pro.min.css b/internal/view/assets/css/source-sans-pro.min.css similarity index 100% rename from internal/http/frontend/css/source-sans-pro.min.css rename to internal/view/assets/css/source-sans-pro.min.css diff --git a/internal/http/frontend/css/stylesheet.css b/internal/view/assets/css/stylesheet.css similarity index 100% rename from internal/http/frontend/css/stylesheet.css rename to internal/view/assets/css/stylesheet.css diff --git a/internal/view/js/component/bookmark.js b/internal/view/assets/js/component/bookmark.js similarity index 100% rename from internal/view/js/component/bookmark.js rename to internal/view/assets/js/component/bookmark.js diff --git a/internal/http/frontend/js/component/dialog.js b/internal/view/assets/js/component/dialog.js similarity index 100% rename from internal/http/frontend/js/component/dialog.js rename to internal/view/assets/js/component/dialog.js diff --git a/internal/http/frontend/js/component/pagination.js b/internal/view/assets/js/component/pagination.js similarity index 100% rename from internal/http/frontend/js/component/pagination.js rename to internal/view/assets/js/component/pagination.js diff --git a/internal/http/frontend/js/dayjs.min.js b/internal/view/assets/js/dayjs.min.js similarity index 100% rename from internal/http/frontend/js/dayjs.min.js rename to internal/view/assets/js/dayjs.min.js diff --git a/internal/view/js/page/base.js b/internal/view/assets/js/page/base.js similarity index 100% rename from internal/view/js/page/base.js rename to internal/view/assets/js/page/base.js diff --git a/internal/view/js/page/home.js b/internal/view/assets/js/page/home.js similarity index 100% rename from internal/view/js/page/home.js rename to internal/view/assets/js/page/home.js diff --git a/internal/view/js/page/setting.js b/internal/view/assets/js/page/setting.js similarity index 100% rename from internal/view/js/page/setting.js rename to internal/view/assets/js/page/setting.js diff --git a/internal/http/frontend/js/url.js b/internal/view/assets/js/url.js similarity index 100% rename from internal/http/frontend/js/url.js rename to internal/view/assets/js/url.js diff --git a/internal/http/frontend/js/url.min.js b/internal/view/assets/js/url.min.js similarity index 100% rename from internal/http/frontend/js/url.min.js rename to internal/view/assets/js/url.min.js diff --git a/internal/http/frontend/js/vue.js b/internal/view/assets/js/vue.js similarity index 100% rename from internal/http/frontend/js/vue.js rename to internal/view/assets/js/vue.js diff --git a/internal/http/frontend/js/vue.min.js b/internal/view/assets/js/vue.min.js similarity index 100% rename from internal/http/frontend/js/vue.min.js rename to internal/view/assets/js/vue.min.js diff --git a/internal/view/less/archive.less b/internal/view/assets/less/archive.less similarity index 100% rename from internal/view/less/archive.less rename to internal/view/assets/less/archive.less diff --git a/internal/http/frontend/less/bookmark-item.less b/internal/view/assets/less/bookmark-item.less similarity index 100% rename from internal/http/frontend/less/bookmark-item.less rename to internal/view/assets/less/bookmark-item.less diff --git a/internal/http/frontend/less/custom-dialog.less b/internal/view/assets/less/custom-dialog.less similarity index 100% rename from internal/http/frontend/less/custom-dialog.less rename to internal/view/assets/less/custom-dialog.less diff --git a/internal/http/frontend/less/stylesheet.less b/internal/view/assets/less/stylesheet.less similarity index 100% rename from internal/http/frontend/less/stylesheet.less rename to internal/view/assets/less/stylesheet.less diff --git a/internal/http/frontend/res/apple-touch-icon-144x144.png b/internal/view/assets/res/apple-touch-icon-144x144.png similarity index 100% rename from internal/http/frontend/res/apple-touch-icon-144x144.png rename to internal/view/assets/res/apple-touch-icon-144x144.png diff --git a/internal/http/frontend/res/apple-touch-icon-152x152.png b/internal/view/assets/res/apple-touch-icon-152x152.png similarity index 100% rename from internal/http/frontend/res/apple-touch-icon-152x152.png rename to internal/view/assets/res/apple-touch-icon-152x152.png diff --git a/internal/http/frontend/res/favicon-16x16.png b/internal/view/assets/res/favicon-16x16.png similarity index 100% rename from internal/http/frontend/res/favicon-16x16.png rename to internal/view/assets/res/favicon-16x16.png diff --git a/internal/http/frontend/res/favicon-32x32.png b/internal/view/assets/res/favicon-32x32.png similarity index 100% rename from internal/http/frontend/res/favicon-32x32.png rename to internal/view/assets/res/favicon-32x32.png diff --git a/internal/http/frontend/res/favicon.ico b/internal/view/assets/res/favicon.ico similarity index 100% rename from internal/http/frontend/res/favicon.ico rename to internal/view/assets/res/favicon.ico diff --git a/internal/view/content.html b/internal/view/content.html index 9a9b89e65..308e3aeed 100644 --- a/internal/view/content.html +++ b/internal/view/content.html @@ -8,19 +8,19 @@ - - - - - + + + + + - - - - + + + + - - + + @@ -45,7 +45,7 @@ - + + @@ -42,10 +42,10 @@ - \ No newline at end of file + diff --git a/internal/view/js/component/dialog.js b/internal/view/js/component/dialog.js deleted file mode 100644 index 4c2836a77..000000000 --- a/internal/view/js/component/dialog.js +++ /dev/null @@ -1,238 +0,0 @@ -var template = ` -
-
-

{{title}}

-
- -

{{content}}

- -
-
- -
-
`; - -export default { - template: template, - props: { - title: String, - loading: Boolean, - visible: Boolean, - content: { - type: String, - default: '' - }, - fields: { - type: Array, - default() { - return [] - } - }, - showLabel: { - type: Boolean, - default: false - }, - mainText: { - type: String, - default: 'OK' - }, - secondText: String, - mainClick: { - type: Function, - default() { this.visible = false; } - }, - secondClick: { - type: Function, - default() { this.visible = false; } - }, - escPressed: { - type: Function, - default() { this.visible = false; } - } - }, - data() { - return { - formFields: [] - }; - }, - computed: { - btnTabIndex() { - return this.fields.length + 1; - } - }, - watch: { - fields: { - immediate: true, - handler() { - this.formFields = this.fields.map(field => { - if (typeof field === 'string') return { - name: field, - label: field, - value: '', - type: 'text', - dictionary: [], - separator: ' ', - suggestion: undefined - } - - if (typeof field === 'object') return { - name: field.name || '', - label: field.label || '', - value: field.value || '', - type: field.type || 'text', - dictionary: field.dictionary instanceof Array ? field.dictionary : [], - separator: field.separator || ' ', - suggestion: undefined - } - }); - } - }, - 'fields.length'() { - this.focus(); - }, - visible: { - immediate: true, - handler() { this.focus() } - } - }, - methods: { - fieldType(f) { - var type = f.type || 'text'; - if (type !== 'text' && type !== 'password') return 'text'; - else return type; - }, - handleMainClick() { - var data = {}; - this.formFields.forEach(field => { - var value = field.value; - if (field.type === 'number') value = parseInt(value, 10) || 0; - else if (field.type === 'float') value = parseFloat(value) || 0.0; - else if (field.type === 'check') value = Boolean(value); - data[field.name] = value; - }) - - this.mainClick(data); - }, - handleSecondClick() { - this.secondClick(); - }, - handleEscPressed() { - this.escPressed(); - }, - handleInput(index) { - // Create initial variable - var field = this.formFields[index], - dictionary = field.dictionary; - - // Make sure dictionary is not empty - if (dictionary.length === 0) return; - - // Fetch suggestion from dictionary - var words = field.value.split(field.separator), - lastWord = words[words.length - 1].toLowerCase(), - suggestion; - - if (lastWord !== '') { - suggestion = dictionary.find(word => { - return word.toLowerCase().startsWith(lastWord) - }); - } - - this.formFields[index].suggestion = suggestion; - - // Make sure suggestion exist - if (suggestion == null) return; - - // Display suggestion - this.$nextTick(() => { - var input = this.$refs.input[index], - span = this.$refs['suggestion-' + index][0], - inputRect = input.getBoundingClientRect(); - - span.style.top = (inputRect.bottom - 1) + 'px'; - span.style.left = inputRect.left + 'px'; - }); - }, - handleInputEnter(index) { - var suggestion = this.formFields[index].suggestion; - - if (suggestion == null) { - this.handleMainClick(); - return; - } - - var separator = this.formFields[index].separator, - words = this.formFields[index].value.split(separator); - - words.pop(); - words.push(suggestion); - - this.formFields[index].value = words.join(separator) + separator; - this.formFields[index].suggestion = undefined; - }, - focus() { - this.$nextTick(() => { - if (!this.visible) return; - - var fields = this.$refs.input, - otherInput = this.$el.querySelectorAll('input'), - button = this.$refs.mainButton; - - if (fields && fields.length > 0) { - this.$refs.input[0].focus(); - this.$refs.input[0].select(); - } else if (otherInput && otherInput.length > 0) { - otherInput[0].focus(); - otherInput[0].select(); - } else if (button) { - button.focus(); - } - }) - } - } -} \ No newline at end of file diff --git a/internal/view/js/component/pagination.js b/internal/view/js/component/pagination.js deleted file mode 100644 index d3b3e244d..000000000 --- a/internal/view/js/component/pagination.js +++ /dev/null @@ -1,44 +0,0 @@ -var template = ` -
-

Page

- -

{{maxPage}}

-
- -
` - -export default { - template: template, - props: { - page: Number, - maxPage: Number, - editMode: Boolean, - }, - methods: { - changePage(page) { - page = parseInt(page, 10) || 0; - if (page >= this.maxPage) page = this.maxPage; - else if (page <= 1) page = 1; - - this.$emit("change", page); - } - } -} \ No newline at end of file diff --git a/internal/view/js/dayjs.min.js b/internal/view/js/dayjs.min.js deleted file mode 100644 index 67df9899d..000000000 --- a/internal/view/js/dayjs.min.js +++ /dev/null @@ -1 +0,0 @@ -!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):t.dayjs=n()}(this,function(){"use strict";var t="millisecond",n="second",e="minute",r="hour",i="day",s="week",u="month",o="quarter",a="year",h=/^(\d{4})-?(\d{1,2})-?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d{1,3})?$/,f=/\[([^\]]+)]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,c=function(t,n,e){var r=String(t);return!r||r.length>=n?t:""+Array(n+1-r.length).join(e)+t},d={s:c,z:function(t){var n=-t.utcOffset(),e=Math.abs(n),r=Math.floor(e/60),i=e%60;return(n<=0?"+":"-")+c(r,2,"0")+":"+c(i,2,"0")},m:function(t,n){var e=12*(n.year()-t.year())+(n.month()-t.month()),r=t.clone().add(e,u),i=n-r<0,s=t.clone().add(e+(i?-1:1),u);return Number(-(e+(n-r)/(i?r-s:s-r))||0)},a:function(t){return t<0?Math.ceil(t)||0:Math.floor(t)},p:function(h){return{M:u,y:a,w:s,d:i,h:r,m:e,s:n,ms:t,Q:o}[h]||String(h||"").toLowerCase().replace(/s$/,"")},u:function(t){return void 0===t}},$={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_")},l="en",m={};m[l]=$;var y=function(t){return t instanceof v},M=function(t,n,e){var r;if(!t)return l;if("string"==typeof t)m[t]&&(r=t),n&&(m[t]=n,r=t);else{var i=t.name;m[i]=t,r=i}return e||(l=r),r},g=function(t,n,e){if(y(t))return t.clone();var r=n?"string"==typeof n?{format:n,pl:e}:n:{};return r.date=t,new v(r)},D=d;D.l=M,D.i=y,D.w=function(t,n){return g(t,{locale:n.$L,utc:n.$u})};var v=function(){function c(t){this.$L=this.$L||M(t.locale,null,!0),this.parse(t)}var d=c.prototype;return d.parse=function(t){this.$d=function(t){var n=t.date,e=t.utc;if(null===n)return new Date(NaN);if(D.u(n))return new Date;if(n instanceof Date)return new Date(n);if("string"==typeof n&&!/Z$/i.test(n)){var r=n.match(h);if(r)return e?new Date(Date.UTC(r[1],r[2]-1,r[3]||1,r[4]||0,r[5]||0,r[6]||0,r[7]||0)):new Date(r[1],r[2]-1,r[3]||1,r[4]||0,r[5]||0,r[6]||0,r[7]||0)}return new Date(n)}(t),this.init()},d.init=function(){var t=this.$d;this.$y=t.getFullYear(),this.$M=t.getMonth(),this.$D=t.getDate(),this.$W=t.getDay(),this.$H=t.getHours(),this.$m=t.getMinutes(),this.$s=t.getSeconds(),this.$ms=t.getMilliseconds()},d.$utils=function(){return D},d.isValid=function(){return!("Invalid Date"===this.$d.toString())},d.isSame=function(t,n){var e=g(t);return this.startOf(n)<=e&&e<=this.endOf(n)},d.isAfter=function(t,n){return g(t) - */ -(function (ns) { - 'use strict'; - - var RX_PROTOCOL = /^[a-z]+:/; - var RX_PORT = /[-a-z0-9]+(\.[-a-z0-9])*:\d+/i; - var RX_CREDS = /\/\/(.*?)(?::(.*?))?@/; - var RX_WIN = /^win/i; - var RX_PROTOCOL_REPL = /:$/; - var RX_QUERY_REPL = /^\?/; - var RX_HASH_REPL = /^#/; - var RX_PATH = /(.*\/)/; - var RX_PATH_FIX = /^\/{2,}/; - var RX_SINGLE_QUOTE = /'/g; - var RX_DECODE_1 = /%([ef][0-9a-f])%([89ab][0-9a-f])%([89ab][0-9a-f])/gi; - var RX_DECODE_2 = /%([cd][0-9a-f])%([89ab][0-9a-f])/gi; - var RX_DECODE_3 = /%([0-7][0-9a-f])/gi; - var RX_PLUS = /\+/g; - var RX_PATH_SEMI = /^\w:$/; - var RX_URL_TEST = /[^/#?]/; - - // configure given url options - function urlConfig(url) { - var config = { - path: true, - query: true, - hash: true - }; - - if (!url) { - return config; - } - - if (RX_PROTOCOL.test(url)) { - config.protocol = true; - config.host = true; - - if (RX_PORT.test(url)) { - config.port = true; - } - - if (RX_CREDS.test(url)) { - config.user = true; - config.pass = true; - } - } - - return config; - } - - var isNode = typeof window === 'undefined' && - typeof global !== 'undefined' && - typeof require === 'function'; - - // Trick to bypass Webpack's require at compile time - var nodeRequire = isNode ? ns['require'] : null; - - // mapping between what we want and element properties - var map = { - protocol: 'protocol', - host: 'hostname', - port: 'port', - path: 'pathname', - query: 'search', - hash: 'hash' - }; - - // jscs: disable - /** - * default ports as defined by http://url.spec.whatwg.org/#default-port - * We need them to fix IE behavior, @see https://github.com/Mikhus/jsurl/issues/2 - */ - // jscs: enable - var defaultPorts = { - ftp: 21, - gopher: 70, - http: 80, - https: 443, - ws: 80, - wss: 443 - }; - - var _currNodeUrl; - function getCurrUrl() { - if (isNode) { - if (!_currNodeUrl) { - _currNodeUrl = ('file://' + - (process.platform.match(RX_WIN) ? '/' : '') + - nodeRequire('fs').realpathSync('.') - ); - } - return _currNodeUrl; - } else { - return document.location.href; - } - } - - function parse(self, url, absolutize) { - var link, i, auth; - - if (!url) { - url = getCurrUrl(); - } - - if (isNode) { - link = nodeRequire('url').parse(url); - } - - else { - link = document.createElement('a'); - link.href = url; - } - - var config = urlConfig(url); - - auth = url.match(RX_CREDS) || []; - - for (i in map) { - if (config[i]) { - self[i] = link[map[i]] || ''; - } - - else { - self[i] = ''; - } - } - - // fix-up some parts - self.protocol = self.protocol.replace(RX_PROTOCOL_REPL, ''); - self.query = self.query.replace(RX_QUERY_REPL, ''); - self.hash = decode(self.hash.replace(RX_HASH_REPL, '')); - self.user = decode(auth[1] || ''); - self.pass = decode(auth[2] || ''); - /* jshint ignore:start */ - self.port = ( - // loosely compare because port can be a string - defaultPorts[self.protocol] == self.port || self.port == 0 - ) ? '' : self.port; // IE fix, Android browser fix - /* jshint ignore:end */ - - if (!config.protocol && RX_URL_TEST.test(url.charAt(0))) { - self.path = url.split('?')[0].split('#')[0]; - } - - if (!config.protocol && absolutize) { - // is IE and path is relative - var base = new Url(getCurrUrl().match(RX_PATH)[0]); - var basePath = base.path.split('/'); - var selfPath = self.path.split('/'); - var props = ['protocol', 'user', 'pass', 'host', 'port']; - var s = props.length; - - basePath.pop(); - - for (i = 0; i < s; i++) { - self[props[i]] = base[props[i]]; - } - - while (selfPath[0] === '..') { // skip all "../ - basePath.pop(); - selfPath.shift(); - } - - self.path = - (url.charAt(0) !== '/' ? basePath.join('/') : '') + - '/' + selfPath.join('/') - ; - } - - self.path = self.path.replace(RX_PATH_FIX, '/'); - - self.paths(self.paths()); - - self.query = new QueryString(self.query); - } - - function encode(s) { - return encodeURIComponent(s).replace(RX_SINGLE_QUOTE, '%27'); - } - - function decode(s) { - s = s.replace(RX_PLUS, ' '); - s = s.replace(RX_DECODE_1, function (code, hex1, hex2, hex3) { - var n1 = parseInt(hex1, 16) - 0xE0; - var n2 = parseInt(hex2, 16) - 0x80; - - if (n1 === 0 && n2 < 32) { - return code; - } - - var n3 = parseInt(hex3, 16) - 0x80; - var n = (n1 << 12) + (n2 << 6) + n3; - - if (n > 0xFFFF) { - return code; - } - - return String.fromCharCode(n); - }); - s = s.replace(RX_DECODE_2, function (code, hex1, hex2) { - var n1 = parseInt(hex1, 16) - 0xC0; - - if (n1 < 2) { - return code; - } - - var n2 = parseInt(hex2, 16) - 0x80; - - return String.fromCharCode((n1 << 6) + n2); - }); - - return s.replace(RX_DECODE_3, function (code, hex) { - return String.fromCharCode(parseInt(hex, 16)); - }); - } - - /** - * Class QueryString - * - * @param {string} qs - string representation of QueryString - * @constructor - */ - function QueryString(qs) { - var parts = qs.split('&'); - - for (var i = 0, s = parts.length; i < s; i++) { - var keyVal = parts[i].split('='); - var key = decodeURIComponent(keyVal[0].replace(RX_PLUS, ' ')); - - if (!key) { - continue; - } - - var value = keyVal[1] !== undefined ? decode(keyVal[1]) : null; - - if (typeof this[key] === 'undefined') { - this[key] = value; - } else { - if (!(this[key] instanceof Array)) { - this[key] = [this[key]]; - } - - this[key].push(value); - } - } - } - - /** - * Converts QueryString object back to string representation - * - * @returns {string} - */ - QueryString.prototype.toString = function () { - var s = ''; - var e = encode; - var i, ii; - - for (i in this) { - var w = this[i]; - - if (w instanceof Function || w === null) { - continue; - } - - if (w instanceof Array) { - var len = w.length; - - if (len) { - for (ii = 0; ii < len; ii++) { - var v = w[ii]; - s += s ? '&' : ''; - s += e(i) + (v === undefined || v === null - ? '' - : '=' + e(v)); - } - } - - else { - // parameter is an empty array, so treat as - // an empty argument - s += (s ? '&' : '') + e(i) + '='; - } - } - - else { - s += s ? '&' : ''; - s += e(i) + (w === undefined ? '' : '=' + e(w)); - } - } - - return s; - }; - - /** - * Class Url - * - * @param {string} [url] - string URL representation - * @param {boolean} [noTransform] - do not transform to absolute URL - * @constructor - */ - function Url(url, noTransform) { - parse(this, url, !noTransform); - } - - /** - * Clears QueryString, making it contain no params at all - * - * @returns {Url} - */ - Url.prototype.clearQuery = function () { - for (var key in this.query) { - if (!(this.query[key] instanceof Function)) { - delete this.query[key]; - } - } - - return this; - }; - - /** - * Returns total number of parameters in QueryString - * - * @returns {number} - */ - Url.prototype.queryLength = function () { - var count = 0; - - for (var key in this.query) { - if (!(this.query[key] instanceof Function)) { - count++; - } - } - - return count; - }; - - /** - * Returns true if QueryString contains no parameters, false otherwise - * - * @returns {boolean} - */ - Url.prototype.isEmptyQuery = function () { - return this.queryLength() === 0; - }; - - /** - * - * @param {Array} [paths] - an array pf path parts (if given will modify - * Url.path property - * @returns {Array} - an array representation of the Url.path property - */ - Url.prototype.paths = function (paths) { - var prefix = ''; - var i = 0; - var s; - - if (paths && paths.length && paths + '' !== paths) { - if (this.isAbsolute()) { - prefix = '/'; - } - - for (s = paths.length; i < s; i++) { - paths[i] = !i && RX_PATH_SEMI.test(paths[i]) - ? paths[i] - : encode(paths[i]); - } - - this.path = prefix + paths.join('/'); - } - - paths = (this.path.charAt(0) === '/' ? - this.path.slice(1) : this.path).split('/'); - - for (i = 0, s = paths.length; i < s; i++) { - paths[i] = decode(paths[i]); - } - - return paths; - }; - - /** - * Performs URL-specific encoding of the given string - * - * @method Url#encode - * @param {string} s - string to encode - * @returns {string} - */ - Url.prototype.encode = encode; - - /** - * Performs URL-specific decoding of the given encoded string - * - * @method Url#decode - * @param {string} s - string to decode - * @returns {string} - */ - Url.prototype.decode = decode; - - /** - * Checks if current URL is an absolute resource locator (globally absolute - * or absolute path to current server) - * - * @returns {boolean} - */ - Url.prototype.isAbsolute = function () { - return this.protocol || this.path.charAt(0) === '/'; - }; - - /** - * Returns string representation of current Url object - * - * @returns {string} - */ - Url.prototype.toString = function () { - return ( - (this.protocol && (this.protocol + '://')) + - (this.user && ( - encode(this.user) + (this.pass && (':' + encode(this.pass)) - ) + '@')) + - (this.host && this.host) + - (this.port && (':' + this.port)) + - (this.path && this.path) + - (this.query.toString() && ('?' + this.query)) + - (this.hash && ('#' + encode(this.hash))) - ); - }; - - ns[ns.exports ? 'exports' : 'Url'] = Url; -}(typeof module !== 'undefined' && module.exports ? module : window)); diff --git a/internal/view/js/url.min.js b/internal/view/js/url.min.js deleted file mode 100644 index 68170f441..000000000 --- a/internal/view/js/url.min.js +++ /dev/null @@ -1 +0,0 @@ -!function(t){"use strict";var y=/^[a-z]+:/,d=/[-a-z0-9]+(\.[-a-z0-9])*:\d+/i,v=/\/\/(.*?)(?::(.*?))?@/,r=/^win/i,g=/:$/,m=/^\?/,q=/^#/,w=/(.*\/)/,A=/^\/{2,}/,e=/'/g,o=/%([ef][0-9a-f])%([89ab][0-9a-f])%([89ab][0-9a-f])/gi,n=/%([cd][0-9a-f])%([89ab][0-9a-f])/gi,i=/%([0-7][0-9a-f])/gi,p=/\+/g,s=/^\w:$/,C=/[^/#?]/;var a,I="undefined"==typeof window&&"undefined"!=typeof global&&"function"==typeof require,S=I?t.require:null,b={protocol:"protocol",host:"hostname",port:"port",path:"pathname",query:"search",hash:"hash"},j={ftp:21,gopher:70,http:80,https:443,ws:80,wss:443};function x(){return I?(a||(a="file://"+(process.platform.match(r)?"/":"")+S("fs").realpathSync(".")),a):document.location.href}function h(t,r,e){var o,n,i;r||(r=x()),I?o=S("url").parse(r):(o=document.createElement("a")).href=r;var s,p,a=(p={path:!0,query:!0,hash:!0},(s=r)&&y.test(s)&&(p.protocol=!0,p.host=!0,d.test(s)&&(p.port=!0),v.test(s)&&(p.user=!0,p.pass=!0)),p);for(n in i=r.match(v)||[],b)a[n]?t[n]=o[b[n]]||"":t[n]="";if(t.protocol=t.protocol.replace(g,""),t.query=t.query.replace(m,""),t.hash=z(t.hash.replace(q,"")),t.user=z(i[1]||""),t.pass=z(i[2]||""),t.port=j[t.protocol]==t.port||0==t.port?"":t.port,!a.protocol&&C.test(r.charAt(0))&&(t.path=r.split("?")[0].split("#")[0]),!a.protocol&&e){var h=new U(x().match(w)[0]),u=h.path.split("/"),c=t.path.split("/"),f=["protocol","user","pass","host","port"],l=f.length;for(u.pop(),n=0;n= 0 && Math.floor(n) === n && isFinite(val) - } - - function isPromise (val) { - return ( - isDef(val) && - typeof val.then === 'function' && - typeof val.catch === 'function' - ) - } - - /** - * Convert a value to a string that is actually rendered. - */ - function toString (val) { - return val == null - ? '' - : Array.isArray(val) || (isPlainObject(val) && val.toString === _toString) - ? JSON.stringify(val, null, 2) - : String(val) - } - - /** - * Convert an input value to a number for persistence. - * If the conversion fails, return original string. - */ - function toNumber (val) { - var n = parseFloat(val); - return isNaN(n) ? val : n - } - - /** - * Make a map and return a function for checking if a key - * is in that map. - */ - function makeMap ( - str, - expectsLowerCase - ) { - var map = Object.create(null); - var list = str.split(','); - for (var i = 0; i < list.length; i++) { - map[list[i]] = true; - } - return expectsLowerCase - ? function (val) { return map[val.toLowerCase()]; } - : function (val) { return map[val]; } - } - - /** - * Check if a tag is a built-in tag. - */ - var isBuiltInTag = makeMap('slot,component', true); - - /** - * Check if an attribute is a reserved attribute. - */ - var isReservedAttribute = makeMap('key,ref,slot,slot-scope,is'); - - /** - * Remove an item from an array. - */ - function remove (arr, item) { - if (arr.length) { - var index = arr.indexOf(item); - if (index > -1) { - return arr.splice(index, 1) - } - } - } - - /** - * Check whether an object has the property. - */ - var hasOwnProperty = Object.prototype.hasOwnProperty; - function hasOwn (obj, key) { - return hasOwnProperty.call(obj, key) - } - - /** - * Create a cached version of a pure function. - */ - function cached (fn) { - var cache = Object.create(null); - return (function cachedFn (str) { - var hit = cache[str]; - return hit || (cache[str] = fn(str)) - }) - } - - /** - * Camelize a hyphen-delimited string. - */ - var camelizeRE = /-(\w)/g; - var camelize = cached(function (str) { - return str.replace(camelizeRE, function (_, c) { return c ? c.toUpperCase() : ''; }) - }); - - /** - * Capitalize a string. - */ - var capitalize = cached(function (str) { - return str.charAt(0).toUpperCase() + str.slice(1) - }); - - /** - * Hyphenate a camelCase string. - */ - var hyphenateRE = /\B([A-Z])/g; - var hyphenate = cached(function (str) { - return str.replace(hyphenateRE, '-$1').toLowerCase() - }); - - /** - * Simple bind polyfill for environments that do not support it, - * e.g., PhantomJS 1.x. Technically, we don't need this anymore - * since native bind is now performant enough in most browsers. - * But removing it would mean breaking code that was able to run in - * PhantomJS 1.x, so this must be kept for backward compatibility. - */ - - /* istanbul ignore next */ - function polyfillBind (fn, ctx) { - function boundFn (a) { - var l = arguments.length; - return l - ? l > 1 - ? fn.apply(ctx, arguments) - : fn.call(ctx, a) - : fn.call(ctx) - } - - boundFn._length = fn.length; - return boundFn - } - - function nativeBind (fn, ctx) { - return fn.bind(ctx) - } - - var bind = Function.prototype.bind - ? nativeBind - : polyfillBind; - - /** - * Convert an Array-like object to a real Array. - */ - function toArray (list, start) { - start = start || 0; - var i = list.length - start; - var ret = new Array(i); - while (i--) { - ret[i] = list[i + start]; - } - return ret - } - - /** - * Mix properties into target object. - */ - function extend (to, _from) { - for (var key in _from) { - to[key] = _from[key]; - } - return to - } - - /** - * Merge an Array of Objects into a single Object. - */ - function toObject (arr) { - var res = {}; - for (var i = 0; i < arr.length; i++) { - if (arr[i]) { - extend(res, arr[i]); - } - } - return res - } - - /* eslint-disable no-unused-vars */ - - /** - * Perform no operation. - * Stubbing args to make Flow happy without leaving useless transpiled code - * with ...rest (https://flow.org/blog/2017/05/07/Strict-Function-Call-Arity/). - */ - function noop (a, b, c) {} - - /** - * Always return false. - */ - var no = function (a, b, c) { return false; }; - - /* eslint-enable no-unused-vars */ - - /** - * Return the same value. - */ - var identity = function (_) { return _; }; - - /** - * Generate a string containing static keys from compiler modules. - */ - function genStaticKeys (modules) { - return modules.reduce(function (keys, m) { - return keys.concat(m.staticKeys || []) - }, []).join(',') - } - - /** - * Check if two values are loosely equal - that is, - * if they are plain objects, do they have the same shape? - */ - function looseEqual (a, b) { - if (a === b) { return true } - var isObjectA = isObject(a); - var isObjectB = isObject(b); - if (isObjectA && isObjectB) { - try { - var isArrayA = Array.isArray(a); - var isArrayB = Array.isArray(b); - if (isArrayA && isArrayB) { - return a.length === b.length && a.every(function (e, i) { - return looseEqual(e, b[i]) - }) - } else if (a instanceof Date && b instanceof Date) { - return a.getTime() === b.getTime() - } else if (!isArrayA && !isArrayB) { - var keysA = Object.keys(a); - var keysB = Object.keys(b); - return keysA.length === keysB.length && keysA.every(function (key) { - return looseEqual(a[key], b[key]) - }) - } else { - /* istanbul ignore next */ - return false - } - } catch (e) { - /* istanbul ignore next */ - return false - } - } else if (!isObjectA && !isObjectB) { - return String(a) === String(b) - } else { - return false - } - } - - /** - * Return the first index at which a loosely equal value can be - * found in the array (if value is a plain object, the array must - * contain an object of the same shape), or -1 if it is not present. - */ - function looseIndexOf (arr, val) { - for (var i = 0; i < arr.length; i++) { - if (looseEqual(arr[i], val)) { return i } - } - return -1 - } - - /** - * Ensure a function is called only once. - */ - function once (fn) { - var called = false; - return function () { - if (!called) { - called = true; - fn.apply(this, arguments); - } - } - } - - var SSR_ATTR = 'data-server-rendered'; - - var ASSET_TYPES = [ - 'component', - 'directive', - 'filter' - ]; - - var LIFECYCLE_HOOKS = [ - 'beforeCreate', - 'created', - 'beforeMount', - 'mounted', - 'beforeUpdate', - 'updated', - 'beforeDestroy', - 'destroyed', - 'activated', - 'deactivated', - 'errorCaptured', - 'serverPrefetch' - ]; - - /* */ - - - - var config = ({ - /** - * Option merge strategies (used in core/util/options) - */ - // $flow-disable-line - optionMergeStrategies: Object.create(null), - - /** - * Whether to suppress warnings. - */ - silent: false, - - /** - * Show production mode tip message on boot? - */ - productionTip: "development" !== 'production', - - /** - * Whether to enable devtools - */ - devtools: "development" !== 'production', - - /** - * Whether to record perf - */ - performance: false, - - /** - * Error handler for watcher errors - */ - errorHandler: null, - - /** - * Warn handler for watcher warns - */ - warnHandler: null, - - /** - * Ignore certain custom elements - */ - ignoredElements: [], - - /** - * Custom user key aliases for v-on - */ - // $flow-disable-line - keyCodes: Object.create(null), - - /** - * Check if a tag is reserved so that it cannot be registered as a - * component. This is platform-dependent and may be overwritten. - */ - isReservedTag: no, - - /** - * Check if an attribute is reserved so that it cannot be used as a component - * prop. This is platform-dependent and may be overwritten. - */ - isReservedAttr: no, - - /** - * Check if a tag is an unknown element. - * Platform-dependent. - */ - isUnknownElement: no, - - /** - * Get the namespace of an element - */ - getTagNamespace: noop, - - /** - * Parse the real tag name for the specific platform. - */ - parsePlatformTagName: identity, - - /** - * Check if an attribute must be bound using property, e.g. value - * Platform-dependent. - */ - mustUseProp: no, - - /** - * Perform updates asynchronously. Intended to be used by Vue Test Utils - * This will significantly reduce performance if set to false. - */ - async: true, - - /** - * Exposed for legacy reasons - */ - _lifecycleHooks: LIFECYCLE_HOOKS - }); - - /* */ - - /** - * unicode letters used for parsing html tags, component names and property paths. - * using https://www.w3.org/TR/html53/semantics-scripting.html#potentialcustomelementname - * skipping \u10000-\uEFFFF due to it freezing up PhantomJS - */ - var unicodeRegExp = /a-zA-Z\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD/; - - /** - * Check if a string starts with $ or _ - */ - function isReserved (str) { - var c = (str + '').charCodeAt(0); - return c === 0x24 || c === 0x5F - } - - /** - * Define a property. - */ - function def (obj, key, val, enumerable) { - Object.defineProperty(obj, key, { - value: val, - enumerable: !!enumerable, - writable: true, - configurable: true - }); - } - - /** - * Parse simple path. - */ - var bailRE = new RegExp(("[^" + (unicodeRegExp.source) + ".$_\\d]")); - function parsePath (path) { - if (bailRE.test(path)) { - return - } - var segments = path.split('.'); - return function (obj) { - for (var i = 0; i < segments.length; i++) { - if (!obj) { return } - obj = obj[segments[i]]; - } - return obj - } - } - - /* */ - - // can we use __proto__? - var hasProto = '__proto__' in {}; - - // Browser environment sniffing - var inBrowser = typeof window !== 'undefined'; - var inWeex = typeof WXEnvironment !== 'undefined' && !!WXEnvironment.platform; - var weexPlatform = inWeex && WXEnvironment.platform.toLowerCase(); - var UA = inBrowser && window.navigator.userAgent.toLowerCase(); - var isIE = UA && /msie|trident/.test(UA); - var isIE9 = UA && UA.indexOf('msie 9.0') > 0; - var isEdge = UA && UA.indexOf('edge/') > 0; - var isAndroid = (UA && UA.indexOf('android') > 0) || (weexPlatform === 'android'); - var isIOS = (UA && /iphone|ipad|ipod|ios/.test(UA)) || (weexPlatform === 'ios'); - var isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge; - var isPhantomJS = UA && /phantomjs/.test(UA); - var isFF = UA && UA.match(/firefox\/(\d+)/); - - // Firefox has a "watch" function on Object.prototype... - var nativeWatch = ({}).watch; - - var supportsPassive = false; - if (inBrowser) { - try { - var opts = {}; - Object.defineProperty(opts, 'passive', ({ - get: function get () { - /* istanbul ignore next */ - supportsPassive = true; - } - })); // https://github.com/facebook/flow/issues/285 - window.addEventListener('test-passive', null, opts); - } catch (e) {} - } - - // this needs to be lazy-evaled because vue may be required before - // vue-server-renderer can set VUE_ENV - var _isServer; - var isServerRendering = function () { - if (_isServer === undefined) { - /* istanbul ignore if */ - if (!inBrowser && !inWeex && typeof global !== 'undefined') { - // detect presence of vue-server-renderer and avoid - // Webpack shimming the process - _isServer = global['process'] && global['process'].env.VUE_ENV === 'server'; - } else { - _isServer = false; - } - } - return _isServer - }; - - // detect devtools - var devtools = inBrowser && window.__VUE_DEVTOOLS_GLOBAL_HOOK__; - - /* istanbul ignore next */ - function isNative (Ctor) { - return typeof Ctor === 'function' && /native code/.test(Ctor.toString()) - } - - var hasSymbol = - typeof Symbol !== 'undefined' && isNative(Symbol) && - typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys); - - var _Set; - /* istanbul ignore if */ // $flow-disable-line - if (typeof Set !== 'undefined' && isNative(Set)) { - // use native Set when available. - _Set = Set; - } else { - // a non-standard Set polyfill that only works with primitive keys. - _Set = /*@__PURE__*/(function () { - function Set () { - this.set = Object.create(null); - } - Set.prototype.has = function has (key) { - return this.set[key] === true - }; - Set.prototype.add = function add (key) { - this.set[key] = true; - }; - Set.prototype.clear = function clear () { - this.set = Object.create(null); - }; - - return Set; - }()); - } - - /* */ - - var warn = noop; - var tip = noop; - var generateComponentTrace = (noop); // work around flow check - var formatComponentName = (noop); - - { - var hasConsole = typeof console !== 'undefined'; - var classifyRE = /(?:^|[-_])(\w)/g; - var classify = function (str) { return str - .replace(classifyRE, function (c) { return c.toUpperCase(); }) - .replace(/[-_]/g, ''); }; - - warn = function (msg, vm) { - var trace = vm ? generateComponentTrace(vm) : ''; - - if (config.warnHandler) { - config.warnHandler.call(null, msg, vm, trace); - } else if (hasConsole && (!config.silent)) { - console.error(("[Vue warn]: " + msg + trace)); - } - }; - - tip = function (msg, vm) { - if (hasConsole && (!config.silent)) { - console.warn("[Vue tip]: " + msg + ( - vm ? generateComponentTrace(vm) : '' - )); - } - }; - - formatComponentName = function (vm, includeFile) { - if (vm.$root === vm) { - return '' - } - var options = typeof vm === 'function' && vm.cid != null - ? vm.options - : vm._isVue - ? vm.$options || vm.constructor.options - : vm; - var name = options.name || options._componentTag; - var file = options.__file; - if (!name && file) { - var match = file.match(/([^/\\]+)\.vue$/); - name = match && match[1]; - } - - return ( - (name ? ("<" + (classify(name)) + ">") : "") + - (file && includeFile !== false ? (" at " + file) : '') - ) - }; - - var repeat = function (str, n) { - var res = ''; - while (n) { - if (n % 2 === 1) { res += str; } - if (n > 1) { str += str; } - n >>= 1; - } - return res - }; - - generateComponentTrace = function (vm) { - if (vm._isVue && vm.$parent) { - var tree = []; - var currentRecursiveSequence = 0; - while (vm) { - if (tree.length > 0) { - var last = tree[tree.length - 1]; - if (last.constructor === vm.constructor) { - currentRecursiveSequence++; - vm = vm.$parent; - continue - } else if (currentRecursiveSequence > 0) { - tree[tree.length - 1] = [last, currentRecursiveSequence]; - currentRecursiveSequence = 0; - } - } - tree.push(vm); - vm = vm.$parent; - } - return '\n\nfound in\n\n' + tree - .map(function (vm, i) { return ("" + (i === 0 ? '---> ' : repeat(' ', 5 + i * 2)) + (Array.isArray(vm) - ? ((formatComponentName(vm[0])) + "... (" + (vm[1]) + " recursive calls)") - : formatComponentName(vm))); }) - .join('\n') - } else { - return ("\n\n(found in " + (formatComponentName(vm)) + ")") - } - }; - } - - /* */ - - var uid = 0; - - /** - * A dep is an observable that can have multiple - * directives subscribing to it. - */ - var Dep = function Dep () { - this.id = uid++; - this.subs = []; - }; - - Dep.prototype.addSub = function addSub (sub) { - this.subs.push(sub); - }; - - Dep.prototype.removeSub = function removeSub (sub) { - remove(this.subs, sub); - }; - - Dep.prototype.depend = function depend () { - if (Dep.target) { - Dep.target.addDep(this); - } - }; - - Dep.prototype.notify = function notify () { - // stabilize the subscriber list first - var subs = this.subs.slice(); - if (!config.async) { - // subs aren't sorted in scheduler if not running async - // we need to sort them now to make sure they fire in correct - // order - subs.sort(function (a, b) { return a.id - b.id; }); - } - for (var i = 0, l = subs.length; i < l; i++) { - subs[i].update(); - } - }; - - // The current target watcher being evaluated. - // This is globally unique because only one watcher - // can be evaluated at a time. - Dep.target = null; - var targetStack = []; - - function pushTarget (target) { - targetStack.push(target); - Dep.target = target; - } - - function popTarget () { - targetStack.pop(); - Dep.target = targetStack[targetStack.length - 1]; - } - - /* */ - - var VNode = function VNode ( - tag, - data, - children, - text, - elm, - context, - componentOptions, - asyncFactory - ) { - this.tag = tag; - this.data = data; - this.children = children; - this.text = text; - this.elm = elm; - this.ns = undefined; - this.context = context; - this.fnContext = undefined; - this.fnOptions = undefined; - this.fnScopeId = undefined; - this.key = data && data.key; - this.componentOptions = componentOptions; - this.componentInstance = undefined; - this.parent = undefined; - this.raw = false; - this.isStatic = false; - this.isRootInsert = true; - this.isComment = false; - this.isCloned = false; - this.isOnce = false; - this.asyncFactory = asyncFactory; - this.asyncMeta = undefined; - this.isAsyncPlaceholder = false; - }; - - var prototypeAccessors = { child: { configurable: true } }; - - // DEPRECATED: alias for componentInstance for backwards compat. - /* istanbul ignore next */ - prototypeAccessors.child.get = function () { - return this.componentInstance - }; - - Object.defineProperties( VNode.prototype, prototypeAccessors ); - - var createEmptyVNode = function (text) { - if ( text === void 0 ) text = ''; - - var node = new VNode(); - node.text = text; - node.isComment = true; - return node - }; - - function createTextVNode (val) { - return new VNode(undefined, undefined, undefined, String(val)) - } - - // optimized shallow clone - // used for static nodes and slot nodes because they may be reused across - // multiple renders, cloning them avoids errors when DOM manipulations rely - // on their elm reference. - function cloneVNode (vnode) { - var cloned = new VNode( - vnode.tag, - vnode.data, - // #7975 - // clone children array to avoid mutating original in case of cloning - // a child. - vnode.children && vnode.children.slice(), - vnode.text, - vnode.elm, - vnode.context, - vnode.componentOptions, - vnode.asyncFactory - ); - cloned.ns = vnode.ns; - cloned.isStatic = vnode.isStatic; - cloned.key = vnode.key; - cloned.isComment = vnode.isComment; - cloned.fnContext = vnode.fnContext; - cloned.fnOptions = vnode.fnOptions; - cloned.fnScopeId = vnode.fnScopeId; - cloned.asyncMeta = vnode.asyncMeta; - cloned.isCloned = true; - return cloned - } - - /* - * not type checking this file because flow doesn't play well with - * dynamically accessing methods on Array prototype - */ - - var arrayProto = Array.prototype; - var arrayMethods = Object.create(arrayProto); - - var methodsToPatch = [ - 'push', - 'pop', - 'shift', - 'unshift', - 'splice', - 'sort', - 'reverse' - ]; - - /** - * Intercept mutating methods and emit events - */ - methodsToPatch.forEach(function (method) { - // cache original method - var original = arrayProto[method]; - def(arrayMethods, method, function mutator () { - var args = [], len = arguments.length; - while ( len-- ) args[ len ] = arguments[ len ]; - - var result = original.apply(this, args); - var ob = this.__ob__; - var inserted; - switch (method) { - case 'push': - case 'unshift': - inserted = args; - break - case 'splice': - inserted = args.slice(2); - break - } - if (inserted) { ob.observeArray(inserted); } - // notify change - ob.dep.notify(); - return result - }); - }); - - /* */ - - var arrayKeys = Object.getOwnPropertyNames(arrayMethods); - - /** - * In some cases we may want to disable observation inside a component's - * update computation. - */ - var shouldObserve = true; - - function toggleObserving (value) { - shouldObserve = value; - } - - /** - * Observer class that is attached to each observed - * object. Once attached, the observer converts the target - * object's property keys into getter/setters that - * collect dependencies and dispatch updates. - */ - var Observer = function Observer (value) { - this.value = value; - this.dep = new Dep(); - this.vmCount = 0; - def(value, '__ob__', this); - if (Array.isArray(value)) { - if (hasProto) { - protoAugment(value, arrayMethods); - } else { - copyAugment(value, arrayMethods, arrayKeys); - } - this.observeArray(value); - } else { - this.walk(value); - } - }; - - /** - * Walk through all properties and convert them into - * getter/setters. This method should only be called when - * value type is Object. - */ - Observer.prototype.walk = function walk (obj) { - var keys = Object.keys(obj); - for (var i = 0; i < keys.length; i++) { - defineReactive$$1(obj, keys[i]); - } - }; - - /** - * Observe a list of Array items. - */ - Observer.prototype.observeArray = function observeArray (items) { - for (var i = 0, l = items.length; i < l; i++) { - observe(items[i]); - } - }; - - // helpers - - /** - * Augment a target Object or Array by intercepting - * the prototype chain using __proto__ - */ - function protoAugment (target, src) { - /* eslint-disable no-proto */ - target.__proto__ = src; - /* eslint-enable no-proto */ - } - - /** - * Augment a target Object or Array by defining - * hidden properties. - */ - /* istanbul ignore next */ - function copyAugment (target, src, keys) { - for (var i = 0, l = keys.length; i < l; i++) { - var key = keys[i]; - def(target, key, src[key]); - } - } - - /** - * Attempt to create an observer instance for a value, - * returns the new observer if successfully observed, - * or the existing observer if the value already has one. - */ - function observe (value, asRootData) { - if (!isObject(value) || value instanceof VNode) { - return - } - var ob; - if (hasOwn(value, '__ob__') && value.__ob__ instanceof Observer) { - ob = value.__ob__; - } else if ( - shouldObserve && - !isServerRendering() && - (Array.isArray(value) || isPlainObject(value)) && - Object.isExtensible(value) && - !value._isVue - ) { - ob = new Observer(value); - } - if (asRootData && ob) { - ob.vmCount++; - } - return ob - } - - /** - * Define a reactive property on an Object. - */ - function defineReactive$$1 ( - obj, - key, - val, - customSetter, - shallow - ) { - var dep = new Dep(); - - var property = Object.getOwnPropertyDescriptor(obj, key); - if (property && property.configurable === false) { - return - } - - // cater for pre-defined getter/setters - var getter = property && property.get; - var setter = property && property.set; - if ((!getter || setter) && arguments.length === 2) { - val = obj[key]; - } - - var childOb = !shallow && observe(val); - Object.defineProperty(obj, key, { - enumerable: true, - configurable: true, - get: function reactiveGetter () { - var value = getter ? getter.call(obj) : val; - if (Dep.target) { - dep.depend(); - if (childOb) { - childOb.dep.depend(); - if (Array.isArray(value)) { - dependArray(value); - } - } - } - return value - }, - set: function reactiveSetter (newVal) { - var value = getter ? getter.call(obj) : val; - /* eslint-disable no-self-compare */ - if (newVal === value || (newVal !== newVal && value !== value)) { - return - } - /* eslint-enable no-self-compare */ - if (customSetter) { - customSetter(); - } - // #7981: for accessor properties without setter - if (getter && !setter) { return } - if (setter) { - setter.call(obj, newVal); - } else { - val = newVal; - } - childOb = !shallow && observe(newVal); - dep.notify(); - } - }); - } - - /** - * Set a property on an object. Adds the new property and - * triggers change notification if the property doesn't - * already exist. - */ - function set (target, key, val) { - if (isUndef(target) || isPrimitive(target) - ) { - warn(("Cannot set reactive property on undefined, null, or primitive value: " + ((target)))); - } - if (Array.isArray(target) && isValidArrayIndex(key)) { - target.length = Math.max(target.length, key); - target.splice(key, 1, val); - return val - } - if (key in target && !(key in Object.prototype)) { - target[key] = val; - return val - } - var ob = (target).__ob__; - if (target._isVue || (ob && ob.vmCount)) { - warn( - 'Avoid adding reactive properties to a Vue instance or its root $data ' + - 'at runtime - declare it upfront in the data option.' - ); - return val - } - if (!ob) { - target[key] = val; - return val - } - defineReactive$$1(ob.value, key, val); - ob.dep.notify(); - return val - } - - /** - * Delete a property and trigger change if necessary. - */ - function del (target, key) { - if (isUndef(target) || isPrimitive(target) - ) { - warn(("Cannot delete reactive property on undefined, null, or primitive value: " + ((target)))); - } - if (Array.isArray(target) && isValidArrayIndex(key)) { - target.splice(key, 1); - return - } - var ob = (target).__ob__; - if (target._isVue || (ob && ob.vmCount)) { - warn( - 'Avoid deleting properties on a Vue instance or its root $data ' + - '- just set it to null.' - ); - return - } - if (!hasOwn(target, key)) { - return - } - delete target[key]; - if (!ob) { - return - } - ob.dep.notify(); - } - - /** - * Collect dependencies on array elements when the array is touched, since - * we cannot intercept array element access like property getters. - */ - function dependArray (value) { - for (var e = (void 0), i = 0, l = value.length; i < l; i++) { - e = value[i]; - e && e.__ob__ && e.__ob__.dep.depend(); - if (Array.isArray(e)) { - dependArray(e); - } - } - } - - /* */ - - /** - * Option overwriting strategies are functions that handle - * how to merge a parent option value and a child option - * value into the final value. - */ - var strats = config.optionMergeStrategies; - - /** - * Options with restrictions - */ - { - strats.el = strats.propsData = function (parent, child, vm, key) { - if (!vm) { - warn( - "option \"" + key + "\" can only be used during instance " + - 'creation with the `new` keyword.' - ); - } - return defaultStrat(parent, child) - }; - } - - /** - * Helper that recursively merges two data objects together. - */ - function mergeData (to, from) { - if (!from) { return to } - var key, toVal, fromVal; - - var keys = hasSymbol - ? Reflect.ownKeys(from) - : Object.keys(from); - - for (var i = 0; i < keys.length; i++) { - key = keys[i]; - // in case the object is already observed... - if (key === '__ob__') { continue } - toVal = to[key]; - fromVal = from[key]; - if (!hasOwn(to, key)) { - set(to, key, fromVal); - } else if ( - toVal !== fromVal && - isPlainObject(toVal) && - isPlainObject(fromVal) - ) { - mergeData(toVal, fromVal); - } - } - return to - } - - /** - * Data - */ - function mergeDataOrFn ( - parentVal, - childVal, - vm - ) { - if (!vm) { - // in a Vue.extend merge, both should be functions - if (!childVal) { - return parentVal - } - if (!parentVal) { - return childVal - } - // when parentVal & childVal are both present, - // we need to return a function that returns the - // merged result of both functions... no need to - // check if parentVal is a function here because - // it has to be a function to pass previous merges. - return function mergedDataFn () { - return mergeData( - typeof childVal === 'function' ? childVal.call(this, this) : childVal, - typeof parentVal === 'function' ? parentVal.call(this, this) : parentVal - ) - } - } else { - return function mergedInstanceDataFn () { - // instance merge - var instanceData = typeof childVal === 'function' - ? childVal.call(vm, vm) - : childVal; - var defaultData = typeof parentVal === 'function' - ? parentVal.call(vm, vm) - : parentVal; - if (instanceData) { - return mergeData(instanceData, defaultData) - } else { - return defaultData - } - } - } - } - - strats.data = function ( - parentVal, - childVal, - vm - ) { - if (!vm) { - if (childVal && typeof childVal !== 'function') { - warn( - 'The "data" option should be a function ' + - 'that returns a per-instance value in component ' + - 'definitions.', - vm - ); - - return parentVal - } - return mergeDataOrFn(parentVal, childVal) - } - - return mergeDataOrFn(parentVal, childVal, vm) - }; - - /** - * Hooks and props are merged as arrays. - */ - function mergeHook ( - parentVal, - childVal - ) { - var res = childVal - ? parentVal - ? parentVal.concat(childVal) - : Array.isArray(childVal) - ? childVal - : [childVal] - : parentVal; - return res - ? dedupeHooks(res) - : res - } - - function dedupeHooks (hooks) { - var res = []; - for (var i = 0; i < hooks.length; i++) { - if (res.indexOf(hooks[i]) === -1) { - res.push(hooks[i]); - } - } - return res - } - - LIFECYCLE_HOOKS.forEach(function (hook) { - strats[hook] = mergeHook; - }); - - /** - * Assets - * - * When a vm is present (instance creation), we need to do - * a three-way merge between constructor options, instance - * options and parent options. - */ - function mergeAssets ( - parentVal, - childVal, - vm, - key - ) { - var res = Object.create(parentVal || null); - if (childVal) { - assertObjectType(key, childVal, vm); - return extend(res, childVal) - } else { - return res - } - } - - ASSET_TYPES.forEach(function (type) { - strats[type + 's'] = mergeAssets; - }); - - /** - * Watchers. - * - * Watchers hashes should not overwrite one - * another, so we merge them as arrays. - */ - strats.watch = function ( - parentVal, - childVal, - vm, - key - ) { - // work around Firefox's Object.prototype.watch... - if (parentVal === nativeWatch) { parentVal = undefined; } - if (childVal === nativeWatch) { childVal = undefined; } - /* istanbul ignore if */ - if (!childVal) { return Object.create(parentVal || null) } - { - assertObjectType(key, childVal, vm); - } - if (!parentVal) { return childVal } - var ret = {}; - extend(ret, parentVal); - for (var key$1 in childVal) { - var parent = ret[key$1]; - var child = childVal[key$1]; - if (parent && !Array.isArray(parent)) { - parent = [parent]; - } - ret[key$1] = parent - ? parent.concat(child) - : Array.isArray(child) ? child : [child]; - } - return ret - }; - - /** - * Other object hashes. - */ - strats.props = - strats.methods = - strats.inject = - strats.computed = function ( - parentVal, - childVal, - vm, - key - ) { - if (childVal && "development" !== 'production') { - assertObjectType(key, childVal, vm); - } - if (!parentVal) { return childVal } - var ret = Object.create(null); - extend(ret, parentVal); - if (childVal) { extend(ret, childVal); } - return ret - }; - strats.provide = mergeDataOrFn; - - /** - * Default strategy. - */ - var defaultStrat = function (parentVal, childVal) { - return childVal === undefined - ? parentVal - : childVal - }; - - /** - * Validate component names - */ - function checkComponents (options) { - for (var key in options.components) { - validateComponentName(key); - } - } - - function validateComponentName (name) { - if (!new RegExp(("^[a-zA-Z][\\-\\.0-9_" + (unicodeRegExp.source) + "]*$")).test(name)) { - warn( - 'Invalid component name: "' + name + '". Component names ' + - 'should conform to valid custom element name in html5 specification.' - ); - } - if (isBuiltInTag(name) || config.isReservedTag(name)) { - warn( - 'Do not use built-in or reserved HTML elements as component ' + - 'id: ' + name - ); - } - } - - /** - * Ensure all props option syntax are normalized into the - * Object-based format. - */ - function normalizeProps (options, vm) { - var props = options.props; - if (!props) { return } - var res = {}; - var i, val, name; - if (Array.isArray(props)) { - i = props.length; - while (i--) { - val = props[i]; - if (typeof val === 'string') { - name = camelize(val); - res[name] = { type: null }; - } else { - warn('props must be strings when using array syntax.'); - } - } - } else if (isPlainObject(props)) { - for (var key in props) { - val = props[key]; - name = camelize(key); - res[name] = isPlainObject(val) - ? val - : { type: val }; - } - } else { - warn( - "Invalid value for option \"props\": expected an Array or an Object, " + - "but got " + (toRawType(props)) + ".", - vm - ); - } - options.props = res; - } - - /** - * Normalize all injections into Object-based format - */ - function normalizeInject (options, vm) { - var inject = options.inject; - if (!inject) { return } - var normalized = options.inject = {}; - if (Array.isArray(inject)) { - for (var i = 0; i < inject.length; i++) { - normalized[inject[i]] = { from: inject[i] }; - } - } else if (isPlainObject(inject)) { - for (var key in inject) { - var val = inject[key]; - normalized[key] = isPlainObject(val) - ? extend({ from: key }, val) - : { from: val }; - } - } else { - warn( - "Invalid value for option \"inject\": expected an Array or an Object, " + - "but got " + (toRawType(inject)) + ".", - vm - ); - } - } - - /** - * Normalize raw function directives into object format. - */ - function normalizeDirectives (options) { - var dirs = options.directives; - if (dirs) { - for (var key in dirs) { - var def$$1 = dirs[key]; - if (typeof def$$1 === 'function') { - dirs[key] = { bind: def$$1, update: def$$1 }; - } - } - } - } - - function assertObjectType (name, value, vm) { - if (!isPlainObject(value)) { - warn( - "Invalid value for option \"" + name + "\": expected an Object, " + - "but got " + (toRawType(value)) + ".", - vm - ); - } - } - - /** - * Merge two option objects into a new one. - * Core utility used in both instantiation and inheritance. - */ - function mergeOptions ( - parent, - child, - vm - ) { - { - checkComponents(child); - } - - if (typeof child === 'function') { - child = child.options; - } - - normalizeProps(child, vm); - normalizeInject(child, vm); - normalizeDirectives(child); - - // Apply extends and mixins on the child options, - // but only if it is a raw options object that isn't - // the result of another mergeOptions call. - // Only merged options has the _base property. - if (!child._base) { - if (child.extends) { - parent = mergeOptions(parent, child.extends, vm); - } - if (child.mixins) { - for (var i = 0, l = child.mixins.length; i < l; i++) { - parent = mergeOptions(parent, child.mixins[i], vm); - } - } - } - - var options = {}; - var key; - for (key in parent) { - mergeField(key); - } - for (key in child) { - if (!hasOwn(parent, key)) { - mergeField(key); - } - } - function mergeField (key) { - var strat = strats[key] || defaultStrat; - options[key] = strat(parent[key], child[key], vm, key); - } - return options - } - - /** - * Resolve an asset. - * This function is used because child instances need access - * to assets defined in its ancestor chain. - */ - function resolveAsset ( - options, - type, - id, - warnMissing - ) { - /* istanbul ignore if */ - if (typeof id !== 'string') { - return - } - var assets = options[type]; - // check local registration variations first - if (hasOwn(assets, id)) { return assets[id] } - var camelizedId = camelize(id); - if (hasOwn(assets, camelizedId)) { return assets[camelizedId] } - var PascalCaseId = capitalize(camelizedId); - if (hasOwn(assets, PascalCaseId)) { return assets[PascalCaseId] } - // fallback to prototype chain - var res = assets[id] || assets[camelizedId] || assets[PascalCaseId]; - if (warnMissing && !res) { - warn( - 'Failed to resolve ' + type.slice(0, -1) + ': ' + id, - options - ); - } - return res - } - - /* */ - - - - function validateProp ( - key, - propOptions, - propsData, - vm - ) { - var prop = propOptions[key]; - var absent = !hasOwn(propsData, key); - var value = propsData[key]; - // boolean casting - var booleanIndex = getTypeIndex(Boolean, prop.type); - if (booleanIndex > -1) { - if (absent && !hasOwn(prop, 'default')) { - value = false; - } else if (value === '' || value === hyphenate(key)) { - // only cast empty string / same name to boolean if - // boolean has higher priority - var stringIndex = getTypeIndex(String, prop.type); - if (stringIndex < 0 || booleanIndex < stringIndex) { - value = true; - } - } - } - // check default value - if (value === undefined) { - value = getPropDefaultValue(vm, prop, key); - // since the default value is a fresh copy, - // make sure to observe it. - var prevShouldObserve = shouldObserve; - toggleObserving(true); - observe(value); - toggleObserving(prevShouldObserve); - } - { - assertProp(prop, key, value, vm, absent); - } - return value - } - - /** - * Get the default value of a prop. - */ - function getPropDefaultValue (vm, prop, key) { - // no default, return undefined - if (!hasOwn(prop, 'default')) { - return undefined - } - var def = prop.default; - // warn against non-factory defaults for Object & Array - if (isObject(def)) { - warn( - 'Invalid default value for prop "' + key + '": ' + - 'Props with type Object/Array must use a factory function ' + - 'to return the default value.', - vm - ); - } - // the raw prop value was also undefined from previous render, - // return previous default value to avoid unnecessary watcher trigger - if (vm && vm.$options.propsData && - vm.$options.propsData[key] === undefined && - vm._props[key] !== undefined - ) { - return vm._props[key] - } - // call factory function for non-Function types - // a value is Function if its prototype is function even across different execution context - return typeof def === 'function' && getType(prop.type) !== 'Function' - ? def.call(vm) - : def - } - - /** - * Assert whether a prop is valid. - */ - function assertProp ( - prop, - name, - value, - vm, - absent - ) { - if (prop.required && absent) { - warn( - 'Missing required prop: "' + name + '"', - vm - ); - return - } - if (value == null && !prop.required) { - return - } - var type = prop.type; - var valid = !type || type === true; - var expectedTypes = []; - if (type) { - if (!Array.isArray(type)) { - type = [type]; - } - for (var i = 0; i < type.length && !valid; i++) { - var assertedType = assertType(value, type[i]); - expectedTypes.push(assertedType.expectedType || ''); - valid = assertedType.valid; - } - } - - if (!valid) { - warn( - getInvalidTypeMessage(name, value, expectedTypes), - vm - ); - return - } - var validator = prop.validator; - if (validator) { - if (!validator(value)) { - warn( - 'Invalid prop: custom validator check failed for prop "' + name + '".', - vm - ); - } - } - } - - var simpleCheckRE = /^(String|Number|Boolean|Function|Symbol)$/; - - function assertType (value, type) { - var valid; - var expectedType = getType(type); - if (simpleCheckRE.test(expectedType)) { - var t = typeof value; - valid = t === expectedType.toLowerCase(); - // for primitive wrapper objects - if (!valid && t === 'object') { - valid = value instanceof type; - } - } else if (expectedType === 'Object') { - valid = isPlainObject(value); - } else if (expectedType === 'Array') { - valid = Array.isArray(value); - } else { - valid = value instanceof type; - } - return { - valid: valid, - expectedType: expectedType - } - } - - /** - * Use function string name to check built-in types, - * because a simple equality check will fail when running - * across different vms / iframes. - */ - function getType (fn) { - var match = fn && fn.toString().match(/^\s*function (\w+)/); - return match ? match[1] : '' - } - - function isSameType (a, b) { - return getType(a) === getType(b) - } - - function getTypeIndex (type, expectedTypes) { - if (!Array.isArray(expectedTypes)) { - return isSameType(expectedTypes, type) ? 0 : -1 - } - for (var i = 0, len = expectedTypes.length; i < len; i++) { - if (isSameType(expectedTypes[i], type)) { - return i - } - } - return -1 - } - - function getInvalidTypeMessage (name, value, expectedTypes) { - var message = "Invalid prop: type check failed for prop \"" + name + "\"." + - " Expected " + (expectedTypes.map(capitalize).join(', ')); - var expectedType = expectedTypes[0]; - var receivedType = toRawType(value); - var expectedValue = styleValue(value, expectedType); - var receivedValue = styleValue(value, receivedType); - // check if we need to specify expected value - if (expectedTypes.length === 1 && - isExplicable(expectedType) && - !isBoolean(expectedType, receivedType)) { - message += " with value " + expectedValue; - } - message += ", got " + receivedType + " "; - // check if we need to specify received value - if (isExplicable(receivedType)) { - message += "with value " + receivedValue + "."; - } - return message - } - - function styleValue (value, type) { - if (type === 'String') { - return ("\"" + value + "\"") - } else if (type === 'Number') { - return ("" + (Number(value))) - } else { - return ("" + value) - } - } - - function isExplicable (value) { - var explicitTypes = ['string', 'number', 'boolean']; - return explicitTypes.some(function (elem) { return value.toLowerCase() === elem; }) - } - - function isBoolean () { - var args = [], len = arguments.length; - while ( len-- ) args[ len ] = arguments[ len ]; - - return args.some(function (elem) { return elem.toLowerCase() === 'boolean'; }) - } - - /* */ - - function handleError (err, vm, info) { - // Deactivate deps tracking while processing error handler to avoid possible infinite rendering. - // See: https://github.com/vuejs/vuex/issues/1505 - pushTarget(); - try { - if (vm) { - var cur = vm; - while ((cur = cur.$parent)) { - var hooks = cur.$options.errorCaptured; - if (hooks) { - for (var i = 0; i < hooks.length; i++) { - try { - var capture = hooks[i].call(cur, err, vm, info) === false; - if (capture) { return } - } catch (e) { - globalHandleError(e, cur, 'errorCaptured hook'); - } - } - } - } - } - globalHandleError(err, vm, info); - } finally { - popTarget(); - } - } - - function invokeWithErrorHandling ( - handler, - context, - args, - vm, - info - ) { - var res; - try { - res = args ? handler.apply(context, args) : handler.call(context); - if (res && !res._isVue && isPromise(res)) { - // issue #9511 - // reassign to res to avoid catch triggering multiple times when nested calls - res = res.catch(function (e) { return handleError(e, vm, info + " (Promise/async)"); }); - } - } catch (e) { - handleError(e, vm, info); - } - return res - } - - function globalHandleError (err, vm, info) { - if (config.errorHandler) { - try { - return config.errorHandler.call(null, err, vm, info) - } catch (e) { - // if the user intentionally throws the original error in the handler, - // do not log it twice - if (e !== err) { - logError(e, null, 'config.errorHandler'); - } - } - } - logError(err, vm, info); - } - - function logError (err, vm, info) { - { - warn(("Error in " + info + ": \"" + (err.toString()) + "\""), vm); - } - /* istanbul ignore else */ - if ((inBrowser || inWeex) && typeof console !== 'undefined') { - console.error(err); - } else { - throw err - } - } - - /* */ - - var isUsingMicroTask = false; - - var callbacks = []; - var pending = false; - - function flushCallbacks () { - pending = false; - var copies = callbacks.slice(0); - callbacks.length = 0; - for (var i = 0; i < copies.length; i++) { - copies[i](); - } - } - - // Here we have async deferring wrappers using microtasks. - // In 2.5 we used (macro) tasks (in combination with microtasks). - // However, it has subtle problems when state is changed right before repaint - // (e.g. #6813, out-in transitions). - // Also, using (macro) tasks in event handler would cause some weird behaviors - // that cannot be circumvented (e.g. #7109, #7153, #7546, #7834, #8109). - // So we now use microtasks everywhere, again. - // A major drawback of this tradeoff is that there are some scenarios - // where microtasks have too high a priority and fire in between supposedly - // sequential events (e.g. #4521, #6690, which have workarounds) - // or even between bubbling of the same event (#6566). - var timerFunc; - - // The nextTick behavior leverages the microtask queue, which can be accessed - // via either native Promise.then or MutationObserver. - // MutationObserver has wider support, however it is seriously bugged in - // UIWebView in iOS >= 9.3.3 when triggered in touch event handlers. It - // completely stops working after triggering a few times... so, if native - // Promise is available, we will use it: - /* istanbul ignore next, $flow-disable-line */ - if (typeof Promise !== 'undefined' && isNative(Promise)) { - var p = Promise.resolve(); - timerFunc = function () { - p.then(flushCallbacks); - // In problematic UIWebViews, Promise.then doesn't completely break, but - // it can get stuck in a weird state where callbacks are pushed into the - // microtask queue but the queue isn't being flushed, until the browser - // needs to do some other work, e.g. handle a timer. Therefore we can - // "force" the microtask queue to be flushed by adding an empty timer. - if (isIOS) { setTimeout(noop); } - }; - isUsingMicroTask = true; - } else if (!isIE && typeof MutationObserver !== 'undefined' && ( - isNative(MutationObserver) || - // PhantomJS and iOS 7.x - MutationObserver.toString() === '[object MutationObserverConstructor]' - )) { - // Use MutationObserver where native Promise is not available, - // e.g. PhantomJS, iOS7, Android 4.4 - // (#6466 MutationObserver is unreliable in IE11) - var counter = 1; - var observer = new MutationObserver(flushCallbacks); - var textNode = document.createTextNode(String(counter)); - observer.observe(textNode, { - characterData: true - }); - timerFunc = function () { - counter = (counter + 1) % 2; - textNode.data = String(counter); - }; - isUsingMicroTask = true; - } else if (typeof setImmediate !== 'undefined' && isNative(setImmediate)) { - // Fallback to setImmediate. - // Techinically it leverages the (macro) task queue, - // but it is still a better choice than setTimeout. - timerFunc = function () { - setImmediate(flushCallbacks); - }; - } else { - // Fallback to setTimeout. - timerFunc = function () { - setTimeout(flushCallbacks, 0); - }; - } - - function nextTick (cb, ctx) { - var _resolve; - callbacks.push(function () { - if (cb) { - try { - cb.call(ctx); - } catch (e) { - handleError(e, ctx, 'nextTick'); - } - } else if (_resolve) { - _resolve(ctx); - } - }); - if (!pending) { - pending = true; - timerFunc(); - } - // $flow-disable-line - if (!cb && typeof Promise !== 'undefined') { - return new Promise(function (resolve) { - _resolve = resolve; - }) - } - } - - /* */ - - var mark; - var measure; - - { - var perf = inBrowser && window.performance; - /* istanbul ignore if */ - if ( - perf && - perf.mark && - perf.measure && - perf.clearMarks && - perf.clearMeasures - ) { - mark = function (tag) { return perf.mark(tag); }; - measure = function (name, startTag, endTag) { - perf.measure(name, startTag, endTag); - perf.clearMarks(startTag); - perf.clearMarks(endTag); - // perf.clearMeasures(name) - }; - } - } - - /* not type checking this file because flow doesn't play well with Proxy */ - - var initProxy; - - { - var allowedGlobals = makeMap( - 'Infinity,undefined,NaN,isFinite,isNaN,' + - 'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' + - 'Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl,' + - 'require' // for Webpack/Browserify - ); - - var warnNonPresent = function (target, key) { - warn( - "Property or method \"" + key + "\" is not defined on the instance but " + - 'referenced during render. Make sure that this property is reactive, ' + - 'either in the data option, or for class-based components, by ' + - 'initializing the property. ' + - 'See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.', - target - ); - }; - - var warnReservedPrefix = function (target, key) { - warn( - "Property \"" + key + "\" must be accessed with \"$data." + key + "\" because " + - 'properties starting with "$" or "_" are not proxied in the Vue instance to ' + - 'prevent conflicts with Vue internals' + - 'See: https://vuejs.org/v2/api/#data', - target - ); - }; - - var hasProxy = - typeof Proxy !== 'undefined' && isNative(Proxy); - - if (hasProxy) { - var isBuiltInModifier = makeMap('stop,prevent,self,ctrl,shift,alt,meta,exact'); - config.keyCodes = new Proxy(config.keyCodes, { - set: function set (target, key, value) { - if (isBuiltInModifier(key)) { - warn(("Avoid overwriting built-in modifier in config.keyCodes: ." + key)); - return false - } else { - target[key] = value; - return true - } - } - }); - } - - var hasHandler = { - has: function has (target, key) { - var has = key in target; - var isAllowed = allowedGlobals(key) || - (typeof key === 'string' && key.charAt(0) === '_' && !(key in target.$data)); - if (!has && !isAllowed) { - if (key in target.$data) { warnReservedPrefix(target, key); } - else { warnNonPresent(target, key); } - } - return has || !isAllowed - } - }; - - var getHandler = { - get: function get (target, key) { - if (typeof key === 'string' && !(key in target)) { - if (key in target.$data) { warnReservedPrefix(target, key); } - else { warnNonPresent(target, key); } - } - return target[key] - } - }; - - initProxy = function initProxy (vm) { - if (hasProxy) { - // determine which proxy handler to use - var options = vm.$options; - var handlers = options.render && options.render._withStripped - ? getHandler - : hasHandler; - vm._renderProxy = new Proxy(vm, handlers); - } else { - vm._renderProxy = vm; - } - }; - } - - /* */ - - var seenObjects = new _Set(); - - /** - * Recursively traverse an object to evoke all converted - * getters, so that every nested property inside the object - * is collected as a "deep" dependency. - */ - function traverse (val) { - _traverse(val, seenObjects); - seenObjects.clear(); - } - - function _traverse (val, seen) { - var i, keys; - var isA = Array.isArray(val); - if ((!isA && !isObject(val)) || Object.isFrozen(val) || val instanceof VNode) { - return - } - if (val.__ob__) { - var depId = val.__ob__.dep.id; - if (seen.has(depId)) { - return - } - seen.add(depId); - } - if (isA) { - i = val.length; - while (i--) { _traverse(val[i], seen); } - } else { - keys = Object.keys(val); - i = keys.length; - while (i--) { _traverse(val[keys[i]], seen); } - } - } - - /* */ - - var normalizeEvent = cached(function (name) { - var passive = name.charAt(0) === '&'; - name = passive ? name.slice(1) : name; - var once$$1 = name.charAt(0) === '~'; // Prefixed last, checked first - name = once$$1 ? name.slice(1) : name; - var capture = name.charAt(0) === '!'; - name = capture ? name.slice(1) : name; - return { - name: name, - once: once$$1, - capture: capture, - passive: passive - } - }); - - function createFnInvoker (fns, vm) { - function invoker () { - var arguments$1 = arguments; - - var fns = invoker.fns; - if (Array.isArray(fns)) { - var cloned = fns.slice(); - for (var i = 0; i < cloned.length; i++) { - invokeWithErrorHandling(cloned[i], null, arguments$1, vm, "v-on handler"); - } - } else { - // return handler return value for single handlers - return invokeWithErrorHandling(fns, null, arguments, vm, "v-on handler") - } - } - invoker.fns = fns; - return invoker - } - - function updateListeners ( - on, - oldOn, - add, - remove$$1, - createOnceHandler, - vm - ) { - var name, def$$1, cur, old, event; - for (name in on) { - def$$1 = cur = on[name]; - old = oldOn[name]; - event = normalizeEvent(name); - if (isUndef(cur)) { - warn( - "Invalid handler for event \"" + (event.name) + "\": got " + String(cur), - vm - ); - } else if (isUndef(old)) { - if (isUndef(cur.fns)) { - cur = on[name] = createFnInvoker(cur, vm); - } - if (isTrue(event.once)) { - cur = on[name] = createOnceHandler(event.name, cur, event.capture); - } - add(event.name, cur, event.capture, event.passive, event.params); - } else if (cur !== old) { - old.fns = cur; - on[name] = old; - } - } - for (name in oldOn) { - if (isUndef(on[name])) { - event = normalizeEvent(name); - remove$$1(event.name, oldOn[name], event.capture); - } - } - } - - /* */ - - function mergeVNodeHook (def, hookKey, hook) { - if (def instanceof VNode) { - def = def.data.hook || (def.data.hook = {}); - } - var invoker; - var oldHook = def[hookKey]; - - function wrappedHook () { - hook.apply(this, arguments); - // important: remove merged hook to ensure it's called only once - // and prevent memory leak - remove(invoker.fns, wrappedHook); - } - - if (isUndef(oldHook)) { - // no existing hook - invoker = createFnInvoker([wrappedHook]); - } else { - /* istanbul ignore if */ - if (isDef(oldHook.fns) && isTrue(oldHook.merged)) { - // already a merged invoker - invoker = oldHook; - invoker.fns.push(wrappedHook); - } else { - // existing plain hook - invoker = createFnInvoker([oldHook, wrappedHook]); - } - } - - invoker.merged = true; - def[hookKey] = invoker; - } - - /* */ - - function extractPropsFromVNodeData ( - data, - Ctor, - tag - ) { - // we are only extracting raw values here. - // validation and default values are handled in the child - // component itself. - var propOptions = Ctor.options.props; - if (isUndef(propOptions)) { - return - } - var res = {}; - var attrs = data.attrs; - var props = data.props; - if (isDef(attrs) || isDef(props)) { - for (var key in propOptions) { - var altKey = hyphenate(key); - { - var keyInLowerCase = key.toLowerCase(); - if ( - key !== keyInLowerCase && - attrs && hasOwn(attrs, keyInLowerCase) - ) { - tip( - "Prop \"" + keyInLowerCase + "\" is passed to component " + - (formatComponentName(tag || Ctor)) + ", but the declared prop name is" + - " \"" + key + "\". " + - "Note that HTML attributes are case-insensitive and camelCased " + - "props need to use their kebab-case equivalents when using in-DOM " + - "templates. You should probably use \"" + altKey + "\" instead of \"" + key + "\"." - ); - } - } - checkProp(res, props, key, altKey, true) || - checkProp(res, attrs, key, altKey, false); - } - } - return res - } - - function checkProp ( - res, - hash, - key, - altKey, - preserve - ) { - if (isDef(hash)) { - if (hasOwn(hash, key)) { - res[key] = hash[key]; - if (!preserve) { - delete hash[key]; - } - return true - } else if (hasOwn(hash, altKey)) { - res[key] = hash[altKey]; - if (!preserve) { - delete hash[altKey]; - } - return true - } - } - return false - } - - /* */ - - // The template compiler attempts to minimize the need for normalization by - // statically analyzing the template at compile time. - // - // For plain HTML markup, normalization can be completely skipped because the - // generated render function is guaranteed to return Array. There are - // two cases where extra normalization is needed: - - // 1. When the children contains components - because a functional component - // may return an Array instead of a single root. In this case, just a simple - // normalization is needed - if any child is an Array, we flatten the whole - // thing with Array.prototype.concat. It is guaranteed to be only 1-level deep - // because functional components already normalize their own children. - function simpleNormalizeChildren (children) { - for (var i = 0; i < children.length; i++) { - if (Array.isArray(children[i])) { - return Array.prototype.concat.apply([], children) - } - } - return children - } - - // 2. When the children contains constructs that always generated nested Arrays, - // e.g.