Compare commits

..

No commits in common. "b8eda48aa720038247f3f5b86915efd016544e08" and "405f45e788c61f726bec36be065ce759e0ce65e4" have entirely different histories.

22 changed files with 671 additions and 575 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=webfs2 COLLECTION_WEBFS=webfs

View File

@ -33,19 +33,15 @@ 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
} // }
err := b.WriteMock(path) // b.WriteMock(path)
if err != nil { // ctx.JSON(http.StatusOK, "OK")
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")
@ -71,46 +67,56 @@ func (b *BlogViewerApplication) Route(route *gin.RouterGroup) {
}) })
} }
func (b *BlogViewerApplication) WriteMock(path string) error { // func (b *BlogViewerApplication) WriteMock(path string) {
blogFileHeader := webfilesystem.FileHeader{ // blogFile := webfilesystem.WebFSFile{
MongoId: primitive.NewObjectID(), // MongoId: primitive.NewObjectID(),
Name: "blog1.blog", // Name: "test-1.blog",
Type: "", // Type: "blog-page",
Icon: "", // Data: BlogFileData{
Data: [12]byte{}, // Header: "OMG THIS IS BLOG",
} // Blocks: []Block{
blogFileData := BlogFileData{ // {
Header: "OMG THIS IS BLOG", // Type: "plain-text",
Blocks: []Block{ // Data: []string{
{ // "Apoqiwepoqiwepo",
Type: "plain-text", // ".,mas;dakls;d",
Data: []string{ // "q[poqwieqpipoi]",
"Apoqiwepoqiwepo", // },
".,mas;dakls;d", // },
"q[poqwieqpipoi]", // },
}, // },
}, // }
}, // err := b.fs.CreateFile(&blogFile, path)
} // if err != nil {
// println(err.Error())
// }
// }
_, _, err := b.fs.Write("/home/user/blog1.blog", &blogFileHeader, blogFileData) func (b *BlogViewerApplication) Render(path string, isMobile bool) (gin.H, error) {
file, err := b.fs.NewRead(path)
if err != nil { if err != nil {
println(err.Error()) println(err.Error())
return err
}
return nil
}
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 nil, err
} }
blogDataRaw := file.Data.(primitive.D).Map()
header := blogDataRaw["header"]
blocks := []Block{}
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)
}
return gin.H{ return gin.H{
"header": data.Header, "header": header,
"blocks": data.Blocks, "blocks": 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.NewReadDeprecated(filePath) file, err := f.fs.NewRead(filePath)
// if err != nil { if err != nil {
// return nil return nil
// } }
return gin.H{ return gin.H{
// "file": file, "file": file,
} }
} }

View File

