Create pathlinks

This commit is contained in:
cyber-dream 2023-05-16 15:12:55 +03:00
parent 2a5f0bb3f4
commit f363ebbe10
11 changed files with 248 additions and 137 deletions

View File

@ -34,46 +34,11 @@ func (f *FinderApplication) Render(isMobile bool) gin.H {
return gin.H{}
}
func (f *FinderApplication) RenderPrivateContextMenu(context string, filePath string, data string) gin.H {
islands := [][]wde.ContexMenuRow{}
islands = append(islands, []wde.ContexMenuRow{})
if context == "FileTileView" {
islands = append(islands, []wde.ContexMenuRow{
{Label: "Get Info", Action: "getInfo"},
{Label: "New Directory", Action: "createDir"},
})
return gin.H{
"Islands": islands,
}
}
switch context {
case "directory":
if f.fs.GetExtension(filePath) == "app" {
islands = append(islands, []wde.ContexMenuRow{
{Label: "Open as Directory", Action: "openAsDir"},
})
}
default:
islands = append(islands, []wde.ContexMenuRow{
{Label: "temp Menu 1", Action: ""},
{Label: "temp Menu 2", Action: ""},
})
}
islands = append(islands, []wde.ContexMenuRow{
{Label: "Delete File", Action: "deleteFile"},
})
return gin.H{
"Islands": islands,
}
}
func (f *FinderApplication) RenderPublicContextMenu(context string, filePath string, data string) gin.H {
islands := [][]wde.ContexMenuRow{}
islands = append(islands, []wde.ContexMenuRow{})
//TODO: Links read as source files in props info
islands = append(islands, []wde.ContexMenuRow{
{Label: "Get Info", Action: "getInfo"},
})
@ -93,54 +58,12 @@ func (f *FinderApplication) RenderPublicContextMenu(context string, filePath str
}
func (f *FinderApplication) RenderProps(filePath string) (gin.H, error) {
// file, err := f.fs.NewReadDeprecated(filePath)
// if err != nil {
// return nil
// }
fileHeader, err := f.fs.Read(filePath, nil)
if err != nil {
return nil, err
}
return gin.H{
// "fileId": fileHeader.MongoId,
// "fileDataId": fileHeader.Data,
"file": fileHeader,
}, nil
}
// func (f *FinderApplication) Routes(routes *gin.RouterGroup) {
// routes.GET("render", func(ctx *gin.Context) {
// isMobileParam := ctx.Query("isMobile")
// isMobile := isMobileParam == "true"
// admin := true
// if isMobile {
// ctx.HTML(http.StatusOK, "finder/mobile-app.tmpl", f.Render(isMobile))
// return
// }
// if admin {
// ctx.HTML(http.StatusOK, "finder/admin-app.tmpl", f.Render(isMobile))
// return
// }
// ctx.HTML(http.StatusOK, "finder/app.tmpl", f.Render(isMobile))
// })
// routes.GET("renderMobileDesktop", func(ctx *gin.Context) {
// ctx.HTML(http.StatusOK, "finder/mobile-desktop.tmpl", gin.H{})
// })
// routes.GET("renderDesktop", func(ctx *gin.Context) {
// path := ctx.Query("path")
// if path == "" {
// ctx.JSON(http.StatusBadRequest, "no path provided")
// return
// }
// ctx.HTML(http.StatusOK, "finder/desktop.tmpl", gin.H{})
// })
// routes.GET("contextMenu", func(ctx *gin.Context) {
// context := ctx.Query("context")
// data := ctx.Query("data")
// ginH := f.RenderContextMenu(context, data)
// ctx.HTML(http.StatusOK, "wde-widgets/context-menu.tmpl", ginH)
// })
// }

View File

@ -2,7 +2,10 @@ package finder
import (
"personalwebsite/apps"
"personalwebsite/wde"
"personalwebsite/webfilesystem"
"github.com/gin-gonic/gin"
)
type FinderAdminApp struct {
@ -20,3 +23,46 @@ func NewFinderAdminApp(webfs *webfilesystem.WebFileSystem) FinderAdminApp {
func (f *FinderAdminApp) RenderWindow() {
}
func (f *FinderApplication) RenderPrivateContextMenu(context string, filePath string, data string) gin.H {
islands := [][]wde.ContexMenuRow{}
islands = append(islands, []wde.ContexMenuRow{})
islands = append(islands, []wde.ContexMenuRow{
{Label: "Get Info", Action: "getInfo"},
})
if context == "FileTileView" {
islands = append(islands, []wde.ContexMenuRow{
{Label: "New Directory", Action: "createDir"},
})
return gin.H{
"Islands": islands,
}
}
islands = append(islands, []wde.ContexMenuRow{
{Label: "Create path link", Action: "createPathLink"},
})
switch context {
case "directory":
if f.fs.GetExtension(filePath) == "app" {
islands = append(islands, []wde.ContexMenuRow{
{Label: "Open as Directory", Action: "openAsDir"},
})
}
default:
islands = append(islands, []wde.ContexMenuRow{
{Label: "temp Menu 1", Action: ""},
{Label: "temp Menu 2", Action: ""},
})
}
islands = append(islands, []wde.ContexMenuRow{
{Label: "Delete File", Action: "deleteFile"},
})
return gin.H{
"Islands": islands,
}
}

View File

@ -26,17 +26,12 @@ func (f *FinderApplication) PublicRoutes(routes *gin.RouterGroup) {
routes.GET("contextMenu", func(ctx *gin.Context) {
context := ctx.Query("context")
// if context == "" {
// ctx.Status(http.StatusBadRequest)
// return
// }
filePath := ctx.Query("path")
if filePath == "" {
ctx.Status(http.StatusBadRequest)
return
}
data := ctx.Query("data")
// ginH := f.RenderPrivateContextMenu(context, filePath, data)
ginH := f.RenderPublicContextMenu(context, filePath, data)
ctx.HTML(http.StatusOK, "wde-widgets/context-menu.tmpl", ginH)
})

View File

@ -161,28 +161,40 @@ class FinderWindow{
* @param {MouseEvent} event
*/
Click(event){
let fileType = event.target.getAttribute("fileType")
let fileName = event.target.getAttribute("name")
this.OpenFile(this.curPath, event.target.getAttribute("name"), event.target.getAttribute("filetype"))
}
/**
* @param {string} filePath
*/
async OpenFile(parentPath, fileName, fileType){
console.log(parentPath, fileName, fileType)
// const splittedPath = filePath.split("/")
// const fileName = splittedPath[splittedPath.length - 1]
const fileExtension = fileName.split(".")[fileName.split(".").length - 1] //FIXME
switch (true) {
case fileType == "objectlink":
WebDesktopEnvironment.Alert("Links not supported yet")
break
case fileType == "pathlink":
let res = await WebFS.ReadPathLink(`${parentPath}/${fileName}`)
console.log(res)
this.OpenFile(res.parentPath, res.name, res.filetype)
break
case fileExtension == "app":
WebDesktopEnvironment.Open(`${this.curPath}/${fileName}`, [])
WebDesktopEnvironment.Open(`${parentPath}/${fileName}`, [])
break
case fileType == "directory":
WebDesktopEnvironment.Open(`/Applications/Finder.app`, [`${this.curPath}/${fileName}`])
WebDesktopEnvironment.Open(`/Applications/Finder.app`, [`${parentPath}/${fileName}`])
break
case fileExtension == "blog":
WebDesktopEnvironment.Open("/Applications/BlogViewer.app", [`${this.curPath}/${fileName}`])
WebDesktopEnvironment.Open("/Applications/BlogViewer.app", [`${parentPath}/${fileName}`])
break
case fileExtension == "jpeg" | fileExtension == "png":
WebDesktopEnvironment.Open("img-viewer", [this.curPath + "/" + fileName])
WebDesktopEnvironment.Open("img-viewer", [`${parentPath}/${fileName}`])
break;
default:
// console.log("Unsupported file type")
WebDesktopEnvironment.Alert("Unsupported file type")
break;
}
@ -245,6 +257,12 @@ class FinderWindow{
if (event.target.classList.contains("Row")){ //TODO add uuid id to rows to more accurate checks??
let res = false
switch (event.target.children[0].getAttribute("action")) {
case "createPathLink":
res = await WebFS.CreatePathLink(`${this.curPath}/${fileName}`, `${this.curPath}/Link ${fileName}` )
if (res){
this.ReRenderDir()
}
break
case "createDir":
res = await WebFS.CreateDirectory(`${this.curPath}`)
console.log(res)

View File

@ -272,7 +272,7 @@ class WebFS{
const params = new URLSearchParams({
path: `${path}/New Directory`
})
const response = await fetch(`/fs/createDir?` + params)
const response = await fetch(`/system/fs/createDir?` + params)
if (response.status != 200){
WebDesktopEnvironment.Alert("DIRCTORY CREATION ERROR") //TODO
return false
@ -288,7 +288,7 @@ class WebFS{
const params = new URLSearchParams({
path: path
})
const response = await fetch(`/fs/delete?` + params)
const response = await fetch(`/system/fs/delete?` + params)
if (response.status != 200){
WebDesktopEnvironment.Alert("DELETE ERROR") //TODO
return false
@ -305,11 +305,62 @@ class WebFS{
sourcePath: sourcePath,
targetPath: targetPath
})
const response = await fetch(`/fs/move?` + params)
const response = await fetch(`/system/fs/move?` + params)
if (response.status != 200){
// WebDesktopEnvironment.Alert("Move ERROR") //TODO
return false
}
return true
}
/**
* @param {string} linkPath
* @returns {string}
*/
static async ReadObjectLink(linkPath){
const params = new URLSearchParams({
linkPath: linkPath,
})
const response = await fetch(`/system/fs/readObjectLink?` + params)
if (response.status != 200){
WebDesktopEnvironment.Alert("TODO") //TODO
return ""
}
const path = await response.text()
//TODO Validate
return path
}
/**
* @param {string} sourcePath
* @param {string} linkPath
* @returns {string}
*/
static async CreatePathLink(sourcePath, linkPath){
const params = new URLSearchParams({
sourcePath: sourcePath,
linkPath: linkPath,
})
const response = await fetch(`/system/fs/createPathLink?` + params)
return response.status == 200
}
/**
* @param {string} linkPath
* @returns {string}
*/
static async ReadPathLink(linkPath){
const params = new URLSearchParams({
linkPath: linkPath,
})
const response = await fetch(`/system/fs/readPathLink?` + params)
if (response.status != 200){
WebDesktopEnvironment.Alert("TODO") //TODO
return ""
}
// console.log(response)
const file = await response.json()
//TODO Validate
return file
}
}

View File

@ -80,10 +80,10 @@ func PublicRoutes(webfs *webfilesystem.WebFileSystem, webde *wde.WDE, appsStorag
})
// fsGroup := systemGroup.Group("fs")
// {
// webfs.PublicRoutes(fsGroup)
// }
fsGroup := systemGroup.Group("fs")
{
webfs.PublicRoutes(fsGroup)
}
}
appGroup := router.Group("app")

View File

@ -20,6 +20,9 @@
<div>
file id : {{.file.MongoId}}
</div>
<div>
file type : {{.file.Type}}
</div>
<div>
file data id : {{.file.Data}}
</div>

View File

@ -21,30 +21,6 @@ func NewWDE(webFs *webfilesystem.WebFileSystem) *WDE {
type FilesWidget struct {
}
// Deprecated
func (w *WDE) Render(path string) (gin.H, error) {
// list, err := w.fs.NewListDeprecated(path)
// if err != nil {
// return nil, err
// }
return gin.H{
"Name": "Greg Brzezinski",
// "Files": list,
}, nil
}
// Deprecated
func (w *WDE) RenderContextMenu() (gin.H, error) {
// list, err := w.fs.List(path)
// if err != nil {
// return nil, err
// }
return gin.H{
"Name": "Greg Brzezinski",
// "Files": list,
}, nil
}
func (w *WDE) RenderFileTileView(directory string, host string) (gin.H, error) {
list, err := w.fs.ListDir(directory)
if err != nil {

View File

@ -8,7 +8,7 @@ func (fs *WebFileSystem) Read(filePath string, fileData interface{}) (*FileHeade
return nil, err
}
fileHeader, err := fs.readFSDocs(fileId, fileData)
fileHeader, err := fs.ReadByObjectID(fileId, fileData)
if err != nil {
return nil, err
}
@ -22,5 +22,19 @@ func (fs *WebFileSystem) ReadByObjectID(fileID primitive.ObjectID, fileData inte
return nil, err
}
if fileHeader.Type == "objectlink" {
objectLinkData := ObjectLinkFileData{}
_, err := fs.readFSDocs(fileID, &objectLinkData)
if err != nil {
return nil, err
}
fileHeader, err := fs.readFSDocs(objectLinkData.Link_id, fileData)
if err != nil {
return nil, err
}
return fileHeader, nil
}
return fileHeader, nil
}

View File

@ -7,6 +7,7 @@ import (
)
func (fs *WebFileSystem) PrivateRoutes(route *gin.RouterGroup) {
fs.PublicRoutes(route)
route.POST("/upload", func(ctx *gin.Context) { //TODO To PUT request
// fileName := ctx.Query("fileName")
// if fileName == "" {
@ -188,8 +189,67 @@ func (fs *WebFileSystem) PrivateRoutes(route *gin.RouterGroup) {
ctx.Status(http.StatusOK)
})
route.GET("readObjectLink", func(ctx *gin.Context) {
linkPath := ctx.Query("linkPath")
if linkPath == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
// file, err := fs.Read(linkPath, nil)
ctx.Status(http.StatusOK)
})
route.GET("createPathLink", func(ctx *gin.Context) {
sourcePath := ctx.Query("sourcePath")
if sourcePath == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
linkPath := ctx.Query("linkPath")
if linkPath == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
_, _, err := fs.CreatePathLink(sourcePath, linkPath)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
}
func (fs *WebFileSystem) PublicRoutes(route *gin.RouterGroup) {
route.GET("readPathLink", func(ctx *gin.Context) {
linkPath := ctx.Query("linkPath")
if linkPath == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
linkData := PathLinkFileData{}
linkHeader, err := fs.Read(linkPath, &linkData)
if err != nil {
ctx.Status(http.StatusBadRequest) //TODO
return
}
if linkHeader.Type != "pathlink" {
ctx.Status(http.StatusBadRequest)
return
}
FileHeader, err := fs.Read(linkData.Path, nil)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.JSON(http.StatusOK, FrontEndFile{
Name: FileHeader.Name,
Type: FileHeader.Type,
ParentPath: fs.GetParentPath(linkData.Path),
})
})
}

View File

@ -54,14 +54,15 @@ type ObjectLinkFileData struct {
Link_id primitive.ObjectID `bson:"link_id" json:"-"`
}
// Deprecated
func (fs *WebFileSystem) ReadHeader(fileID primitive.ObjectID) (*FileHeader, error) {
file := &FileHeader{}
filter := primitive.M{
"_id": fileID,
}
err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(file)
return file, err
type PathLinkFileData struct {
MongoId primitive.ObjectID `bson:"_id" json:"-"`
Path string `bson:"path" json:"path"`
}
type FrontEndFile struct {
Name string `json:"name"`
Type string `json:"filetype"` //TODO: Rename to Type
ParentPath string `json:"parentPath"`
}
//TODO To private, get name from path and set it to file struct, force set newObjectID
@ -224,12 +225,6 @@ func (fs *WebFileSystem) Remove(filePath string) error {
}
func (fs *WebFileSystem) CreateObjectLink(sourcePath string, linkPath string) error {
// sourceParentPath := fs.GetParentPath(sourcePath)
// sourceParentDirId, err := fs.FindFile(sourceParentPath)
// if err != nil {
// return err
// }
linkSplittedPath := fs.SplitPath(linkPath)
linkFileName := linkSplittedPath[len(linkSplittedPath)-1]
@ -267,6 +262,36 @@ func (fs *WebFileSystem) CreateObjectLink(sourcePath string, linkPath string) er
return nil
}
func (fs *WebFileSystem) CreatePathLink(sourcePath string, linkPath string) (primitive.ObjectID, primitive.ObjectID, error) {
sourceFileHeader, err := fs.Read(sourcePath, nil)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
if sourceFileHeader.Type == "pathlink" {
return primitive.NilObjectID, primitive.NilObjectID, errors.New("multiplies pathlinks not supported yet") //TODO
}
newLinkHeader := FileHeader{
MongoId: primitive.NewObjectID(),
Name: sourceFileHeader.Name,
Type: "pathlink",
Icon: "",
Data: primitive.NewObjectID(),
}
newLinkData := PathLinkFileData{
MongoId: primitive.NewObjectID(),
Path: sourcePath,
}
headerID, dataID, err := fs.Write(linkPath, &newLinkHeader, newLinkData)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
return headerID, dataID, nil
}
func (fs *WebFileSystem) SplitPath(path string) []string {
resPath := []string{}
splittedPath := strings.Split(path, "/")