-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyFile.go
More file actions
135 lines (114 loc) · 2.36 KB
/
Copy pathmyFile.go
File metadata and controls
135 lines (114 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"github.com/google/uuid"
"strings"
"errors"
)
type Chunk struct {
id uuid.UUID
server [3]int
}
type MyFile struct {
id uuid.UUID
isFile bool
path string
basename string
extension string
filename string
files []*MyFile
chunks []*Chunk
}
var root *MyFile
func newChunk(index int) *Chunk {
tmp := new(Chunk)
tmp.id = uuid.New()
for i, j := 0, 0; i < 4 && j < 3; i++ {
if i != index {
tmp.server[j] = i
j++
}
}
return tmp
}
func exists(path string, name string) bool {
currentPath := getFileFromPath(path)
for _, previousFile := range currentPath.files {
if name == previousFile.basename {
return true
}
}
return false
}
func newFile(path string, name string, isFile bool) (*MyFile, error) {
tmp := new(MyFile)
//for creating root
if path == "" && name == "" {
tmp.files = make([]*MyFile, 1)
tmp.files[0] = tmp
return tmp, nil
}
tmp.id = uuid.New()
tmp.isFile = isFile
tmp.path = path
tmp.basename = name
//add to path
currentPath := getFileFromPath(path)
//check for same file
for _, previousFile := range currentPath.files {
if name == previousFile.basename {
return nil, errors.New("file existed")
}
}
currentPath.files = append(currentPath.files, tmp)
if isFile {
//tmp.chunks = make([]Chunk, chunkNum)
index := strings.LastIndex(name, ".")
if index != -1 {
tmp.filename = name[0:index]
tmp.extension = name[index+1:]
} else {
tmp.filename = name
tmp.extension = ""
}
} else {
tmp.files = make([]*MyFile, 1)
tmp.files[0] = tmp
}
return tmp, nil
}
func getFileFromPath(path string) *MyFile {
if path == "" {
return root
}
paths := strings.Split(path, "/")
currentPath := root
flag := false
for i, dir := range paths {
for _, file := range currentPath.files {
if file.basename == dir {
currentPath = file
if i == len(paths)-1 {
flag = true
}
break
}
}
}
if flag {
return currentPath
}
return nil
}
/*func main() {
root, _ = newFile("", "", false, 0)
a, _ := newFile("", "a", false, 0)
_, err := newFile("", "a", true, 4)
g:=getFileFromPath("a")
c, _ := newFile("a", "c", false, 0)
d, _ := newFile("a/c", "d", false, 0)
e, _ := newFile("a/c", "e.233", true, 44)
fmt.Println(a.id, err, c.id, d.id, e.id)
f:=getFileFromPath("a/c/e.233")
h := exists("a/c", "e.233")
fmt.Println(f.id, g.id, h)
}*/