@ -1,8 +1,10 @@
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"
@ -26,25 +28,12 @@ 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"
filePath := ctx.Query("path") ginH, err := p.Render()
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)
@ -61,65 +50,170 @@ func (p *PersonalPropertiesApp) GetId() string {
return p.manifest.AppId return p.manifest.AppId
} }
func (p *PersonalPropertiesApp) WriteMock() error { // func (p *PersonalPropertiesApp) WriteToDb() {
fileHeader := webfilesystem.FileHeader{ // allProps := make([]PropIsland, 0)
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
}
func (p *PersonalPropertiesApp) Render(filePath string) (gin.H, error) { // // careerProps := make([]Prop, 0)
propsData := PropertiesFileData{} // expertiseIsland := PropIsland{
_, err := p.fs.Read(filePath, &propsData) // 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")
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": propsData.Header, "headerProps": hIsland,
"allprops": propsData.Props, "allprops": allProps,
}, nil }, nil
} }
type HeaderIsland struct { type HeaderIsland struct {
Name string `bson:"name"` Name string
IconPath string `bson:"iconpath"` Icon *libs.Base64Img
Info1 string `bson:"info1"` Info1 string
Info2 string `bson:"info2"` Info2 string
} }
type PropElement struct { type PropElement struct {
@ -133,7 +227,6 @@ type PropIsland struct {
Props []PropElement Props []PropElement
} }
type PropertiesFileData struct { type PropertiesFile struct {
Header HeaderIsland `bson:"header"`
Props []PropIsland `bson:"props"` Props []PropIsland `bson:"props"`
} }

View File

@ -1,10 +1,12 @@
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 {
@ -17,26 +19,40 @@ 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.String(http.StatusBadRequest, "TODO") //TODO json error struct ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return return
} }
imgData := Img{} file, err := l.fs.NewRead(path)
_, err := l.fs.Read(path, &imgData)
if err != nil { if err != nil {
ctx.Status(http.StatusInternalServerError) ctx.String(http.StatusInternalServerError, "TODO") //TODO
return
} }
// _, err = l.fs.readFSDocs(fileId, &imgData) data := file.Data.(primitive.Binary)
// if err != nil {
// ctx.Status(http.StatusInternalServerError)
// }
ctx.Data(http.StatusOK, "image/jpeg", imgData.Bin) ctx.Data(http.StatusOK, "image/jpeg", data.Data)
}) })
} }
@ -61,6 +77,6 @@ type Base64Img struct {
type Img struct { type Img struct {
Header string `bson:"header"` Header string `bson:"header"`
Bin []byte `bson:"bin"` Data []byte `bson:"srcdata"`
// BinMin32 []byte `bson:"binmin32"` Miniature32 []byte `bson:"min32data"`
} }

View File

@ -15,17 +15,17 @@
.blog-viewer .header-h1{ .blog-viewer .header-h1{
font-size:x-large; font-size:x-large;
padding-bottom: 10px; padding: 10px;
} }
.blog-viewer .header-h2{ .blog-viewer .header-h2{
font-size:large; font-size:large;
padding-bottom: 6px; padding: 6px;
} }
.blog-viewer .header-h3{ .blog-viewer .header-h3{
font-size:larger; font-size:larger;
padding-bottom: 2px; padding: 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("#RootButton").addEventListener('click', () =>{ // newWindow.querySelector("#BackButton").addEventListener('click', () =>{
this.OpenDir('/') // this.OpenPreviousDir()
}) // })
// 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(this.path) // console.log(filesList)
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({
parentPath: this.path, path: '/home/user/',
}), }),
{ {
method: "POST", method: "POST",
@ -125,16 +125,15 @@ class Finder{
* @param {string} path * @param {string} path
*/ */
OpenDir(path){ OpenDir(path){
this.path = path this.fileView.OpenFolder(this.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,23 +22,7 @@
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; */
@ -57,7 +41,7 @@
} }
.PropertiesList .ShortBio{ .Personal-properties-bio{
/* width: 100%; /* width: 100%;
height: auto; */ height: auto; */
/* margin-right: -20px; */ /* margin-right: -20px; */
@ -77,7 +61,9 @@
gap:15px; gap:15px;
} }
.ShortBio .Text{
.Personal-properties-textbio{
/* width: 100%; /* width: 100%;
height: auto; */ height: auto; */
@ -95,14 +81,8 @@
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;
@ -117,9 +97,11 @@
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;
@ -129,11 +111,7 @@
top: -9px; top: -9px;
} }
.Focused .Island .Title{ .Personal-properties-prop-content{
background-color: #CCCCCC;
}
.Island .Content{
width: 100%; width: 100%;
/* top: 0px; */ /* top: 0px; */
/* Auto layout */ /* Auto layout */
@ -144,7 +122,7 @@
gap: 12px; gap: 12px;
} }
.Island .Row{ .Personal-properties-prop-row{
margin-left: 12px; margin-left: 12px;
margin-right: 12px; margin-right: 12px;
/* Auto layout */ /* Auto layout */
@ -154,7 +132,7 @@
padding: 0px; padding: 0px;
gap: 5px; gap: 5px;
} }
.Island .Key{ .Personal-properties-prop-key{
position: relative; position: relative;
font-family: "Virtue"; font-family: "Virtue";
font-size: 11px; font-size: 11px;
@ -167,7 +145,7 @@
/* font-weight: bold; */ /* font-weight: bold; */
} }
.Island .KeyComment{ .Personal-properties-prop-key-comments{
/* color: rgb(129, 129, 129); TODO*/ /* color: rgb(129, 129, 129); TODO*/
color: #646464; color: #646464;
font-size: 9px; font-size: 9px;
@ -176,8 +154,7 @@
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;
@ -187,7 +164,7 @@
} }
.Values .Value{ .Personal-properties-prop-value{
/* width: 55%; */ /* width: 55%; */
} }

View File

@ -13,7 +13,6 @@ 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) => {
@ -23,7 +22,7 @@ class PersonalProperties{
newWindow.innerHTML = html newWindow.innerHTML = html
let scrollBar = new WdeScrollBar(newWindow.querySelector('.ScrollBarScrollElement'), newWindow.querySelector('.PropsView')) let scrollBar = new WdeScrollBar(newWindow.querySelector('.ScrollBarScrollElement'), newWindow.querySelector('.ScrollContent'))
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/blog1.blog"]) // WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"])
WebDesktopEnvironment.Open("personal-properties", ["/home/user/aboutme.props"]) // WebDesktopEnvironment.Open("personal-properties", ["kek"])
} 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="RootButton">/</button> <button id="BackButton">Back</button>
<button id="HomeButton">Home</button> <button id="HomeButton">Home</button>
</div> </div>
<div class="FinderFileView"> <div class="FinderFileView">

View File

@ -7,37 +7,36 @@
</div> </div>
<div id="Drag" class="VisualDragArea"></div> <div id="Drag" class="VisualDragArea"></div>
</div> </div>
<div class="ContentBorder AdjectiveElement"> <div class="ContentBorder">
<div class="PersPropsContent"> <div class="ScrollContent">
<div class="PropsView">
<div class="PropertiesList"> <div class="PropertiesList">
<div class="ShortBio"> <div class="Personal-properties-bio">
<img src="/system/libs/img/get?path={{ .headerProps.IconPath }}" alt="My Photo" style="width: 48px;height: 48px;"> <img src="data:{{ .headerProps.Icon.Header }},{{ .headerProps.Icon.Base64 }}" alt="My Photo" style="width: 48px;height: 48px;">
<div class="Text"> <div class="Personal-properties-textbio">
<div class="Name">{{ .headerProps.Name }}</div> <div>{{ .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 class="Island"> <div id="prop" class="Personal-properties-prop">
<div class="Title"> <div class="Personal-properties-prop-title">
{{$propIsland.Header}}: {{$propIsland.Header}}:
</div> </div>
<div class="Content"> <div class="Personal-properties-prop-content">
{{range $prop := $propIsland.Props}} {{range $prop := $propIsland.Props}}
<div class="Row"> <div class="Personal-properties-prop-row">
<div class="Key"> <div class="Personal-properties-prop-key">
{{$prop.Key}}: {{$prop.Key}}:
{{ range $value := $prop.KeyComments }} {{ range $value := $prop.KeyComments }}
<div class="KeyComment"> <div class="Personal-properties-prop-key-comments">
{{ $value }} {{ $value }}
</div> </div>
{{ end }} {{ end }}
</div> </div>
<div class="Values"> <div class="Personal-properties-prop-values">
{{ range $value := $prop.Values }} {{ range $value := $prop.Values }}
<div class="Value"> <div class="Personal-properties-prop-value">
{{ $value }} {{ $value }}
</div> </div>
{{ end }} {{ end }}
@ -51,7 +50,6 @@
</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)

Binary file not shown.

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

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.NewListDeprecated(path) list, err := w.fs.NewList(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,11 +44,10 @@ 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.ListDir(directory) list, err := w.fs.NewList(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":
@ -60,7 +59,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/folder.png" //FIXME file.Icon = host + "/system/libs/img/get?path=/wde/icons/macos9/about-me.png" //FIXME
} }
} }
return gin.H{ return gin.H{

View File

@ -1,136 +1,84 @@
package webfilesystem package webfilesystem
import ( import (
"errors" "context"
"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 {
MongoId primitive.ObjectID `bson:"_id"` Parent primitive.ObjectID `bson:"parent"`
Parent primitive.ObjectID `bson:"parent_id"` Children []primitive.ObjectID `bson:"children"`
Children []primitive.ObjectID `bson:"children_id"`
} }
func (fs *WebFileSystem) CreateDirectory(dirPath string) (primitive.ObjectID, primitive.ObjectID, error) { func (fs *WebFileSystem) NewCreateDirectory(dirPath string) (primitive.ObjectID, error) {
splittedPath := fs.SplitPath(dirPath) splittedPath := fs.SplitPath(dirPath)
parentPath := fs.GetParentPath(dirPath) parentPath := fs.GetParentPath(dirPath)
parentDirId, err := fs.FindFile(parentPath) parentDirRaw, err := fs.NewRead(parentPath)
if err != nil { if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err return primitive.NilObjectID, err
} }
parentDirHeader, err := fs.readFSDocs(parentDirId, nil) newDir := WebFSFile{
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: "",
} }
newDirData := DirectoryData{ objectId, err := fs.writeMongo(newDir, parentPath)
MongoId: primitive.NewObjectID(), if err != nil {
Parent: parentDirHeader.MongoId, return primitive.NilObjectID, err
Children: []primitive.ObjectID{},
} }
headerId, dataId, err := fs.writeFileToMongo(&newDir, &newDirData) return objectId, nil
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
} }
err = fs.AppendChildToDirectory(headerId, parentDirId) func (fs *WebFileSystem) validateDir(dir *WebFSFile) error {
if err != nil { kek := dir.Data.(primitive.D).Map()["children"].(primitive.A)
return primitive.NilObjectID, primitive.NilObjectID, err _ = kek
}
return headerId, dataId, nil
}
func (fs *WebFileSystem) AppendChildToDirectory(childId primitive.ObjectID, parentId primitive.ObjectID) error { children := []primitive.ObjectID{}
parentDirData := DirectoryData{} counter := 0
parentDir, err := fs.readFSDocs(parentId, &parentDirData) for _, v := range kek {
_, err := fs.ReadByObjectID(v.(primitive.ObjectID))
if err != nil { if err != nil {
counter++
} else {
children = append(children, v.(primitive.ObjectID))
}
}
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 return err
} }
//TODO Check for duplicates?
parentDirData.Children = append(parentDirData.Children, childId)
err = fs.UpdateFileData(parentDir, parentDirData)
if err != nil {
return err
} }
return nil return nil
} }
func (fs *WebFileSystem) ListDir(dirPath string) ([]*FileHeader, error) { func castToFile(raw *interface{}) *WebFSFile {
dirId, err := fs.FindFile(dirPath) var dirPtr interface{} = *raw
if err != nil { return dirPtr.(*WebFSFile)
return nil, err
} }
func castToDirectoryData(data interface{}) (*DirectoryData, error) {
dirData := DirectoryData{} dirData := DirectoryData{}
_, err = fs.readFSDocs(dirId, &dirData) err := mapstructure.Decode(data.(primitive.D).Map(), &dirData)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &dirData, nil
// 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) // func List()
}
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
}
for _, childId := range parentDirData.Children {
childFileHeader, err := fs.readFSDocs(childId, nil)
if err != nil {
return primitive.NilObjectID, err
}
if childFileHeader.Name == fileName {
return childId, nil
}
}
return primitive.NilObjectID, errors.New("file not found")
}

