Compare commits

...

5 Commits

Author SHA1 Message Date
b8eda48aa7 pers props app update 2023-05-07 04:24:46 +03:00
9b88db9289 temp default icon 2023-05-07 04:24:33 +03:00
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
22 changed files with 579 additions and 675 deletions

2
.env
View File

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

View File

@ -1,10 +1,8 @@
package personalprops package personalprops
import ( import (
"errors"
"net/http" "net/http"
websiteapp "personalwebsite/apps" websiteapp "personalwebsite/apps"
"personalwebsite/libs"
"personalwebsite/webfilesystem" "personalwebsite/webfilesystem"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -28,12 +26,25 @@ func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) PersonalPropertiesApp {
} }
func (p *PersonalPropertiesApp) Route(route *gin.RouterGroup) { 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) { route.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile") isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true" 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 { if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO ctx.JSON(http.StatusInternalServerError, "TODO") //TODO
return
} }
if isMobile { if isMobile {
ctx.HTML(http.StatusOK, "personal-properties/mobile-app.tmpl", ginH) ctx.HTML(http.StatusOK, "personal-properties/mobile-app.tmpl", ginH)
@ -50,170 +61,65 @@ func (p *PersonalPropertiesApp) GetId() string {
return p.manifest.AppId return p.manifest.AppId
} }
// func (p *PersonalPropertiesApp) WriteToDb() { func (p *PersonalPropertiesApp) WriteMock() error {
// allProps := make([]PropIsland, 0) 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) func (p *PersonalPropertiesApp) Render(filePath string) (gin.H, error) {
// expertiseIsland := PropIsland{ propsData := PropertiesFileData{}
// Header: "Area Of Expertise", _, err := p.fs.Read(filePath, &propsData)
// 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")
if err != nil { if err != nil {
return nil, err 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{ return gin.H{
"headerProps": hIsland, "headerProps": propsData.Header,
"allprops": allProps, "allprops": propsData.Props,
}, nil }, nil
} }
type HeaderIsland struct { type HeaderIsland struct {
Name string Name string `bson:"name"`
Icon *libs.Base64Img IconPath string `bson:"iconpath"`
Info1 string Info1 string `bson:"info1"`
Info2 string Info2 string `bson:"info2"`
} }
type PropElement struct { type PropElement struct {
@ -227,6 +133,7 @@ type PropIsland struct {
Props []PropElement Props []PropElement
} }
type PropertiesFile struct { type PropertiesFileData struct {
Header HeaderIsland `bson:"header"`
Props []PropIsland `bson:"props"` Props []PropIsland `bson:"props"`
} }

View File

@ -1,12 +1,10 @@
package libs package libs
import ( import (
"errors"
"net/http" "net/http"
"personalwebsite/webfilesystem" "personalwebsite/webfilesystem"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
) )
type ImagLib struct { 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) { func (l *ImagLib) Route(route *gin.RouterGroup) {
route.GET("get", func(ctx *gin.Context) { route.GET("get", func(ctx *gin.Context) {
path := ctx.Query("path") path := ctx.Query("path")
if path == "" { if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct ctx.String(http.StatusBadRequest, "TODO") //TODO json error struct
return return
} }
file, err := l.fs.NewRead(path) imgData := Img{}
_, err := l.fs.Read(path, &imgData)
if err != nil { 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)
}) })
} }
@ -77,6 +61,6 @@ type Base64Img struct {
type Img struct { type Img struct {
Header string `bson:"header"` Header string `bson:"header"`
Data []byte `bson:"srcdata"` Bin []byte `bson:"bin"`
Miniature32 []byte `bson:"min32data"` // BinMin32 []byte `bson:"binmin32"`
} }

View File

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

View File

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

View File

@ -22,7 +22,23 @@
width: 0; width: 0;
height: 0; height: 0;
} */ } */
.PersPropsContent{
width: 100%;
height: 100%;
/* Auto layout */
display: flex;
flex-direction: row;
justify-content: center;
align-items: flex-start;
padding: 0px;
}
.PersPropsContent .PropsView{
/* background-color: rebeccapurple; */
width: 100%;
height: auto;
}
.PropertiesList{ .PropertiesList{
/* width: 100%; /* width: 100%;
height: auto; */ height: auto; */
@ -41,7 +57,7 @@
} }
.Personal-properties-bio{ .PropertiesList .ShortBio{
/* width: 100%; /* width: 100%;
height: auto; */ height: auto; */
/* margin-right: -20px; */ /* margin-right: -20px; */
@ -61,9 +77,7 @@
gap:15px; gap:15px;
} }
.ShortBio .Text{
.Personal-properties-textbio{
/* width: 100%; /* width: 100%;
height: auto; */ height: auto; */
@ -81,8 +95,14 @@
gap:1px; gap:1px;
} }
.ShortBio .Name{
font-family: "Virtue";
/* FIXME */
letter-spacing: 0.35px;
}
.Personal-properties-prop{
.PropertiesList .Island{
width: 100%; width: 100%;
height: auto; height: auto;
border: 1px solid #888888; border: 1px solid #888888;
@ -97,11 +117,9 @@
gap:1px; */ gap:1px; */
} }
.Island .Title {
.Personal-properties-prop-title{
font-family: "Virtue"; font-family: "Virtue";
/* FIXME */
letter-spacing: 0.35px; letter-spacing: 0.35px;
position:relative; position:relative;
display: inline-block; display: inline-block;
@ -111,7 +129,11 @@
top: -9px; top: -9px;
} }
.Personal-properties-prop-content{ .Focused .Island .Title{
background-color: #CCCCCC;
}
.Island .Content{
width: 100%; width: 100%;
/* top: 0px; */ /* top: 0px; */
/* Auto layout */ /* Auto layout */
@ -122,7 +144,7 @@
gap: 12px; gap: 12px;
} }
.Personal-properties-prop-row{ .Island .Row{
margin-left: 12px; margin-left: 12px;
margin-right: 12px; margin-right: 12px;
/* Auto layout */ /* Auto layout */
@ -132,7 +154,7 @@
padding: 0px; padding: 0px;
gap: 5px; gap: 5px;
} }
.Personal-properties-prop-key{ .Island .Key{
position: relative; position: relative;
font-family: "Virtue"; font-family: "Virtue";
font-size: 11px; font-size: 11px;
@ -145,7 +167,7 @@
/* font-weight: bold; */ /* font-weight: bold; */
} }
.Personal-properties-prop-key-comments{ .Island .KeyComment{
/* color: rgb(129, 129, 129); TODO*/ /* color: rgb(129, 129, 129); TODO*/
color: #646464; color: #646464;
font-size: 9px; font-size: 9px;
@ -154,7 +176,8 @@
white-space:normal; white-space:normal;
/* filter: drop-shadow(-.5px -.5px 0px #616161); */ /* filter: drop-shadow(-.5px -.5px 0px #616161); */
} }
.Personal-properties-prop-values{
.Island .Values{
width: 55%; width: 55%;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@ -164,7 +187,7 @@
} }
.Personal-properties-prop-value{ .Values .Value{
/* width: 55%; */ /* width: 55%; */
} }

