init
This commit is contained in:
commit
fa6a4c650f
93 files changed
+6555
No files matched your search
@@ -0,0 +1,190 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type renamePathRequest struct {
|
||||
OldPath string `json:"old_path"`
|
||||
NewPath string `json:"new_path"`
|
||||
Recursive bool `json:"recursive"`
|
||||
}
|
||||
|
||||
type renamePreview struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
func (s *Server) handleRenamePath(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
var req renamePathRequest
|
||||
|
||||
err := json.NewDecoder(
|
||||
r.Body,
|
||||
).Decode(&req)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
400,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if req.OldPath == "" ||
|
||||
req.NewPath == "" {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
"invalid path",
|
||||
400,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if req.Recursive {
|
||||
|
||||
var count int
|
||||
|
||||
err :=
|
||||
s.store.DB.QueryRow(`
|
||||
SELECT COUNT(*)
|
||||
FROM paths
|
||||
WHERE path = ?
|
||||
OR path LIKE ?
|
||||
`,
|
||||
req.OldPath,
|
||||
req.OldPath+"/%",
|
||||
).Scan(&count)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Query().Get("preview") == "1" {
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"application/json",
|
||||
)
|
||||
|
||||
json.NewEncoder(w).Encode(
|
||||
renamePreview{
|
||||
Count: count,
|
||||
},
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
tx, err :=
|
||||
s.store.DB.BeginTx(
|
||||
r.Context(),
|
||||
&sql.TxOptions{},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`
|
||||
UPDATE paths
|
||||
SET path =
|
||||
CASE
|
||||
WHEN path = ?
|
||||
THEN ?
|
||||
ELSE REPLACE(
|
||||
path,
|
||||
? || '/',
|
||||
? || '/'
|
||||
)
|
||||
END
|
||||
WHERE path = ?
|
||||
OR path LIKE ?
|
||||
`,
|
||||
req.OldPath,
|
||||
req.NewPath,
|
||||
|
||||
req.OldPath,
|
||||
req.NewPath,
|
||||
|
||||
req.OldPath,
|
||||
req.OldPath+"/%",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
tx.Rollback()
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(200)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_, err =
|
||||
s.store.DB.Exec(`
|
||||
UPDATE paths
|
||||
SET path = ?
|
||||
WHERE path = ?
|
||||
`,
|
||||
req.NewPath,
|
||||
req.OldPath,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(200)
|
||||
}
|
||||
Reference in new issue
Block a user