View File

@ -5,13 +5,11 @@ 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, parentPath string) error { func (fs *WebFileSystem) UploadFile(realFilepath string, path 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
@ -20,36 +18,57 @@ func (fs *WebFileSystem) UploadFile(realFilepath string, parentPath string) erro
fileName := splittedPath[len(splittedPath)-1] fileName := splittedPath[len(splittedPath)-1]
extension := fs.GetExtension(fileName) extension := fs.GetExtension(fileName)
newFile2 := FileHeader{
MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "",
Icon: "",
Data: primitive.NilObjectID,
}
newFileData := BinaryFileData{
MongoId: primitive.NewObjectID(),
Bin: file,
}
filePath := path.Join(parentPath, fileName)
switch extension { switch extension {
case "jpg": case "jpg":
fallthrough fallthrough
case "jpeg": case "jpeg":
newFile2.Type = "jpeg" newFile := WebFSFile{
_, _, err := fs.Write(filePath, &newFile2, &newFileData) MongoId: primitive.NewObjectID(),
Name: fileName,
Type: "jpeg",
Data: file,
Icon: "",
}
err := fs.WriteFile(&newFile, path)
return err return err
case "png": case "png":
newFile2.Type = "png" newFile := WebFSFile{
_, _, err := fs.Write(filePath, &newFile2, &newFileData) MongoId: primitive.NewObjectID(),
if err != nil { Name: fileName,
println(err.Error()) Type: "png",
return err Data: file,
} }
return nil err := fs.WriteFile(&newFile, path)
return err
default:
return errors.New("this filetype not allowed")
}
}
func (fs *WebFileSystem) UploadBinaryFile(file []byte, fileName string, path string) error {
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: default:
return errors.New("this filetype not allowed") return errors.New("this filetype not allowed")
} }

View File

@ -1,77 +1,94 @@
package webfilesystem package webfilesystem
import ( import (
"context"
"errors" "errors"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
func (fs *WebFileSystem) readFSDocs(fileID primitive.ObjectID, fileData interface{}) (*FileHeader, error) { type readStruct struct {
fileHeader := &FileHeader{} File interface{}
filter := primitive.M{ Filter interface{}
"_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
} }
if fileData != nil { return &read.File, 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": fileHeader.Data, "_id": file.MongoId,
} }
err := fs.webfsFilesData.FindOne(fs.ctx, filter).Decode(fileData) res, err := fs.webfsCollection.DeleteOne(fs.ctx, filter)
if err != nil { if err != nil {
return nil, err return err
}
}
return fileHeader, nil
} }
func (fs *WebFileSystem) writeFileToMongo(file *FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) { if res.DeletedCount < 1 {
resData, err := fs.webfsFilesData.InsertOne(fs.ctx, data) return errors.New("no file removed")
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
} }
func (fs *WebFileSystem) removeFromMongo(fileId primitive.ObjectID) error { return nil
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

@ -0,0 +1,56 @@
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")
}

View File

@ -1,15 +0,0 @@
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
// } // }
parentPath := ctx.Query("parentPath") path := ctx.Query("path")
if parentPath == "" { if path == "" {
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, parentPath) err := fs.UploadFile(dst, path)
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, "Inited") // ctx.JSON(http.StatusOK, "OK")
}) // })
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.CreateDirectory(path) _, err := fs.NewCreateDirectory(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.ListDir(path) files, err := fs.NewList(path)
if err != nil { if err != nil {
ctx.String(http.StatusInternalServerError, err.Error()) //TODO json error struct ctx.JSON(http.StatusInternalServerError, "TODO") //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.NewReadDeprecated(path) file, err := fs.NewRead(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.Remove(path) err := fs.Delete(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,155 +12,57 @@ 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(),
} }
} }
type FileHeader struct { func (fs *WebFileSystem) ReadByObjectID(objectId primitive.ObjectID) (*WebFSFile, error) {
MongoId primitive.ObjectID `bson:"_id" json:"-"` filter := primitive.D{
Name string `bson:"name" json:"name"` {
Type string `bson:"type" json:"type"` Key: "_id",
Icon string `bson:"-" json:"icon"` Value: objectId,
Data primitive.ObjectID `bson:"data_id" json:"-"` },
} }
type BinaryFileData struct { //TODO to readMongo()
MongoId primitive.ObjectID `bson:"_id" json:"-"` file, err := fs.findFileInMongo(filter)
Bin []byte `bson:"bin" json:"-"`
}
// Deprecated
func (fs *WebFileSystem) ReadHeader(fileID primitive.ObjectID) (*FileHeader, error) {
file := &FileHeader{}
filter := primitive.M{
"_id": fileID,
}
err := fs.webfsFilesTable.FindOne(fs.ctx, filter).Decode(file)
return file, err return file, err
} }
//TODO To private, get name from path and set it to file struct, force set newObjectID // Deprecated
func (fs *WebFileSystem) Write(filePath string, file *FileHeader, data interface{}) (primitive.ObjectID, primitive.ObjectID, error) { func (fs *WebFileSystem) findFileInMongo(filter primitive.D) (*WebFSFile, error) {
headerId, dataId, err := fs.writeFileToMongo(file, data) res := fs.webfsCollection.FindOne(context.Background(), &filter)
file := WebFSFile{}
err := res.Decode(&file)
if err != nil { if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err return nil, err
} }
parentPath := fs.GetParentPath(filePath) return &file, nil
parentDir, err := fs.FindFile(parentPath)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
} }
if parentDir.IsZero() { func (fs *WebFileSystem) castInsertId(res *mongo.InsertOneResult) primitive.ObjectID {
return primitive.NilObjectID, primitive.NilObjectID, errors.New("parent dir not found") return res.InsertedID.(primitive.ObjectID)
}
err = fs.AppendChildToDirectory(headerId, parentDir)
if err != nil {
return primitive.NilObjectID, primitive.NilObjectID, err
}
return headerId, dataId, nil
} }
func (fs *WebFileSystem) UpdateFileData(file *FileHeader, data interface{}) error { func (fs *WebFileSystem) insertFileToDirectory(fileId primitive.ObjectID, directoryId primitive.ObjectID) error {
update := bson.M{"$set": data} dir, err := fs.ReadByObjectID(directoryId)
// 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
} }
if res.ModifiedCount < 1 { //TODO check if file exist
return errors.New("no data updated") fileData := DirectoryData{}
} 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
} }
return nil fileData.Children = append(fileData.Children, fileId)
}
//TODO get on boot and safe to struct fs.webfsCollection.UpdateByID(context.Background(), directoryId, bson.M{"$set": bson.M{"data": fileData}})
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
} }
@ -184,8 +86,96 @@ 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)
if len(splittedPath) > 1 { parentPath := strings.Join(splittedPath[:len(splittedPath)-1], "/")
return "/" + strings.Join(splittedPath[1:len(splittedPath)-1], "/") return parentPath
} }
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:"-"`
} }