Compare commits

...

3 Commits

Author SHA1 Message Date
0ceae10530 Port code base to new fs version 2023-05-07 04:00:22 +03:00
6c3bc32b59 massive commit for safe refactoring 2023-05-07 03:14:30 +03:00
1beba0f0ee Personal props 2023-05-06 01:50:57 +03:00
21 changed files with 517 additions and 637 deletions

2
.env
View File

@ -1,3 +1,3 @@
MONGO_CONNECT=mongodb://localhost:27017
DATABASE=personal-website
COLLECTION_WEBFS=webfs
COLLECTION_WEBFS=webfs2

View File

@ -33,15 +33,19 @@ func (b *BlogViewerApplication) GetId() string {
}
func (b *BlogViewerApplication) Route(route *gin.RouterGroup) {
// route.GET("writeMockBlog", func(ctx *gin.Context) {
// path := ctx.Query("path")
// if path == "" {
// ctx.JSON(http.StatusBadRequest, "no path provided")
// return
// }
// b.WriteMock(path)
// ctx.JSON(http.StatusOK, "OK")
// })
route.GET("writeMockBlog", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "no path provided")
return
}
err := b.WriteMock(path)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
ctx.JSON(http.StatusOK, "OK")
})
route.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
@ -67,56 +71,46 @@ func (b *BlogViewerApplication) Route(route *gin.RouterGroup) {
})
}
// func (b *BlogViewerApplication) WriteMock(path string) {
// blogFile := webfilesystem.WebFSFile{
// MongoId: primitive.NewObjectID(),
// Name: "test-1.blog",
// Type: "blog-page",
// Data: BlogFileData{
// Header: "OMG THIS IS BLOG",
// Blocks: []Block{
// {
// Type: "plain-text",
// Data: []string{
// "Apoqiwepoqiwepo",
// ".,mas;dakls;d",
// "q[poqwieqpipoi]",
// },
// },
// },
// },
// }
// err := b.fs.CreateFile(&blogFile, path)
// if err != nil {
// println(err.Error())
// }
// }
func (b *BlogViewerApplication) WriteMock(path string) error {
blogFileHeader := webfilesystem.FileHeader{
MongoId: primitive.NewObjectID(),
Name: "blog1.blog",
Type: "",
Icon: "",
Data: [12]byte{},
}
blogFileData := BlogFileData{
Header: "OMG THIS IS BLOG",
Blocks: []Block{
{
Type: "plain-text",
Data: []string{
"Apoqiwepoqiwepo",
".,mas;dakls;d",
"q[poqwieqpipoi]",
},
},
},
}
func (b *BlogViewerApplication) Render(path string, isMobile bool) (gin.H, error) {
file, err := b.fs.NewRead(path)
_, _, err := b.fs.Write("/home/user/blog1.blog", &blogFileHeader, blogFileData)
if err != nil {
println(err.Error())
return nil, err
return err
}
blogDataRaw := file.Data.(primitive.D).Map()
header := blogDataRaw["header"]
blocks := []Block{}
return nil
}
for _, rawBlock := range blogDataRaw["blocks"].(primitive.A) {
lel := rawBlock.(primitive.D).Map()
block := Block{
Type: lel["type"].(string),
Data: []string{},
}
for _, v := range lel["data"].(primitive.A) {
block.Data = append(block.Data, v.(string))
}
blocks = append(blocks, block)
func (b *BlogViewerApplication) Render(filePath string, isMobile bool) (gin.H, error) {
data := BlogFileData{}
_, err := b.fs.Read(filePath, &data)
if err != nil {
return nil, err
}
return gin.H{
"header": header,
"blocks": blocks,
"header": data.Header,
"blocks": data.Blocks,
}, nil
}

View File

@ -66,13 +66,13 @@ func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H
}
func (f *FinderApplication) RenderProps(filePath string) gin.H {
file, err := f.fs.NewRead(filePath)
if err != nil {
return nil
}
// file, err := f.fs.NewReadDeprecated(filePath)
// if err != nil {
// return nil
// }
return gin.H{
"file": file,
// "file": file,
}
}

View File

@ -1,10 +1,8 @@
package personalprops
import (
"errors"
"net/http"
websiteapp "personalwebsite/apps"
"personalwebsite/libs"
"personalwebsite/webfilesystem"
"github.com/gin-gonic/gin"
@ -28,12 +26,25 @@ func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) PersonalPropertiesApp {
}
func (p *PersonalPropertiesApp) Route(route *gin.RouterGroup) {
route.GET("writeMock", func(ctx *gin.Context) {
err := p.WriteMock()
if err != nil {
ctx.Status(http.StatusInternalServerError)
}
ctx.Status(http.StatusOK)
})
route.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
ginH, err := p.Render()
filePath := ctx.Query("path")
if filePath == "" {
ctx.Status(http.StatusBadRequest)
return
}
ginH, err := p.Render(filePath)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO
return
}
if isMobile {
ctx.HTML(http.StatusOK, "personal-properties/mobile-app.tmpl", ginH)
@ -50,170 +61,65 @@ func (p *PersonalPropertiesApp) GetId() string {
return p.manifest.AppId
}
// func (p *PersonalPropertiesApp) WriteToDb() {
// allProps := make([]PropIsland, 0)
func (p *PersonalPropertiesApp) WriteMock() error {
fileHeader := webfilesystem.FileHeader{
MongoId: primitive.NewObjectID(),
Name: "aboutme.props",
Type: "personal-properties",
Icon: "",
}
fileData := PropertiesFileData{
Header: HeaderIsland{
Name: "Test Name",
IconPath: "test icon path",
Info1: "Info1",
Info2: "Info2",
},
Props: []PropIsland{
{
Header: "Test Prop Header",
Props: []PropElement{
{
Key: "Test key",
KeyComments: []string{"Test key comment 1", "Test key comment 1"},
Values: []string{"test value1", "test value2"},
},
},
},
{
Header: "Test Prop Header 2",
Props: []PropElement{
{
Key: "Test key",
KeyComments: []string{"Test key comment 1", "Test key comment 1"},
Values: []string{"test value1", "test value2"},
},
},
},
},
}
_, _, err := p.fs.Write("/home/user/aboutme.props", &fileHeader, fileData)
return err
}
// // careerProps := make([]Prop, 0)
// expertiseIsland := PropIsland{
// Header: "Area Of Expertise",
// Props: []PropElement{{
// Key: "Programming",
// Values: []string{
// "Creating tools and plugins for artists",
// "Editor and basic gameplay scripting",
// },
// },
// {
// Key: "Game Art",
// Values: []string{
// "Professional modeling",
// "Complete knowledge in CG render theory",
// },
// },
// },
// }
// eduIsland := PropIsland{
// Header: "Education",
// Props: []PropElement{
// {
// Key: "Gymnasium 526",
// KeyComments: []string{"2005-2015"},
// Values: []string{"Extended natural sciences course", "Additional C++, media production and computer graphics courses", "Winner of conference “The future of a strong Russia is in high technology” in programming section"},
// },
// {
// Key: "Lyceum 281",
// KeyComments: []string{"2015-2016"},
// Values: []string{"Extended IT and Physical sciences course"},
// },
// {
// Key: "University",
// KeyComments: []string{"2017-2019"},
// Values: []string{"Faculty: Info-communication Networks and Systems", "Specialty: Information Security"},
// },
// },
// }
// careerProps := PropIsland{
// Header: "Career",
// Props: []PropElement{
// {
// Key: "VR lab assistant",
// KeyComments: []string{"Academy of Digital Technologies", "2019-2020"},
// Values: []string{"Teaching lessons for students in Unreal Engine 4, Unity and Blender editor courses", "Training students for World Skills Russia"},
// },
// {
// Key: "3d artist",
// KeyComments: []string{"Space Time VR", "2020-2020"},
// Values: []string{"Creating 3d assets for VR game"},
// },
// {
// Key: "Jr. Techartist",
// KeyComments: []string{"MP Games"},
// Values: []string{"Game content integration and production", "Shader coding", "Optimization asset production in artists pipeline"},
// },
// {
// Key: "Techartist",
// Values: []string{"Game content optimization and production", "Profiling and debugging render pipeline", "Creating in-house tools for pipeline software", "Working process pipeline integration and maintenance", "Linux servers administration"},
// },
// },
// }
// volunteerProps := PropIsland{
// Header: "Volunteer Experience",
// Props: []PropElement{
// {
// Key: "Metrostroi Mod",
// Values: []string{
// "Help unite fragmented community on one social platform",
// "Worked on social elements of official site, create ranking",
// "Took a part on creating ranking system for players",
// "Creating models and maps for Steam Workshop"},
// },
// {
// Key: "Age of Silence",
// Values: []string{
// "Start as tech-artist, create naming system in big ue4 project",
// "Promoted to chief of 3d Department",
// "Project win Unreal Day 2019",
// },
// },
// },
// }
// allProps = append(allProps, expertiseIsland, careerProps, eduIsland, volunteerProps)
// file := webfilesystem.WebFSFile{
// MongoId: primitive.NewObjectID(),
// Name: "personal.props",
// Type: "props",
// Data: PropertiesFile{
// Props: allProps,
// },
// }
// err := p.fs.CreateFile(&file, "/home/user/")
// if err != nil {
// println(err.Error())
// } else {
// println("Ok")
// }
// }
func (p *PersonalPropertiesApp) Render() (gin.H, error) {
props, err := p.fs.NewRead("/home/user/personal.props")
func (p *PersonalPropertiesApp) Render(filePath string) (gin.H, error) {
propsData := PropertiesFileData{}
_, err := p.fs.Read(filePath, &propsData)
if err != nil {
return nil, err
}
if props.Data == nil || props.Type != "props" {
return nil, errors.New("bad file")
}
headerProps := props.Data.(primitive.D).Map()["headerprops"].(primitive.D).Map()
// file, err := p.fs.Read(headerProps["icon"].(string))
if err != nil {
//TODO err catch
}
// data, err := wde.ReadImage(file)
// if err != nil {
// return nil, err
// }
hIsland := HeaderIsland{
Name: headerProps["name"].(string),
// Icon: data,
Info1: "LLL",
Info2: "QQQ",
}
allProps := make([]PropIsland, 0)
for _, v := range props.Data.(primitive.D).Map()["props"].(primitive.A) {
island := PropIsland{}
island.Header = v.(primitive.D).Map()["header"].(string)
for _, prop := range v.(primitive.D).Map()["props"].(primitive.A) {
elem := PropElement{
Key: prop.(primitive.D).Map()["key"].(string),
}
if prop.(primitive.D).Map()["keycomments"] != nil {
for _, keyComments := range prop.(primitive.D).Map()["keycomments"].(primitive.A) {
elem.KeyComments = append(elem.KeyComments, keyComments.(string))
}
}
for _, elemValues := range prop.(primitive.D).Map()["values"].(primitive.A) {
elem.Values = append(elem.Values, elemValues.(string))
}
island.Props = append(island.Props, elem)
}
allProps = append(allProps, island)
}
return gin.H{
"headerProps": hIsland,
"allprops": allProps,
"headerProps": propsData.Header,
"allprops": propsData.Props,
}, nil
}
type HeaderIsland struct {
Name string
Icon *libs.Base64Img
Info1 string
Info2 string
Name string `bson:"name"`
IconPath string `bson:"iconpath"`
Info1 string `bson:"info1"`
Info2 string `bson:"info2"`
}
type PropElement struct {
@ -227,6 +133,7 @@ type PropIsland struct {
Props []PropElement
}
type PropertiesFile struct {
Props []PropIsland `bson:"props"`
type PropertiesFileData struct {
Header HeaderIsland `bson:"header"`
Props []PropIsland `bson:"props"`
}

View File

@ -1,12 +1,10 @@
package libs
import (
"errors"
"net/http"
"personalwebsite/webfilesystem"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type ImagLib struct {
@ -19,40 +17,26 @@ func NewImgLib(webfs *webfilesystem.WebFileSystem) *ImagLib {
}
}
func ReadImage(img *webfilesystem.WebFSFile) (*Img, error) {
data, ok := img.Data.(primitive.D).Map()["srcdata"]
if !ok {
return nil, errors.New("error in file decoding")
}
bin := data.(primitive.Binary).Data
min32Data, ok := img.Data.(primitive.D).Map()["min32data"]
if !ok {
return nil, errors.New("error in file decoding")
}
min32Bin := min32Data.(primitive.Binary).Data
return &Img{
// Header: header,
Data: bin,
Miniature32: min32Bin,
}, nil
}
func (l *ImagLib) Route(route *gin.RouterGroup) {
route.GET("get", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
ctx.String(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
file, err := l.fs.NewRead(path)
imgData := Img{}
_, err := l.fs.Read(path, &imgData)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
ctx.Status(http.StatusInternalServerError)
return
}
data := file.Data.(primitive.Binary)
// _, err = l.fs.readFSDocs(fileId, &imgData)
// if err != nil {
// ctx.Status(http.StatusInternalServerError)
// }
ctx.Data(http.StatusOK, "image/jpeg", data.Data)
ctx.Data(http.StatusOK, "image/jpeg", imgData.Bin)
})
}
@ -76,7 +60,7 @@ type Base64Img struct {
}
type Img struct {
Header string `bson:"header"`
Data []byte `bson:"srcdata"`
Miniature32 []byte `bson:"min32data"`
Header string `bson:"header"`
Bin []byte `bson:"bin"`
// BinMin32 []byte `bson:"binmin32"`
}

View File

@ -15,17 +15,17 @@
.blog-viewer .header-h1{
font-size:x-large;
padding: 10px;
padding-bottom: 10px;
}
.blog-viewer .header-h2{
font-size:large;
padding: 6px;
padding-bottom: 6px;
}
.blog-viewer .header-h3{
font-size:larger;
padding: 2px;
padding-bottom: 2px;
}
.blog-viewer .plain-text{

View File

@ -36,9 +36,9 @@ class Finder{
)
this.OpenDir(this.path)
// newWindow.querySelector("#BackButton").addEventListener('click', () =>{
// this.OpenPreviousDir()
// })
newWindow.querySelector("#RootButton").addEventListener('click', () =>{
this.OpenDir('/')
})
// newWindow.querySelector("#HomeButton").addEventListener('click', () =>{
// this.OpenDir(this.homePath)
@ -68,16 +68,16 @@ class Finder{
FileUploading(filesList){
let formData = new FormData()
// console.log(filesList)
console.log(this.path)
for (let i = 0; i < filesList.length; i++) {
const element = filesList[i];
formData.append("file", element.getAsFile())
console.log(formData)
// console.log(formData)
}
// console.log(formData)
// formData.append("photo", photo);
fetch('/fs/upload/?' + new URLSearchParams({
path: '/home/user/',
parentPath: this.path,
}),
{
method: "POST",
@ -125,15 +125,16 @@ class Finder{
* @param {string} path
*/
OpenDir(path){
this.fileView.OpenFolder(this.path)
this.path = path
this.fileView.OpenFolder(path)
}
/**
*/
OpenNewDir(){
WebDesktopEnvironment.Open("finder", [this.path])
}
// /**
// */
// OpenNewDir(){
// WebDesktopEnvironment.Open("finder", [this.path])
// }
/**
* @param {MouseEvent} event

View File

@ -13,6 +13,7 @@ class PersonalProperties{
NewWindow(path){
fetch(`${window.location.origin}/application/personal-properties/render?`+ new URLSearchParams({
isMobile: WebDesktopEnvironment.isMobile,
path: path
}))
.then((response) => response.text())
.then((html) => {

View File

@ -4,9 +4,9 @@ document.addEventListener('DOMContentLoaded', function() {
// console.log(window.screen.width)
wde = new WebDesktopEnvironment
if (!WebDesktopEnvironment.isMobile){
WebDesktopEnvironment.Open("finder", ["/home/user"])
// WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"])
// WebDesktopEnvironment.Open("personal-properties", ["kek"])
// WebDesktopEnvironment.Open("finder", ["/home/user"])
// WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog1.blog"])
WebDesktopEnvironment.Open("personal-properties", ["/home/user/aboutme.props"])
} else {
WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"])
}

View File

@ -12,7 +12,7 @@
<div id="ContentBorder" class="ContentBorder AdjectiveElement">
<div class="FinderContent">
<div class="ToolBar ConvexElement">
<button id="BackButton">Back</button>
<button id="RootButton">/</button>
<button id="HomeButton">Home</button>
</div>
<div class="FinderFileView">

View File

@ -11,8 +11,9 @@
<div class="ScrollContent">
<div class="PropertiesList">
<div class="Personal-properties-bio">
<img src="data:{{ .headerProps.Icon.Header }},{{ .headerProps.Icon.Base64 }}" alt="My Photo" style="width: 48px;height: 48px;">
<img src="/system/libs/img/get?path={{ .headerProps.IconPath }}" alt="My Photo" style="width: 48px;height: 48px;">
<div class="Personal-properties-textbio">
{{ .headerProps.IconPath }}
<div>{{ .headerProps.Name }}</div>
<div>{{ .headerProps.Info1 }}</div>
<div>{{ .headerProps.Info2 }}</div>

BIN
test-img/folder.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
test-img/ohno.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -22,13 +22,13 @@ type FilesWidget struct {
}
func (w *WDE) Render(path string) (gin.H, error) {
list, err := w.fs.NewList(path)
if err != nil {
return nil, err
}
// list, err := w.fs.NewListDeprecated(path)
// if err != nil {
// return nil, err
// }
return gin.H{
"Name": "Greg Brzezinski",
"Files": list,
"Name": "Greg Brzezinski",
// "Files": list,
}, nil
}
@ -44,10 +44,11 @@ func (w *WDE) RenderContextMenu() (gin.H, error) {
}
func (w *WDE) RenderFileTileView(directory string, host string) (gin.H, error) {
list, err := w.fs.NewList(directory)
list, err := w.fs.ListDir(directory)
if err != nil {
return nil, err
}
for _, file := range list {
switch file.Type {
case "directory":

View File

@ -1,84 +1,136 @@
package webfilesystem
import (
"context"
"strconv"
"errors"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type DirectoryData struct {
Parent primitive.ObjectID `bson:"parent"`
Children []primitive.ObjectID `bson:"children"`
MongoId primitive.ObjectID `bson:"_id"`
Parent primitive.ObjectID `bson:"parent_id"`
Children []primitive.ObjectID `bson:"children_id"`
}
func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) {
func (fs *WebFileSystem) CreateDirectory(dirPath string) (primitive.ObjectID, primitive.ObjectID, error) {
splittedPath := fs.SplitPath(dirPath)
parentPath := fs.GetParentPath(dirPath)
parentDirRaw, err := fs.NewRead(parentPath)
parentDirId, err := fs.FindFile(parentPath)
if err != nil {
return primitive.NilObjectID, err
return primitive.NilObjectID, primitive.NilObjectID, err
}
newDir := WebFSFile{
parentDirHeader, err := fs.readFSDocs(parentDirId, nil)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
newDir := FileHeader{
MongoId: primitive.NewObjectID(),
Name: splittedPath[len(splittedPath)-1],
Type: "directory",
Data: DirectoryData{
Parent: parentDirRaw.MongoId,
Children: []primitive.ObjectID{},
},
Icon: "",
Icon: "",
}
objectId, err := fs.writeMongo(newDir, parentPath)
newDirData := DirectoryData{
MongoId: primitive.NewObjectID(),
Parent: parentDirHeader.MongoId,
Children: []primitive.ObjectID{},
}
headerId, dataId, err := fs.writeFileToMongo(&newDir, &newDirData)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
err = fs.AppendChildToDirectory(headerId, parentDirId)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
return headerId, dataId, nil
}
func (fs *WebFileSystem) AppendChildToDirectory(childId primitive.ObjectID, parentId primitive.ObjectID) error {
parentDirData := DirectoryData{}
parentDir, err := fs.readFSDocs(parentId, &parentDirData)
if err != nil {
return err
}
//TODO Check for duplicates?
parentDirData.Children = append(parentDirData.Children, childId)
err = fs.UpdateFileData(parentDir, parentDirData)
if err != nil {
return err
}
return nil
}
func (fs *WebFileSystem) ListDir(dirPath string) ([]*FileHeader, error) {
dirId, err := fs.FindFile(dirPath)
if err != nil {
return nil, err
}
dirData := DirectoryData{}
_, err = fs.readFSDocs(dirId, &dirData)
if err != nil {
return nil, err
}
// println(dirId.String())
// println(dirData.MongoId.String())
children := []*FileHeader{}
for _, childID := range dirData.Children {
childFile, err := fs.readFSDocs(childID, nil)
if err != nil {
// println(err.Error())
continue
}
children = append(children, childFile)
}
return children, nil
}
func (fs *WebFileSystem) FindFile(filePath string) (primitive.ObjectID, error) {
splittedPath := fs.SplitPath(filePath)
rootDir, _, err := fs.GetRootDir()
if err != nil {
return primitive.NilObjectID, err
}
// println(rootDir.Name)
lastFileID := rootDir.MongoId
for _, pathPart := range splittedPath[1:] {
// println(pathPart)
id, err := fs.findInChildren(pathPart, lastFileID)
if err != nil {
return primitive.NilObjectID, err
}
lastFileID = id
}
return lastFileID, nil
}
func (fs *WebFileSystem) findInChildren(fileName string, parentDirId primitive.ObjectID) (primitive.ObjectID, error) {
parentDirData := DirectoryData{}
_, err := fs.readFSDocs(parentDirId, &parentDirData)
if err != nil {
return primitive.NilObjectID, err
}
return objectId, nil
}
func (fs *WebFileSystem) validateDir(dir *WebFSFile) error {
kek := dir.Data.(primitive.D).Map()["children"].(primitive.A)
_ = kek
children := []primitive.ObjectID{}
counter := 0
for _, v := range kek {
_, err := fs.ReadByObjectID(v.(primitive.ObjectID))
for _, childId := range parentDirData.Children {
childFileHeader, err := fs.readFSDocs(childId, nil)
if err != nil {
counter++
} else {
children = append(children, v.(primitive.ObjectID))
return primitive.NilObjectID, err
}
}
if counter > 0 {
println(dir.Name + " broken iDs: " + strconv.Itoa(counter))
_, err := fs.webfsCollection.UpdateByID(context.Background(), dir.MongoId, bson.M{"$set": bson.M{"data.children": children}})
if err != nil {
println(err.Error())
return err
if childFileHeader.Name == fileName {
return childId, nil
}
}
return nil
return primitive.NilObjectID, errors.New("file not found")
}
func castToFile(raw *interface{}) *WebFSFile {
var dirPtr interface{} = *raw
return dirPtr.(*WebFSFile)
}
func castToDirectoryData(data interface{}) (*DirectoryData, error) {
dirData := DirectoryData{}
err := mapstructure.Decode(data.(primitive.D).Map(), &dirData)
if err != nil {
return nil, err
}
return &dirData, nil
}
// func List()

View File

@ -5,11 +5,13 @@ package webfilesystem
import (
"errors"
"os"
"path"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error {
func (fs *WebFileSystem) UploadFile(realFilepath string, parentPath string) error {
//TODO check is file exists
file, err := os.ReadFile(realFilepath)
if err != nil {
return err
@ -18,57 +20,36 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error {
fileName := splittedPath[len(splittedPath)-1]
extension := fs.GetExtension(fileName)
switch extension {
case "jpg":
fallthrough
case "jpeg":
newFile := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "jpeg",
Data: file,
Icon: "",
}
err := fs.WriteFile(&newFile, path)
return err
case "png":
newFile := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "png",
Data: file,
}
err := fs.WriteFile(&newFile, path)
return err
default:
return errors.New("this filetype not allowed")
newFile2 := FileHeader{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "",
Icon: "",
Data: primitive.NilObjectID,
}
}
func (fs *WebFileSystem) UploadBinaryFile(file []byte, fileName string, path string) error {
extension := fs.GetExtension(fileName)
newFileData := BinaryFileData{
MongoId: primitive.NewObjectID(),
Bin: file,
}
filePath := path.Join(parentPath, fileName)
switch extension {
case "jpg":
fallthrough
case "jpeg":
newFile := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "jpeg",
Data: file,
Icon: "",
}
err := fs.WriteFile(&newFile, path)
newFile2.Type = "jpeg"
_, _, err := fs.Write(filePath, &newFile2, &newFileData)
return err
case "png":
newFile := WebFSFile{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "png",
Data: file,
newFile2.Type = "png"
_, _, err := fs.Write(filePath, &newFile2, &newFileData)
if err != nil {
println(err.Error())
return err
}
err := fs.WriteFile(&newFile, path)
return err
return nil
default:
return errors.New("this filetype not allowed")
}

View File

@ -1,94 +1,77 @@
package webfilesystem
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type readStruct struct {
File interface{}
Filter interface{}
}
func (fs *WebFileSystem) readMongo(read readStruct) (*interface{}, error) {
err := fs.webfsCollection.FindOne(fs.ctx, read.Filter).Decode(read.File)
func (fs *WebFileSystem) readFSDocs(fileID primitive.ObjectID, fileData interface{}) (*FileHeader, error) {
fileHeader := &FileHeader{}
filter := primitive.M{
"_id": fileID,
}
err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(fileHeader)
if err != nil {
return nil, err
}
return &read.File, nil
if fileData != nil {
filter := primitive.M{
"_id": fileHeader.Data,
}
err := fs.webfsFilesData.FindOne(fs.ctx, filter).Decode(fileData)
if err != nil {
return nil, err
}
}
return fileHeader, nil
}
func (fs *WebFileSystem) writeMongo(file WebFSFile, parentPath string) (primitive.ObjectID, error) {
//TODO Check file existance
parentDir, err := fs.NewRead(parentPath)
func (fs *WebFileSystem) writeFileToMongo(file *FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) {
resData, err := fs.webfsFilesData.InsertOne(fs.ctx, data)
if err != nil {
return primitive.NilObjectID, err
return primitive.NilObjectID, primitive.NilObjectID, err
}
res, err := fs.webfsCollection.InsertOne(context.Background(), &file)
file.Data = resData.InsertedID.(primitive.ObjectID)
resHeader, err := fs.webfsFilesTable.InsertOne(fs.ctx, file)
if err != nil {
return primitive.NilObjectID, err
return primitive.NilObjectID, primitive.NilObjectID, err
}
fileId := fs.castInsertId(res)
fs.insertFileToDirectory(fileId, parentDir.MongoId)
return fileId, nil
return resHeader.InsertedID.(primitive.ObjectID), resData.InsertedID.(primitive.ObjectID), err
}
func (fs *WebFileSystem) removeMongo(filePath string) error {
file, err := fs.NewRead(filePath)
if err != nil {
return err
}
if file.MongoId == primitive.NilObjectID {
return errors.New("TODO") //TODO
}
filter := primitive.M{
"_id": file.MongoId,
}
res, err := fs.webfsCollection.DeleteOne(fs.ctx, filter)
if err != nil {
return err
}
if res.DeletedCount < 1 {
return errors.New("no file removed")
}
return nil
func (fs *WebFileSystem) removeFromMongo(fileId primitive.ObjectID) error {
return errors.New("todo not implemented yet")
}
func (fs *WebFileSystem) Validate() error {
filter := primitive.D{
{
Key: "type",
Value: "directory",
},
}
cur, err := fs.webfsCollection.Find(context.Background(), filter)
if err != nil {
return err
}
defer cur.Close(context.Background())
// filter := primitive.D{
// {
// Key: "type",
// Value: "directory",
// },
// }
// cur, err := fs.webfsCollection.Find(context.Background(), filter)
// if err != nil {
// return err
// }
// defer cur.Close(context.Background())
directories := []*WebFSFile{}
// directories := []*WebFSFile{}
for cur.Next(context.Background()) {
dir := &WebFSFile{}
// for cur.Next(context.Background()) {
// dir := &WebFSFile{}
err = cur.Decode(dir)
if err != nil {
println(err.Error())
return err
}
directories = append(directories, dir)
}
// err = cur.Decode(dir)
// if err != nil {
// println(err.Error())
// return err
// }
// directories = append(directories, dir)
// }
for _, d := range directories {
fs.validateDir(d)
}
// for _, d := range directories {
// fs.validateDir(d)
// }
return nil
}

View File

@ -1,56 +0,0 @@
package webfilesystem
import (
"errors"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (fs *WebFileSystem) NewRead(filePath string) (*WebFSFile, error) {
splittedPath := fs.SplitPath(filePath)
read := readStruct{
File: &WebFSFile{},
Filter: primitive.M{
"name": splittedPath[len(splittedPath)-1],
},
}
fileRaw, err := fs.readMongo(read)
if err != nil {
return nil, err
}
file := castToFile(fileRaw)
return file, nil
}
func (fs *WebFileSystem) NewList(dirPath string) ([]*WebFSFile, error) {
dirFile, err := fs.NewRead(dirPath)
if err != nil {
return nil, err
}
if dirFile.Type != "directory" {
return nil, errors.New("file is not a directory")
}
fileData, err := castToDirectoryData(dirFile.Data)
if err != nil {
return nil, err
}
files := []*WebFSFile{}
for _, child := range fileData.Children {
file, err := fs.ReadByObjectID(child)
if err != nil {
println(err.Error())
continue
}
files = append(files, file)
}
return files, nil
}
func (fs *WebFileSystem) WriteFile(file *WebFSFile, parentPath string) error {
fs.writeMongo(*file, parentPath)
return errors.New("Not ready yet")
}

15
webfilesystem/public.go Normal file
View File

@ -0,0 +1,15 @@
package webfilesystem
func (fs *WebFileSystem) Read(filePath string, fileData interface{}) (*FileHeader, error) {
fileId, err := fs.FindFile(filePath)
if err != nil {
return nil, err
}
fileHeader, err := fs.readFSDocs(fileId, fileData)
if err != nil {
return nil, err
}
return fileHeader, nil
}

View File

@ -13,8 +13,8 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
// ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
// return
// }
path := ctx.Query("path")
if path == "" {
parentPath := ctx.Query("parentPath")
if parentPath == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
@ -32,7 +32,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
ctx.SaveUploadedFile(file, dst)
//TODO: Not Save to disk
err := fs.UploadFile(dst, path)
err := fs.UploadFile(dst, parentPath)
if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO
return
@ -65,14 +65,14 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
// ctx.JSON(http.StatusOK, "OK")
// })
// route.GET("init", func(ctx *gin.Context) {
// err := fs.InitFS()
// if err != nil {
// ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
// return
// }
// ctx.JSON(http.StatusOK, "OK")
// })
route.GET("init", func(ctx *gin.Context) {
err := fs.InitFS()
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
return
}
ctx.JSON(http.StatusOK, "Inited")
})
route.GET("createDir", func(ctx *gin.Context) {
path := ctx.Query("path")
@ -81,7 +81,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
return
}
_, err := fs.NewCreateDirectory(path)
_, _, err := fs.CreateDirectory(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
return
@ -97,30 +97,30 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
return
}
files, err := fs.NewList(path)
files, err := fs.ListDir(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
ctx.String(http.StatusInternalServerError, err.Error()) //TODO json error struct
return
}
ctx.JSON(http.StatusOK, &files)
})
route.GET("read", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
// route.GET("read", func(ctx *gin.Context) {
// path := ctx.Query("path")
// if path == "" {
// ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
// return
// }
file, err := fs.NewRead(path)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
// file, err := fs.NewReadDeprecated(path)
// if err != nil {
// ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
// return
// }
ctx.JSON(http.StatusOK, &file)
})
// ctx.JSON(http.StatusOK, &file)
// })
route.GET("validate", func(ctx *gin.Context) {
err := fs.Validate()
@ -138,7 +138,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
ctx.Status(http.StatusBadRequest) //TODO
return
}
err := fs.Delete(path)
err := fs.Remove(path)
if err != nil {
ctx.Status(http.StatusInternalServerError)
return

View File

@ -2,9 +2,9 @@ package webfilesystem
import (
"context"
"errors"
"strings"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
@ -12,57 +12,155 @@ import (
type WebFileSystem struct {
webfsCollection *mongo.Collection
webfsFilesTable *mongo.Collection
webfsFilesData *mongo.Collection
ctx context.Context
}
func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem {
return &WebFileSystem{
webfsCollection: mongoClient.Database(dBName).Collection(fsCollectionName), // TODO Check collection is exist
webfsFilesTable: mongoClient.Database(dBName).Collection("webfs-table"), // TODO Check collection is exist, //FIXME
webfsFilesData: mongoClient.Database(dBName).Collection("webfs-data"), // TODO Check collection is exist, //FIXME
ctx: context.Background(),
}
}
func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) {
filter := primitive.D{
{
Key: "_id",
Value: objectId,
},
}
//TODO to readMongo()
file, err := fs.findFileInMongo(filter)
return file, err
type FileHeader struct {
MongoId primitive.ObjectID `bson:"_id" json:"-"`
Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"`
Icon string `bson:"-" json:"icon"`
Data primitive.ObjectID `bson:"data_id" json:"-"`
}
type BinaryFileData struct {
MongoId primitive.ObjectID `bson:"_id" json:"-"`
Bin []byte `bson:"bin" json:"-"`
}
// Deprecated
func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error) {
res := fs.webfsCollection.FindOne(context.Background(), &filter)
file := WebFSFile{}
err := res.Decode(&file)
if err != nil {
return nil, err
func (fs *WebFileSystem) ReadHeader(fileID primitive.ObjectID) (*FileHeader, error) {
file := &FileHeader{}
filter := primitive.M{
"_id": fileID,
}
return &file, nil
err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(file)
return file, err
}
func (fs *WebFileSystem) castInsertId(res *mongo.InsertOneResult) primitive.ObjectID {
return res.InsertedID.(primitive.ObjectID)
//TODO To private, get name from path and set it to file struct, force set newObjectID
func (fs *WebFileSystem) Write(filePath string, file *FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) {
headerId, dataId, err := fs.writeFileToMongo(file, data)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
parentPath := fs.GetParentPath(filePath)
parentDir, err := fs.FindFile(parentPath)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
if parentDir.IsZero() {
return primitive.NilObjectID, primitive.NilObjectID, errors.New("parent dir not found")
}
err = fs.AppendChildToDirectory(headerId, parentDir)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
return headerId, dataId, nil
}
func (fs *WebFileSystem) insertFileToDirectory(fileId primitive.ObjectID, directoryId primitive.ObjectID) error {
dir, err := fs.ReadByObjectID(directoryId)
func (fs *WebFileSystem) UpdateFileData(file *FileHeader, data interface{}) error {
update := bson.M{"$set": data}
// println("updating data file: " + file.MongoId.String())
res, err := fs.webfsFilesData.UpdateByID(fs.ctx, file.Data, update)
if err != nil {
return err
}
//TODO check if file exist
fileData := DirectoryData{}
err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData)
if res.ModifiedCount < 1 {
return errors.New("no data updated")
}
return err
}
func (fs *WebFileSystem) InitFS() error { //FIXME Can't set parent_id to itself
rootData := DirectoryData{
MongoId: primitive.NewObjectID(),
Parent: primitive.NewObjectID(),
Children: []primitive.ObjectID{},
}
rootHeader := FileHeader{
MongoId: primitive.NewObjectID(),
Name: "/",
Type: "directory",
Icon: "",
}
_, _, err := fs.writeFileToMongo(&rootHeader, &rootData)
if err != nil {
return err
}
fileData.Children = append(fileData.Children, fileId)
return nil
}
fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data": fileData}})
//TODO get on boot and safe to struct
func (fs *WebFileSystem) GetRootDir() (*FileHeader, *DirectoryData, error) {
filter := primitive.M{
"name": "/",
}
res := fs.webfsFilesTable.FindOne(fs.ctx, filter)
if res == nil {
return nil, nil, errors.New("TODO") //TODO
}
rootDir := FileHeader{}
err := res.Decode(&rootDir)
if res == nil {
return nil, nil, err
}
filterData := primitive.M{
"_id": rootDir.Data,
}
resData := fs.webfsFilesData.FindOne(fs.ctx, filterData)
if resData.Err() != nil {
return nil, nil, err
}
rootDirData := DirectoryData{}
err = resData.Decode(&rootDirData)
if err != nil {
return nil, nil, err
}
return &rootDir, &rootDirData, nil
}
func (fs *WebFileSystem) Remove(filePath string) error {
parentPath := fs.GetParentPath(filePath)
parentDirId, err := fs.FindFile(parentPath)
if err != nil {
return err
}
//TODO: Check, if parent file is dir
parentDirData := DirectoryData{}
parentDir, err := fs.readFSDocs(parentDirId, &parentDirData)
if err != nil {
return err
}
fileId, err := fs.FindFile(filePath)
if err != nil {
return err
}
newChildren := []primitive.ObjectID{}
for _, childId := range parentDirData.Children {
if childId != fileId {
newChildren = append(newChildren, childId)
}
}
parentDirData.Children = newChildren
err = fs.UpdateFileData(parentDir, parentDirData)
if err != nil {
return err
}
return nil
}
@ -86,96 +184,8 @@ func (fs *WebFileSystem) GetExtension(filename string) string {
func (fs *WebFileSystem) GetParentPath(path string) string {
splittedPath := fs.SplitPath(path)
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")
return parentPath
}
func (fs *WebFileSystem) Delete(filePath string) error {
// splittedPath := fs.SplitPath(filePath)
// parentPath := fs.GetParentPath(filePath)
_, err := fs.NewRead(filePath)
if err != nil {
return err
if len(splittedPath) > 1 {
return "/" + strings.Join(splittedPath[1:len(splittedPath)-1], "/")
}
err = fs.removeMongo(filePath)
if err != nil {
return err
}
err = fs.Validate() //FIXME
if err != nil {
return err
}
//Delete from parent folder
// parentDir, err := fs.NewRead(parentPath)
// if err != nil {
// return err
// }
// parentDirData, err := castToDirectoryData(parentDir.Data)
// if err != nil {
// return err
// }
// newChildrenSlice := []primitive.ObjectID{}
// for i := 0; i < len(parentDirData.Children); i++ {
// if parentDirData.Children[i] != file.MongoId {
// newChildrenSlice = append(newChildrenSlice, parentDirData.Children[i])
// }
// }
// parentDirData.Children = newChildrenSlice
// parentDir.Data = parentDirData
// parentParentDir := fs.GetParentPath(parentPath)
// _, err = fs.writeMongo(*parentDir, parentParentDir)
// if err != nil {
// return err
// }
return nil
// for _, childId := range parentDirData.Children {
// // read := readStruct{
// // File: filePath,
// // Filter: primitive.M{
// // "_id": childId,
// // },
// // }
// // childFile, err := fs.ReadByObjectID(childId)
// // if err != nil {
// // println(err.Error())
// // continue
// // }
// // println(childFile.Name + " " + chil)
// if file.MongoId == childId {
// println(file.Name)
// parentDirData.Children.
// }
// }
// 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
}
type WebFSFile struct {
MongoId primitive.ObjectID `bson:"_id" json:"-"`
Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"`
Icon string `bson:"-" json:"icon"`
Data interface{} `bson:"data" json:"-"`
return "/"
}