View File

@ -13,6 +13,7 @@ class PersonalProperties{
NewWindow(path){ NewWindow(path){
fetch(`${window.location.origin}/application/personal-properties/render?`+ new URLSearchParams({ fetch(`${window.location.origin}/application/personal-properties/render?`+ new URLSearchParams({
isMobile: WebDesktopEnvironment.isMobile, isMobile: WebDesktopEnvironment.isMobile,
path: path
})) }))
.then((response) => response.text()) .then((response) => response.text())
.then((html) => { .then((html) => {
@ -22,7 +23,7 @@ class PersonalProperties{
newWindow.innerHTML = html newWindow.innerHTML = html
let scrollBar = new WdeScrollBar(newWindow.querySelector('.ScrollBarScrollElement'), newWindow.querySelector('.ScrollContent')) let scrollBar = new WdeScrollBar(newWindow.querySelector('.ScrollBarScrollElement'), newWindow.querySelector('.PropsView'))
newWindow.querySelector("#closeWindowButton").addEventListener('click', () => { newWindow.querySelector("#closeWindowButton").addEventListener('click', () => {
console.log("qewqweqweqweqwe") console.log("qewqweqweqweqwe")

View File

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

View File

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

View File

@ -7,36 +7,37 @@
</div> </div>
<div id="Drag" class="VisualDragArea"></div> <div id="Drag" class="VisualDragArea"></div>
</div> </div>
<div class="ContentBorder"> <div class="ContentBorder AdjectiveElement">
<div class="ScrollContent"> <div class="PersPropsContent">
<div class="PropsView">
<div class="PropertiesList"> <div class="PropertiesList">
<div class="Personal-properties-bio"> <div class="ShortBio">
<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"> <div class="Text">
<div>{{ .headerProps.Name }}</div> <div class="Name">{{ .headerProps.Name }}</div>
<div>{{ .headerProps.Info1 }}</div> <div>{{ .headerProps.Info1 }}</div>
<div>{{ .headerProps.Info2 }}</div> <div>{{ .headerProps.Info2 }}</div>
</div> </div>
</div> </div>
{{ range $propIsland := .allprops }} {{ range $propIsland := .allprops }}
<div id="prop" class="Personal-properties-prop"> <div class="Island">
<div class="Personal-properties-prop-title"> <div class="Title">
{{$propIsland.Header}}: {{$propIsland.Header}}:
</div> </div>
<div class="Personal-properties-prop-content"> <div class="Content">
{{range $prop := $propIsland.Props}} {{range $prop := $propIsland.Props}}
<div class="Personal-properties-prop-row"> <div class="Row">
<div class="Personal-properties-prop-key"> <div class="Key">
{{$prop.Key}}: {{$prop.Key}}:
{{ range $value := $prop.KeyComments }} {{ range $value := $prop.KeyComments }}
<div class="Personal-properties-prop-key-comments"> <div class="KeyComment">
{{ $value }} {{ $value }}
</div> </div>
{{ end }} {{ end }}
</div> </div>
<div class="Personal-properties-prop-values"> <div class="Values">
{{ range $value := $prop.Values }} {{ range $value := $prop.Values }}
<div class="Personal-properties-prop-value"> <div class="Value">
{{ $value }} {{ $value }}
</div> </div>
{{ end }} {{ end }}
@ -50,6 +51,7 @@
</div> </div>
{{template "wde-widgets/scrollbar.tmpl" .}} {{template "wde-widgets/scrollbar.tmpl" .}}
</div> </div>
</div>
{{ end }} {{ end }}

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) { func (w *WDE) Render(path string) (gin.H, error) {
list, err := w.fs.NewList(path) // list, err := w.fs.NewListDeprecated(path)
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
return gin.H{ return gin.H{
"Name": "Greg Brzezinski", "Name": "Greg Brzezinski",
"Files": list, // "Files": list,
}, nil }, nil
} }
@ -44,10 +44,11 @@ func (w *WDE) RenderContextMenu() (gin.H, error) {
} }
func (w *WDE) RenderFileTileView(directory string, host string) (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 { if err != nil {
return nil, err return nil, err
} }
for _, file := range list { for _, file := range list {
switch file.Type { switch file.Type {
case "directory": case "directory":
@ -59,7 +60,7 @@ func (w *WDE) RenderFileTileView(directory string, host string) (gin.H, error) {
case "jpg": case "jpg":
file.Icon = host + "/system/libs/img/get?path=" + path.Join(directory, file.Name) //FIXME file.Icon = host + "/system/libs/img/get?path=" + path.Join(directory, file.Name) //FIXME
default: default:
file.Icon = host + "/system/libs/img/get?path=/wde/icons/macos9/about-me.png" //FIXME file.Icon = host + "/system/libs/img/get?path=/wde/icons/macos9/folder.png" //FIXME
} }
} }
return gin.H{ return gin.H{

View File

@ -1,84 +1,136 @@
package webfilesystem package webfilesystem
import ( import (
"context" "errors"
"strconv"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
type DirectoryData struct { type DirectoryData struct {
Parent primitive.ObjectID `bson:"parent"` MongoId primitive.ObjectID `bson:"_id"`
Children []primitive.ObjectID `bson:"children"` 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) splittedPath := fs.SplitPath(dirPath)
parentPath := fs.GetParentPath(dirPath) parentPath := fs.GetParentPath(dirPath)
parentDirRaw, err := fs.NewRead(parentPath) parentDirId, err := fs.FindFile(parentPath)
if err != nil { 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(), MongoId: primitive.NewObjectID(),
Name: splittedPath[len(splittedPath)-1], Name: splittedPath[len(splittedPath)-1],
Type: "directory", 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 { if err != nil {
return primitive.NilObjectID, err return primitive.NilObjectID, err
} }
return objectId, nil for _, childId := range parentDirData.Children {
} childFileHeader, err := fs.readFSDocs(childId, 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))
if err != nil { if err != nil {
counter++ return primitive.NilObjectID, err
} else {
children = append(children, v.(primitive.ObjectID))
} }
} if childFileHeader.Name == fileName {
if counter > 0 { return childId, nil
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
} }
} }
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 ( import (
"errors" "errors"
"os" "os"
"path"
"go.mongodb.org/mongo-driver/bson/primitive" "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) file, err := os.ReadFile(realFilepath)
if err != nil { if err != nil {
return err return err
@ -18,57 +20,36 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, path string) error {
fileName := splittedPath[len(splittedPath)-1] fileName := splittedPath[len(splittedPath)-1]
extension := fs.GetExtension(fileName) extension := fs.GetExtension(fileName)
switch extension { newFile2 := FileHeader{
case "jpg":
fallthrough
case "jpeg":
newFile := WebFSFile{
MongoId: primitive.NewObjectID(), MongoId: primitive.NewObjectID(),
Name: fileName, Name: fileName,
Type: "jpeg", Type: "",
Data: file,
Icon: "", Icon: "",
} Data: primitive.NilObjectID,
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")
} }
newFileData := BinaryFileData{
MongoId: primitive.NewObjectID(),
Bin: file,
} }
func (fs *WebFileSystem) UploadBinaryFile(file []byte, fileName string, path string) error {
extension := fs.GetExtension(fileName) filePath := path.Join(parentPath, fileName)
switch extension { switch extension {
case "jpg": case "jpg":
fallthrough fallthrough
case "jpeg": case "jpeg":
newFile := WebFSFile{ newFile2.Type = "jpeg"
MongoId: primitive.NewObjectID(), _, _, err := fs.Write(filePath, &newFile2, &newFileData)
Name: fileName,
Type: "jpeg",
Data: file,
Icon: "",
}
err := fs.WriteFile(&newFile, path)
return err return err
case "png": case "png":
newFile := WebFSFile{ newFile2.Type = "png"
MongoId: primitive.NewObjectID(), _, _, err := fs.Write(filePath, &newFile2, &newFileData)
Name: fileName, if err != nil {
Type: "png", println(err.Error())
Data: file,
}
err := fs.WriteFile(&newFile, path)
return err return err
}
return nil
default: default:
return errors.New("this filetype not allowed") return errors.New("this filetype not allowed")
} }

View File

@ -1,94 +1,77 @@
package webfilesystem package webfilesystem
import ( import (
"context"
"errors" "errors"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
type readStruct struct { func (fs *WebFileSystem) readFSDocs(fileID primitive.ObjectID, fileData interface{}) (*FileHeader, error) {
File interface{} fileHeader := &FileHeader{}
Filter interface{} filter := primitive.M{
"_id": fileID,
} }
err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(fileHeader)
func (fs *WebFileSystem) readMongo(read readStruct) (*interface{}, error) {
err := fs.webfsCollection.FindOne(fs.ctx, read.Filter).Decode(read.File)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &read.File, nil if fileData != nil {
}
func (fs *WebFileSystem) writeMongo(file WebFSFile, parentPath string) (primitive.ObjectID, error) {
//TODO Check file existance
parentDir, err := fs.NewRead(parentPath)
if err != nil {
return primitive.NilObjectID, err
}
res, err := fs.webfsCollection.InsertOne(context.Background(), &file)
if err != nil {
return primitive.NilObjectID, err
}
fileId := fs.castInsertId(res)
fs.insertFileToDirectory(fileId, parentDir.MongoId)
return fileId, nil
}
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{ filter := primitive.M{
"_id": file.MongoId, "_id": fileHeader.Data,
} }
res, err := fs.webfsCollection.DeleteOne(fs.ctx, filter) err := fs.webfsFilesData.FindOne(fs.ctx, filter).Decode(fileData)
if err != nil { if err != nil {
return err return nil, err
}
}
return fileHeader, nil
} }
if res.DeletedCount < 1 { func (fs *WebFileSystem) writeFileToMongo(file *FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) {
return errors.New("no file removed") resData, err := fs.webfsFilesData.InsertOne(fs.ctx, data)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
file.Data = resData.InsertedID.(primitive.ObjectID)
resHeader, err := fs.webfsFilesTable.InsertOne(fs.ctx, file)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
return resHeader.InsertedID.(primitive.ObjectID), resData.InsertedID.(primitive.ObjectID), err
} }
return nil func (fs *WebFileSystem) removeFromMongo(fileId primitive.ObjectID) error {
return errors.New("todo not implemented yet")
} }
func (fs *WebFileSystem) Validate() error { func (fs *WebFileSystem) Validate() error {
filter := primitive.D{ // filter := primitive.D{
{ // {
Key: "type", // Key: "type",
Value: "directory", // Value: "directory",
}, // },
} // }
cur, err := fs.webfsCollection.Find(context.Background(), filter) // cur, err := fs.webfsCollection.Find(context.Background(), filter)
if err != nil { // if err != nil {
return err // return err
} // }
defer cur.Close(context.Background()) // defer cur.Close(context.Background())
directories := []*WebFSFile{} // directories := []*WebFSFile{}
for cur.Next(context.Background()) { // for cur.Next(context.Background()) {
dir := &WebFSFile{} // dir := &WebFSFile{}
err = cur.Decode(dir) // err = cur.Decode(dir)
if err != nil { // if err != nil {
println(err.Error()) // println(err.Error())
return err // return err
} // }
directories = append(directories, dir) // directories = append(directories, dir)
} // }
for _, d := range directories { // for _, d := range directories {
fs.validateDir(d) // fs.validateDir(d)
} // }
return nil 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 // ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
// return // return
// } // }
path := ctx.Query("path") parentPath := ctx.Query("parentPath")
if path == "" { if parentPath == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return return
} }
@ -32,7 +32,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
ctx.SaveUploadedFile(file, dst) ctx.SaveUploadedFile(file, dst)
//TODO: Not Save to disk //TODO: Not Save to disk
err := fs.UploadFile(dst, path) err := fs.UploadFile(dst, parentPath)
if err != nil { if err != nil {
ctx.String(http.StatusInternalServerError, "TODO") //TODO ctx.String(http.StatusInternalServerError, "TODO") //TODO
return return
@ -65,14 +65,14 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
// ctx.JSON(http.StatusOK, "OK") // ctx.JSON(http.StatusOK, "OK")
// }) // })
// route.GET("init", func(ctx *gin.Context) { route.GET("init", func(ctx *gin.Context) {
// err := fs.InitFS() err := fs.InitFS()
// if err != nil { if err != nil {
// ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
// return return
// } }
// ctx.JSON(http.StatusOK, "OK") ctx.JSON(http.StatusOK, "Inited")
// }) })
route.GET("createDir", func(ctx *gin.Context) { route.GET("createDir", func(ctx *gin.Context) {
path := ctx.Query("path") path := ctx.Query("path")
@ -81,7 +81,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
return return
} }
_, err := fs.NewCreateDirectory(path) _, _, err := fs.CreateDirectory(path)
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct ctx.JSON(http.StatusInternalServerError, err.Error()) //TODO json error struct
return return
@ -97,30 +97,30 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
return return
} }
files, err := fs.NewList(path) files, err := fs.ListDir(path)
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct ctx.String(http.StatusInternalServerError, err.Error()) //TODO json error struct
return return
} }
ctx.JSON(http.StatusOK, &files) ctx.JSON(http.StatusOK, &files)
}) })
route.GET("read", func(ctx *gin.Context) { // route.GET("read", func(ctx *gin.Context) {
path := ctx.Query("path") // path := ctx.Query("path")
if path == "" { // if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct // ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return // return
} // }
file, err := fs.NewRead(path) // file, err := fs.NewReadDeprecated(path)
if err != nil { // if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct // ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return // return
} // }
ctx.JSON(http.StatusOK, &file) // ctx.JSON(http.StatusOK, &file)
}) // })
route.GET("validate", func(ctx *gin.Context) { route.GET("validate", func(ctx *gin.Context) {
err := fs.Validate() err := fs.Validate()
@ -138,7 +138,7 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
ctx.Status(http.StatusBadRequest) //TODO ctx.Status(http.StatusBadRequest) //TODO
return return
} }
err := fs.Delete(path) err := fs.Remove(path)
if err != nil { if err != nil {
ctx.Status(http.StatusInternalServerError) ctx.Status(http.StatusInternalServerError)
return return

View File

@ -2,9 +2,9 @@ package webfilesystem
import ( import (
"context" "context"
"errors"
"strings" "strings"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
@ -12,57 +12,155 @@ import (
type WebFileSystem struct { type WebFileSystem struct {
webfsCollection *mongo.Collection webfsCollection *mongo.Collection
webfsFilesTable *mongo.Collection
webfsFilesData *mongo.Collection
ctx context.Context ctx context.Context
} }
func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem { func NewWebFileSystem(mongoClient *mongo.Client, dBName string, fsCollectionName string) *WebFileSystem {
return &WebFileSystem{ return &WebFileSystem{
webfsCollection: mongoClient.Database(dBName).Collection(fsCollectionName), // TODO Check collection is exist 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(), ctx: context.Background(),
} }
} }
func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) { type FileHeader struct {
filter := primitive.D{ MongoId primitive.ObjectID `bson:"_id" json:"-"`
{ Name string `bson:"name" json:"name"`
Key: "_id", Type string `bson:"type" json:"type"`
Value: objectId, Icon string `bson:"-" json:"icon"`
}, Data primitive.ObjectID `bson:"data_id" json:"-"`
} }
//TODO to readMongo() type BinaryFileData struct {
file, err := fs.findFileInMongo(filter) MongoId primitive.ObjectID `bson:"_id" json:"-"`
return file, err Bin []byte `bson:"bin" json:"-"`
} }
// Deprecated // Deprecated
func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error) { func (fs *WebFileSystem) ReadHeader(fileID primitive.ObjectID) (*FileHeader, error) {
res := fs.webfsCollection.FindOne(context.Background(), &filter) file := &FileHeader{}
file := WebFSFile{} filter := primitive.M{
err := res.Decode(&file) "_id": fileID,
}
err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(file)
return file, err
}
//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 { if err != nil {
return nil, err return primitive.NilObjectID, primitive.NilObjectID, err
} }
return &file, nil parentPath := fs.GetParentPath(filePath)
parentDir, err := fs.FindFile(parentPath)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
} }
func (fs *WebFileSystem) castInsertId(res *mongo.InsertOneResult) primitive.ObjectID { if parentDir.IsZero() {
return res.InsertedID.(primitive.ObjectID) 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 { func (fs *WebFileSystem) UpdateFileData(file *FileHeader, data interface{}) error {
dir, err := fs.ReadByObjectID(directoryId) 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 { if err != nil {
return err return err
} }
//TODO check if file exist if res.ModifiedCount < 1 {
fileData := DirectoryData{} return errors.New("no data updated")
err = mapstructure.Decode(dir.Data.(primitive.D).Map(), &fileData) }
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 { if err != nil {
return err 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 return nil
} }
@ -86,96 +184,8 @@ func (fs *WebFileSystem) GetExtension(filename string) string {
func (fs *WebFileSystem) GetParentPath(path string) string { func (fs *WebFileSystem) GetParentPath(path string) string {
splittedPath := fs.SplitPath(path) splittedPath := fs.SplitPath(path)
parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/") if len(splittedPath) > 1 {
return parentPath return "/" + strings.Join(splittedPath[1:len(splittedPath)-1], "/")
} }
return "/"
func (fs *WebFileSystem) Delete(filePath string) error {
// splittedPath := fs.SplitPath(filePath)
// parentPath := fs.GetParentPath(filePath)
_, err := fs.NewRead(filePath)
if err != nil {
return err
}
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:"-"`
} }