Add file deletion (not work)

This commit is contained in:
cyber-dream 2023-05-05 01:11:11 +03:00
parent 70a163c4aa
commit ccaebdc667
2 changed files with 98 additions and 37 deletions

View File

@ -30,19 +30,19 @@ class Finder{
newWindow.innerHTML = html
this.fileView = new FileView(newWindow.querySelector(".FileTileView"),
(event) =>{ this.Open(event, false) },
(event) =>{ this.Click(event) },
(event) =>{ this.RightClick(event) },
(event) =>{ this.FileUploading(event) },
)
this.OpenDir(this.path)
newWindow.querySelector("#BackButton").addEventListener('click', () =>{
this.OpenPreviousDir()
})
// newWindow.querySelector("#BackButton").addEventListener('click', () =>{
// this.OpenPreviousDir()
// })
newWindow.querySelector("#HomeButton").addEventListener('click', () =>{
this.OpenDir(this.homePath)
})
// newWindow.querySelector("#HomeButton").addEventListener('click', () =>{
// this.OpenDir(this.homePath)
// })
this.windowElement = newWindow
@ -102,7 +102,7 @@ class Finder{
args[2].innerHTML = html
this.fileView = new FileView(args[2].querySelector(".FileTileView"), (event) =>{
this.Open(event, true)
this.Click(event, true)
})
this.OpenDir(this.path)
})
@ -111,55 +111,53 @@ class Finder{
})
}
OpenPreviousDir(){
if (this.pathHistory.length > 0){
// console.log(this.pathHistory)
let path = this.pathHistory[this.pathHistory.length - 1]
// console.log(typeof( this.pathHistory))
this.pathHistory.pop()
this.OpenDir(this.path)
}
}
// OpenPreviousDir(){
// if (this.pathHistory.length > 0){
// // console.log(this.pathHistory)
// let path = this.pathHistory[this.pathHistory.length - 1]
// // console.log(typeof( this.pathHistory))
// this.pathHistory.pop()
// this.OpenDir(this.path)
// }
// }
/**
* @param {string} path
*/
OpenDir(path){
this.pathHistory += this.path
this.path = path
this.fileView.OpenFolder(this.path)
}
/**
* @param {MouseEvent} event
* @param {boolean} inNewWindow
*/
Click(event, inNewWindow){
OpenNewDir(){
WebDesktopEnvironment.Open("finder", [this.path])
}
/**
* @param {MouseEvent} event
*/
RightClick(event){
this.CreateContextMenu(event.target, [event.clientY, event.clientX])
}
/**
* @param {MouseEvent} event
* @param {boolean} inNewWindow
*/
Open(event, inNewWindow){
Click(event){
let fileType = event.target.getAttribute("fileType")
let fileName = event.target.getAttribute("name")
switch (fileType) {
case "directory":
if (inNewWindow){
WebDesktopEnvironment.Open("finder", ["/home/user/" + fileName]) //FIXME this.path is shared for all windows
break
}
this.OpenDir(this.path +"/" + fileName)
WebDesktopEnvironment.Open("finder", [`${this.path}/${fileName}`]) //FIXME this.path is shared for all windows
break
case "blog-page":
WebDesktopEnvironment.Open("blog-viewer", [this.path + "/" + fileName])
WebDesktopEnvironment.Open("blog-viewer", [`${this.path}/${fileName}`])
break
case "deleteFile":
fetch(`/fs/delete` + new URLSearchParams({
path: "/home/user/" + fileName //FIXME
path: `${this.path}/${fileName}` //FIXME
}))
.then((response) => {
console.log(response.status)
@ -181,10 +179,6 @@ class Finder{
break;
}
}
RightClick(event){
this.CreateContextMenu(event.target, [event.clientY, event.clientX])
}
CreateContextMenu(target, pos){
let context = ""

View File

@ -103,7 +103,7 @@ func (fs *WebFileSystem) CreateDirectory(path string) error {
}
fs.CreateFile(&directory, parentPath)
// res, err := fs.webfsCollection.InsertOne(context.Background(), &directory)
// res, err := fs.webfsCollection.InsertOne(context.Background(), &directory) //TODO
// if err != nil {
// return err
// }
@ -113,6 +113,27 @@ func (fs *WebFileSystem) CreateDirectory(path string) error {
return nil
}
func (fs *WebFileSystem) UpdateFile(filePath string, update primitive.M) error {
file, err := fs.Read(filePath)
if err != nil {
return err
}
if file.MongoId.IsZero() {
return errors.New("mongo id is zero")
}
filter := primitive.M{
"_id": file.MongoId,
}
_, err = fs.webfsCollection.UpdateOne(context.Background(), filter, primitive.M{"$set": update})
if err != nil {
return err
}
return nil
}
func (fs *WebFileSystem) CreateFile(file *WebFSFile, parentPath string) error {
//TODO Check file existance
parentDir, err := fs.Read(parentPath)
@ -175,6 +196,37 @@ func (fs *WebFileSystem) GetParentPath(path string) string {
return parentPath
}
func (fs *WebFileSystem) Delete(filePath string) error {
splittedPath := fs.SplitPath(filePath)
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")
file, err := fs.Read(filePath)
if err != nil {
return err
}
parentDir, err := fs.Read(parentPath)
if err != nil {
return err
}
parentDir.Data.(primitive.D).Map()
// update:= primitive.M{
// "data.children":
// }
// filter := primitive.M{}
res, err := fs.webfsCollection.UpdateByID(context.Background(), parentDir.MongoId, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}})
// res, err := fs.webfsCollection.UpdateOne(context.Background(), filter, primitive.M{"$unset": bson.M{"data.children." + file.MongoId.String(): ""}})
if err != nil {
return err
}
if res.MatchedCount < 1 {
return errors.New("no documents found")
}
return nil
}
func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
route.POST("/upload", func(ctx *gin.Context) { //TODO To PUT request
// fileName := ctx.Query("fileName")
@ -291,6 +343,21 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
ctx.Status(http.StatusOK)
})
route.GET("delete", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.Status(http.StatusBadRequest) //TODO
return
}
err := fs.Delete(path)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
}
func (fs *WebFileSystem) Validate() error {