[server] Remove name conflict prefix

This commit is contained in:
Abhishek Shroff
2024-11-01 12:17:22 +05:30
parent 260fac2952
commit 42db526aa7
19 changed files with 183 additions and 207 deletions

View File

@@ -26,14 +26,14 @@ var htmlReplacer = strings.NewReplacer(
var ErrRangeNotSupported = errors.New("byte range not suppoered")
type ResourceInfo interface {
FSName() string
FSPath() string
FSDir() bool
FSCreated() time.Time
FSModified() time.Time
FSContentSize() int64
FSContentSHA256() string
FSContentType() string
Name() string
Path() string
Dir() bool
Created() time.Time
Modified() time.Time
ContentSize() int64
ContentSHA256() string
ContentType() string
}
type Resource interface {
@@ -43,7 +43,7 @@ type Resource interface {
}
func Serve(w http.ResponseWriter, r *http.Request, res Resource) {
if res.FSDir() {
if res.Dir() {
serveCollection(w, r, res)
} else {
serveResource(w, r, res)
@@ -59,7 +59,7 @@ func serveCollection(w http.ResponseWriter, r *http.Request, file Resource) {
writeNotModified(w)
return
}
w.Header().Set("Last-Modified", file.FSModified().Format(http.TimeFormat))
w.Header().Set("Last-Modified", file.Modified().Format(http.TimeFormat))
files, err := file.ReadDir(false)
if err != nil {
@@ -70,29 +70,29 @@ func serveCollection(w http.ResponseWriter, r *http.Request, file Resource) {
sort.Slice(files, func(i, j int) bool {
a := files[i]
b := files[j]
if a.FSDir() != b.FSDir() {
if a.FSDir() {
if a.Dir() != b.Dir() {
if a.Dir() {
return true
}
}
return strings.Compare(strings.ToLower(a.FSName()), strings.ToLower((b.FSName()))) <= 0
return strings.Compare(strings.ToLower(a.Name()), strings.ToLower((b.Name()))) <= 0
})
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintln(w, "<html>")
fmt.Fprintln(w, "<head>")
fmt.Fprintf(w, "<title>%s | Phylum</title>\n", file.FSPath())
fmt.Fprintf(w, "<title>%s | Phylum</title>\n", file.Path())
fmt.Fprintln(w, "<style>td, th {padding: 4px 12px;}</style>")
fmt.Fprintln(w, "</head>")
fmt.Fprintln(w, "<body>")
fmt.Fprintln(w, "<pre>")
fmt.Fprintf(w, "<h2>Index of %s</h2>", file.FSPath())
fmt.Fprintf(w, "<h2>Index of %s</h2>", file.Path())
fmt.Fprintln(w, "<table>")
fmt.Fprintln(w, "<thead><tr><th>Name</th><th>Size</th><th>Content-Type</th><th>SHA-256</th></tr></thead>")
fmt.Fprintln(w, "<tbody>")
for _, f := range files {
name := f.FSName()
if f.FSDir() {
name := f.Name()
if f.Dir() {
name += "/"
}
// name may contain '?' or '#', which must be escaped to remain
@@ -101,10 +101,10 @@ func serveCollection(w http.ResponseWriter, r *http.Request, file Resource) {
url := url.URL{Path: name}
link := fmt.Sprintf("<a href=\"%s\">%s</a>", url.String(), htmlReplacer.Replace(name))
if f.FSDir() {
if f.Dir() {
fmt.Fprintf(w, "<tr><td>%s</td></tr>\n", link)
} else {
fmt.Fprintf(w, "<tr><td>%s</td><td align=\"right\">%s</td><td>%s</td><td>%s</td></tr>\n", link, formatSize(f.FSContentSize()), f.FSContentType(), f.FSContentSHA256()[0:12])
fmt.Fprintf(w, "<tr><td>%s</td><td align=\"right\">%s</td><td>%s</td><td>%s</td></tr>\n", link, formatSize(f.ContentSize()), f.ContentType(), f.ContentSHA256()[0:12])
}
}
fmt.Fprintln(w, "</tbody></table></pre>\n")
@@ -120,9 +120,9 @@ func formatSize(size int64) string {
}
func serveResource(w http.ResponseWriter, r *http.Request, file Resource) {
w.Header().Set("Etag", file.FSContentSHA256())
w.Header().Set("Last-Modified", file.FSModified().Format(http.TimeFormat))
w.Header().Set("Content-Type", file.FSContentType())
w.Header().Set("Etag", file.ContentSHA256())
w.Header().Set("Last-Modified", file.Modified().Format(http.TimeFormat))
w.Header().Set("Content-Type", file.ContentType())
done, rangeReq := checkPreconditions(w, r, file)
if done {
@@ -130,10 +130,10 @@ func serveResource(w http.ResponseWriter, r *http.Request, file Resource) {
}
code := http.StatusOK
sendSize := file.FSContentSize()
ranges, err := parseRange(rangeReq, file.FSContentSize())
sendSize := file.ContentSize()
ranges, err := parseRange(rangeReq, file.ContentSize())
if err != nil {
w.Header().Set("Content-Range", fmt.Sprintf("bytes */%d", file.FSContentSize))
w.Header().Set("Content-Range", fmt.Sprintf("bytes */%d", file.ContentSize))
http.Error(w, err.Error(), http.StatusRequestedRangeNotSatisfiable)
return
}
@@ -156,7 +156,7 @@ func serveResource(w http.ResponseWriter, r *http.Request, file Resource) {
reader, err = file.OpenRead(ra.start, ra.length)
if err == nil {
code = http.StatusPartialContent
w.Header().Set("Content-Range", ra.contentRange(file.FSContentSize()))
w.Header().Set("Content-Range", ra.contentRange(file.ContentSize()))
} else if errors.Is(err, ErrRangeNotSupported) {
err = nil
}
@@ -374,7 +374,7 @@ func checkIfMatch(r *http.Request, ri Resource) condResult {
if etag == "" {
break
}
if etagStrongMatch(etag, ri.FSContentSHA256()) {
if etagStrongMatch(etag, ri.ContentSHA256()) {
return condTrue
}
im = remain
@@ -385,7 +385,7 @@ func checkIfMatch(r *http.Request, ri Resource) condResult {
func checkIfUnmodifiedSince(r *http.Request, ri Resource) condResult {
ius := r.Header.Get("If-Unmodified-Since")
if ius == "" || isZeroTime(ri.FSModified()) {
if ius == "" || isZeroTime(ri.Modified()) {
return condNone
}
t, err := http.ParseTime(ius)
@@ -395,7 +395,7 @@ func checkIfUnmodifiedSince(r *http.Request, ri Resource) condResult {
// The Last-Modified header truncates sub-second precision so
// the modtime needs to be truncated too.
modtime := ri.FSModified().Truncate(time.Second)
modtime := ri.Modified().Truncate(time.Second)
if ret := modtime.Compare(t); ret <= 0 {
return condTrue
}
@@ -424,7 +424,7 @@ func checkIfNoneMatch(r *http.Request, ri Resource) condResult {
if etag == "" {
break
}
if etagWeakMatch(etag, ri.FSContentSHA256()) {
if etagWeakMatch(etag, ri.ContentSHA256()) {
return condFalse
}
buf = remain
@@ -437,7 +437,7 @@ func checkIfModifiedSince(r *http.Request, ri Resource) condResult {
return condNone
}
ims := r.Header.Get("If-Modified-Since")
if ims == "" || isZeroTime(ri.FSModified()) {
if ims == "" || isZeroTime(ri.Modified()) {
return condNone
}
t, err := http.ParseTime(ims)
@@ -446,7 +446,7 @@ func checkIfModifiedSince(r *http.Request, ri Resource) condResult {
}
// The Last-Modified header truncates sub-second precision so
// the modtime needs to be truncated too.
modtime := ri.FSModified().Truncate(time.Second)
modtime := ri.Modified().Truncate(time.Second)
if ret := modtime.Compare(t); ret <= 0 {
return condFalse
}
@@ -463,7 +463,7 @@ func checkIfRange(r *http.Request, ri Resource) condResult {
}
etag, _ := scanETag(ir)
if etag != "" {
if etagStrongMatch(etag, ri.FSContentSHA256()) {
if etagStrongMatch(etag, ri.ContentSHA256()) {
return condTrue
} else {
return condFalse
@@ -471,14 +471,14 @@ func checkIfRange(r *http.Request, ri Resource) condResult {
}
// The If-Range value is typically the ETag value, but it may also be
// the modtime date. See golang.org/issue/8367.
if ri.FSModified().IsZero() {
if ri.Modified().IsZero() {
return condFalse
}
t, err := http.ParseTime(ir)
if err != nil {
return condFalse
}
if t.Unix() == ri.FSModified().Unix() {
if t.Unix() == ri.Modified().Unix() {
return condTrue
}
return condFalse

View File

@@ -16,7 +16,7 @@ func handleCatRequest(c *gin.Context) {
f := auth.GetFileSystem(c)
r, err := f.ResourceByID(resourceID)
if r.FSDir() {
if r.Dir() {
err = fs.ErrResourceCollection
}
if err != nil {

View File

@@ -21,15 +21,15 @@ func handleDownloadRequest(c *gin.Context) {
f := auth.GetFileSystem(c)
r, err := f.ResourceByID(resourceID)
prefix := path.Dir(r.FSPath())
prefix := path.Dir(r.Path())
c.Writer.Header().Set("Content-Type", "application/zip")
c.Writer.Header().Set("Content-Disposition", "attachment; filename=\""+r.FSName()+".zip\"")
c.Writer.Header().Set("Content-Disposition", "attachment; filename=\""+r.Name()+".zip\"")
var zip = zip.NewWriter(c.Writer)
close := c.Writer.CloseNotify()
err = r.Walk(2, func(r serve.ResourceInfo) error {
if r.FSDir() {
if r.Dir() {
return nil
}
@@ -39,7 +39,7 @@ func handleDownloadRequest(c *gin.Context) {
default:
}
path, found := strings.CutPrefix(r.FSPath(), prefix)
path, found := strings.CutPrefix(r.Path(), prefix)
if !found {
return fs.ErrResourcePathInvalid
}

View File

@@ -137,7 +137,7 @@ func props(fs FileSystem, ls LockSystem, fi serve.ResourceInfo, pnames []xml.Nam
pstatNotFound := Propstat{Status: http.StatusNotFound}
for _, pn := range pnames {
// Otherwise, it must either be a live property or we don't know it.
if prop := liveProps[pn]; prop.findFn != nil && (prop.dir || !fi.FSDir()) {
if prop := liveProps[pn]; prop.findFn != nil && (prop.dir || !fi.Dir()) {
innerXML := prop.findFn(fi)
pstatOK.Props = append(pstatOK.Props, Property{
XMLName: pn,
@@ -156,7 +156,7 @@ func props(fs FileSystem, ls LockSystem, fi serve.ResourceInfo, pnames []xml.Nam
func propnames(fs FileSystem, ls LockSystem, fi serve.ResourceInfo) ([]xml.Name, error) {
pnames := make([]xml.Name, 0, len(liveProps))
for pn, prop := range liveProps {
if prop.findFn != nil && (prop.dir || !fi.FSDir()) {
if prop.findFn != nil && (prop.dir || !fi.Dir()) {
pnames = append(pnames, pn)
}
}
@@ -253,34 +253,34 @@ func escapeXML(s string) string {
}
func findResourceType(fi serve.ResourceInfo) string {
if fi.FSDir() {
if fi.Dir() {
return `<D:collection xmlns:D="DAV:"/>`
}
return ""
}
func findDisplayName(fi serve.ResourceInfo) string {
return escapeXML(fi.FSName())
return escapeXML(fi.Name())
}
func findContentLength(fi serve.ResourceInfo) string {
return strconv.FormatInt(fi.FSContentSize(), 10)
return strconv.FormatInt(fi.ContentSize(), 10)
}
func findCreationDate(fi serve.ResourceInfo) string {
return fi.FSCreated().UTC().Format(http.TimeFormat)
return fi.Created().UTC().Format(http.TimeFormat)
}
func findLastModified(fi serve.ResourceInfo) string {
return fi.FSModified().UTC().Format(http.TimeFormat)
return fi.Modified().UTC().Format(http.TimeFormat)
}
func findContentType(fi serve.ResourceInfo) string {
return fi.FSContentType()
return fi.ContentType()
}
func findETag(fi serve.ResourceInfo) string {
return fi.FSContentSHA256()
return fi.ContentSHA256()
}
func findSupportedLock(fi serve.ResourceInfo) string {

View File

@@ -167,7 +167,7 @@ func (h *Handler) confirmLocks(r *http.Request, src, dst string) (release func()
if res, err := h.FileSystem.ResourceByPath(path); err != nil {
return false, err
} else {
return res.FSContentSHA256() == etag, nil
return res.ContentSHA256() == etag, nil
}
}, l.conditions...)
if err == ErrConfirmationFailed {
@@ -192,7 +192,7 @@ func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) (status
}
allow := "OPTIONS, LOCK, PUT, MKCOL"
if fi, err := h.FileSystem.ResourceByPath(reqPath); err == nil {
if fi.FSDir() {
if fi.Dir() {
allow = "OPTIONS, LOCK, DELETE, PROPPATCH, COPY, MOVE, UNLOCK, PROPFIND"
} else {
allow = "OPTIONS, LOCK, GET, HEAD, POST, DELETE, PROPPATCH, COPY, MOVE, UNLOCK, PROPFIND, PUT"
@@ -268,7 +268,7 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int,
}
} else if err != nil {
return http.StatusNotFound, err
} else if res.FSDir() {
} else if res.Dir() {
return http.StatusConflict, fs.ErrResourceCollection
}
@@ -286,7 +286,7 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int,
if closeErr != nil {
return http.StatusMethodNotAllowed, closeErr
}
w.Header().Set("ETag", fi.FSContentSHA256())
w.Header().Set("ETag", fi.ContentSHA256())
return http.StatusCreated, nil
}
@@ -476,8 +476,8 @@ func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request) (status
if err != nil {
return err
}
href := path.Join(h.Prefix, r.FSPath())
if href != "/" && r.FSDir() {
href := path.Join(h.Prefix, r.Path())
if href != "/" && r.Dir() {
href += "/"
}
return mw.write(makePropstatResponse(href, pstats))

View File

@@ -23,7 +23,7 @@ func setupCatCommand() *cobra.Command {
os.Exit(1)
}
if r.FSDir() {
if r.Dir() {
fmt.Println("cannot read'" + pathOrUUID + "': is a directory")
os.Exit(2)
}

View File

@@ -24,7 +24,7 @@ func setupCpCommand() *cobra.Command {
}
force, _ := cmd.Flags().GetBool("force")
if src.FSDir() {
if src.Dir() {
if recursive, err := cmd.Flags().GetBool("recursive"); err != nil || !recursive {
fmt.Println("cannot copy'" + srcPathOrUUID + "' to '" + args[1] + "': must use -r to copy directories")
os.Exit(1)

View File

@@ -25,10 +25,10 @@ func setupLsCommand() *cobra.Command {
os.Exit(1)
}
fmt.Println(r.FSPath() + " " + r.FullPermissionsString())
fmt.Println(formatRow(r.ID().String(), formatSize(r.FSContentSize()), r.FSContentSHA256(), ".", r.PermissionsString(), r.Publinks()))
fmt.Println(r.Path() + " " + r.FullPermissionsString())
fmt.Println(formatRow(r.ID().String(), formatSize(r.ContentSize()), r.ContentSHA256(), ".", r.PermissionsString(), r.Publinks()))
if r.FSDir() {
if r.Dir() {
children, err := r.ReadDir(false)
if err != nil {
fmt.Println("cannot access '" + pathOrUUID + "': " + err.Error())
@@ -37,12 +37,12 @@ func setupLsCommand() *cobra.Command {
sort.Slice(children, func(i, j int) bool {
a := children[i]
b := children[j]
if a.FSDir() != b.FSDir() {
if a.FSDir() {
if a.Dir() != b.Dir() {
if a.Dir() {
return true
}
}
return strings.Compare(strings.ToLower(a.FSName()), strings.ToLower((b.FSName()))) <= 0
return strings.Compare(strings.ToLower(a.Name()), strings.ToLower((b.Name()))) <= 0
})
for _, c := range children {
fmt.Println(formatResourceSummary(c.(fs.ResourceInfo)))
@@ -69,9 +69,9 @@ func formatRow(id, size, sha256, name, permissions string, links int) string {
}
func formatResourceSummary(r fs.ResourceInfo) string {
name := r.FSName()
if r.FSDir() {
name := r.Name()
if r.Dir() {
name += "/"
}
return formatRow(r.ID.String(), formatSize(r.FSContentSize()), r.FSContentSHA256(), name, r.PermissionsString(), r.Publinks)
return formatRow(r.ID().String(), formatSize(r.ContentSize()), r.ContentSHA256(), name, r.PermissionsString(), r.Publinks())
}

View File

@@ -22,7 +22,7 @@ func setupRmCommand() *cobra.Command {
os.Exit(1)
}
if r.FSDir() {
if r.Dir() {
if recursive, err := cmd.Flags().GetBool("recursive"); err != nil || !recursive {
fmt.Println("cannot remove '" + pathOrUUID + "': Is a directory. Try again with '-r'")
os.Exit(1)

View File

@@ -37,7 +37,7 @@ func setupBookmarksListCommand() *cobra.Command {
os.Exit(1)
} else {
for _, b := range bookmarks {
fmt.Printf("%s %s %s\n", b.ID.String(), b.Name, b.PermissionsString())
fmt.Printf("%s %s %s\n", b.ID().String(), b.Name(), b.PermissionsString())
}
}
},

View File

@@ -61,8 +61,8 @@ func (r Resource) Move(target string, overwrite bool) (Resource, bool, error) {
}
return err
} else {
r.Ancestry[0].Name = nameParent.Name
r.Ancestry[0].ParentID = nameParent.Parent
r.Ancestry[0].name = nameParent.Name
r.Ancestry[0].parentID = nameParent.Parent
return nil
}
})
@@ -82,7 +82,7 @@ func (r Resource) Copy(target string, id uuid.UUID, recursive, overwrite bool) (
}
return Resource{}, false, err
}
if !destParent.FSDir() {
if !destParent.Dir() {
return Resource{}, false, ErrResourceNotCollection
}
@@ -91,7 +91,7 @@ func (r Resource) Copy(target string, id uuid.UUID, recursive, overwrite bool) (
}
var tree []db.PublinkedResource
if recursive && r.FSDir() {
if recursive && r.Dir() {
var err error
tree, err = r.f.db.ReadDir(r.f.ctx, db.ReadDirParams{ResourceID: r.ID(), MinDepth: 1, MaxDepth: 1000})
if err != nil {
@@ -101,7 +101,7 @@ func (r Resource) Copy(target string, id uuid.UUID, recursive, overwrite bool) (
create := make([]db.CreateResourcesParams, 0, len(tree)+1)
contents := make(map[uuid.UUID]uuid.UUID)
ids := make(map[uuid.UUID]uuid.UUID)
if r.FSDir() {
if r.Dir() {
ids[r.ID()] = id
} else {
contents[r.ID()] = id
@@ -111,10 +111,10 @@ func (r Resource) Copy(target string, id uuid.UUID, recursive, overwrite bool) (
ID: id,
Parent: destParent.ID(),
Name: destName,
Dir: r.FSDir(),
ContentSize: r.FSContentSize(),
ContentType: r.FSContentType(),
ContentSha256: r.FSContentSHA256(),
Dir: r.Dir(),
ContentSize: r.ContentSize(),
ContentType: r.ContentType(),
ContentSha256: r.ContentSHA256(),
})
for _, src := range tree {
id, _ := uuid.NewUUID()
@@ -169,23 +169,23 @@ func (r Resource) Copy(target string, id uuid.UUID, recursive, overwrite bool) (
parentID := destParent.ID()
var info = ResourceInfo{
ID: id,
ParentID: &parentID,
Name: destName,
Dir: r.FSDir(),
Created: time.Now(),
Modified: time.Now(),
Deleted: nil,
ContentSize: r.FSContentSize(),
ContentType: r.FSContentType(),
ContentSHA256: r.FSContentSHA256(),
Permissions: "{}",
Publinks: 0,
id: id,
parentID: &parentID,
name: destName,
dir: r.Dir(),
created: time.Now(),
modified: time.Now(),
deleted: nil,
contentSize: r.ContentSize(),
contentType: r.ContentType(),
contentSHA256: r.ContentSHA256(),
permissions: "{}",
publinks: 0,
}
if destParent.FSPath() == "/" {
info.Path = "/" + info.Name
if destParent.Path() == "/" {
info.path = "/" + info.name
} else {
info.Path = destParent.FSPath() + "/" + info.Name
info.path = destParent.Path() + "/" + info.name
}
ancestry := make([]ResourceInfo, len(destParent.Ancestry)+1)
@@ -248,7 +248,7 @@ func (f filesystem) targetByPathOrUUID(src Resource, pathOrUUID string) (Resourc
name := pathOrUUID[index+1:]
if name == "" {
name = src.FSName()
name = src.Name()
} else if CheckNameInvalid(name) {
return Resource{}, "", ErrResourceNameInvalid
}

View File

@@ -32,7 +32,7 @@ func (f filesystem) CreateResourceByPath(path string, dir, recursive bool) (Reso
}
func (r Resource) CreateMemberResource(name string, id uuid.UUID, dir bool) (Resource, error) {
if !r.FSDir() {
if !r.Dir() {
return Resource{}, ErrResourceNotCollection
}
if !r.hasPermission(PermissionWrite) {
@@ -62,7 +62,7 @@ func (r Resource) CreateMemberResource(name string, id uuid.UUID, dir bool) (Res
if err != nil {
return Resource{}, err
}
if !r.FSDir() {
if !r.Dir() {
out, err := r.f.cs.OpenWrite(id, nil, nil)
if err == nil {
err = out.Close()
@@ -88,11 +88,11 @@ func (r Resource) CreateMemberResource(name string, id uuid.UUID, dir bool) (Res
// Path: path,
// UserPermission: r.UserPermission,
// }, nil
info := resourceInfoFromDBResource(result)
if r.FSPath() == "/" {
info.Path = "/" + info.Name
info := ResourceInfoFromDBResource(result)
if r.Path() == "/" {
info.path = "/" + info.name
} else {
info.Path = r.FSPath() + "/" + info.Name
info.path = r.Path() + "/" + info.name
}
ancestry := make([]ResourceInfo, len(r.Ancestry)+1)
copy(ancestry, r.Ancestry)
@@ -104,22 +104,22 @@ func (r Resource) CreateMemberResource(name string, id uuid.UUID, dir bool) (Res
}, nil
}
func resourceInfoFromDBResource(r db.Resource) ResourceInfo {
func ResourceInfoFromDBResource(r db.Resource) ResourceInfo {
var delTime *time.Time
if r.Deleted.Valid {
delTime = &r.Deleted.Time
}
return ResourceInfo{
ID: r.ID,
ParentID: r.Parent,
Name: r.Name,
Dir: r.Dir,
Created: r.Created.Time,
Modified: r.Modified.Time,
Deleted: delTime,
ContentSize: r.ContentSize,
ContentType: r.ContentType,
ContentSHA256: r.ContentSha256,
Permissions: string(r.Permissions),
id: r.ID,
parentID: r.Parent,
name: r.Name,
dir: r.Dir,
created: r.Created.Time,
modified: r.Modified.Time,
deleted: delTime,
contentSize: r.ContentSize,
contentType: r.ContentType,
contentSHA256: r.ContentSha256,
permissions: string(r.Permissions),
}
}

View File

@@ -35,9 +35,9 @@ func (f filesystem) resourceFromResult(e []db.PublinkedResource, err error) (Res
}
info := resourceInfoFromDBPublinkedResource(e[i])
if b.Len() == 0 {
info.Path = "/"
info.path = "/"
} else {
info.Path = b.String()
info.path = b.String()
}
ancestry[len(e)-i-1] = info
@@ -81,18 +81,18 @@ func resourceInfoFromDBPublinkedResource(r db.PublinkedResource) ResourceInfo {
delTime = &r.Deleted.Time
}
return ResourceInfo{
ID: r.ID,
ParentID: r.Parent,
Name: r.Name,
Dir: r.Dir,
Created: r.Created.Time,
Modified: r.Modified.Time,
Deleted: delTime,
ContentSize: r.ContentSize,
ContentType: r.ContentType,
ContentSHA256: r.ContentSha256,
Permissions: string(r.Permissions),
Publinks: int(r.Publinks),
id: r.ID,
parentID: r.Parent,
name: r.Name,
dir: r.Dir,
created: r.Created.Time,
modified: r.Modified.Time,
deleted: delTime,
contentSize: r.ContentSize,
contentType: r.ContentType,
contentSHA256: r.ContentSha256,
permissions: string(r.Permissions),
publinks: int(r.Publinks),
}
}

View File

@@ -34,7 +34,7 @@ func (r Resource) ReadDir(recursive bool) ([]serve.ResourceInfo, error) {
if !r.hasPermission(PermissionRead) {
return nil, ErrInsufficientPermissions
}
if !r.FSDir() {
if !r.Dir() {
return nil, ErrResourceNotCollection
}
maxDepth := 1
@@ -52,14 +52,14 @@ func (r Resource) ReadDir(recursive bool) ([]serve.ResourceInfo, error) {
result := make([]serve.ResourceInfo, len(children))
path := make(map[uuid.UUID]string)
path[r.ID()] = r.FSPath()
if r.FSPath() == "/" {
path[r.ID()] = r.Path()
if r.Path() == "/" {
path[r.ID()] = ""
}
for i, c := range children {
info := resourceInfoFromDBPublinkedResource(c)
info.Path = path[*info.ParentID] + "/" + info.Name
path[info.ID] = info.Path
info.path = path[*info.parentID] + "/" + info.name
path[info.id] = info.path
result[i] = info
}
return result, nil
@@ -70,7 +70,7 @@ func (r Resource) Walk(depth int, fn func(serve.ResourceInfo) error) error {
if err != nil {
return err
}
if !r.FSDir() || depth <= 0 {
if !r.Dir() || depth <= 0 {
return nil
}

View File

@@ -26,14 +26,14 @@ func (r Resource) PermissionsString() string {
}
func (r ResourceInfo) PermissionsString() string {
return permissionJsonString(r.Permissions)
return permissionJsonString(r.permissions)
}
func (r Resource) FullPermissionsString() string {
result := make(map[string]Permission)
for _, i := range r.Ancestry {
p := make(map[string]Permission)
json.Unmarshal([]byte(i.Permissions), &p)
json.Unmarshal([]byte(i.permissions), &p)
for k, v := range p {
result[k] = result[k] | v
}

View File

@@ -13,40 +13,44 @@ type Resource struct {
}
type ResourceInfo struct {
ID uuid.UUID `json:"id"`
ParentID *uuid.UUID `json:"parent"`
Name string `json:"name"`
Dir bool `json:"dir"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
Deleted *time.Time `json:"deleted"`
ContentSize int64 `json:"csize"`
ContentType string `json:"ctype"`
ContentSHA256 string `json:"csha256"`
Permissions string `json:"permissions"`
Publinks int `json:"publinks"`
Path string `json:"-"`
id uuid.UUID
parentID *uuid.UUID
name string
dir bool
created time.Time
modified time.Time
deleted *time.Time
contentSize int64
contentType string
contentSHA256 string
permissions string
publinks int
path string
}
func (r Resource) Info() ResourceInfo { return r.Ancestry[len(r.Ancestry)-1] }
func (r Resource) ID() uuid.UUID { return r.Info().ID }
func (r Resource) ParentID() *uuid.UUID { return r.Info().ParentID }
func (r Resource) FSName() string { return r.Info().Name }
func (r Resource) FSDir() bool { return r.Info().Dir }
func (r Resource) FSCreated() time.Time { return r.Info().Created }
func (r Resource) FSModified() time.Time { return r.Info().Modified }
func (r Resource) Deleted() *time.Time { return r.Info().Deleted }
func (r Resource) FSContentSize() int64 { return r.Info().ContentSize }
func (r Resource) FSContentSHA256() string { return r.Info().ContentSHA256 }
func (r Resource) FSContentType() string { return r.Info().ContentType }
func (r Resource) Publinks() int { return r.Info().Publinks }
func (r Resource) FSPath() string { return r.Info().Path }
func (r Resource) Info() ResourceInfo { return r.Ancestry[len(r.Ancestry)-1] }
func (r Resource) ID() uuid.UUID { return r.Info().id }
func (r Resource) ParentID() *uuid.UUID { return r.Info().parentID }
func (r Resource) Name() string { return r.Info().name }
func (r Resource) Dir() bool { return r.Info().dir }
func (r Resource) Created() time.Time { return r.Info().created }
func (r Resource) Modified() time.Time { return r.Info().modified }
func (r Resource) Deleted() *time.Time { return r.Info().deleted }
func (r Resource) ContentSize() int64 { return r.Info().contentSize }
func (r Resource) ContentSHA256() string { return r.Info().contentSHA256 }
func (r Resource) ContentType() string { return r.Info().contentType }
func (r Resource) Publinks() int { return r.Info().publinks }
func (r Resource) Path() string { return r.Info().path }
func (r ResourceInfo) FSName() string { return r.Name }
func (r ResourceInfo) FSDir() bool { return r.Dir }
func (r ResourceInfo) FSCreated() time.Time { return r.Created }
func (r ResourceInfo) FSModified() time.Time { return r.Modified }
func (r ResourceInfo) FSContentSize() int64 { return r.ContentSize }
func (r ResourceInfo) FSContentSHA256() string { return r.ContentSHA256 }
func (r ResourceInfo) FSContentType() string { return r.ContentType }
func (r ResourceInfo) FSPath() string { return r.Path }
func (r ResourceInfo) ID() uuid.UUID { return r.id }
func (r ResourceInfo) ParentID() *uuid.UUID { return r.parentID }
func (r ResourceInfo) Name() string { return r.name }
func (r ResourceInfo) Dir() bool { return r.dir }
func (r ResourceInfo) Created() time.Time { return r.created }
func (r ResourceInfo) Modified() time.Time { return r.modified }
func (r ResourceInfo) Deleted() *time.Time { return r.deleted }
func (r ResourceInfo) ContentSize() int64 { return r.contentSize }
func (r ResourceInfo) ContentSHA256() string { return r.contentSHA256 }
func (r ResourceInfo) ContentType() string { return r.contentType }
func (r ResourceInfo) Publinks() int { return r.publinks }
func (r ResourceInfo) Path() string { return r.path }

View File

@@ -28,6 +28,6 @@ func (r Resource) UpdatePermissions(username string, permission Permission) (Res
if err != nil {
return Resource{}, err
}
r.Ancestry[0].Permissions = string(p)
r.Ancestry[0].permissions = string(p)
return r, nil
}

View File

@@ -23,15 +23,15 @@ type Resource struct {
contentSHA256 string
}
func (r Resource) FSID() uuid.UUID { return r.id }
func (r Resource) FSName() string { return r.name }
func (r Resource) FSPath() string { return r.path }
func (r Resource) FSDir() bool { return r.dir }
func (r Resource) FSCreated() time.Time { return r.created }
func (r Resource) FSModified() time.Time { return r.modified }
func (r Resource) FSContentSize() int64 { return r.contentSize }
func (r Resource) FSContentSHA256() string { return r.contentSHA256 }
func (r Resource) FSContentType() string { return r.contentType }
func (r Resource) FSID() uuid.UUID { return r.id }
func (r Resource) Name() string { return r.name }
func (r Resource) Path() string { return r.path }
func (r Resource) Dir() bool { return r.dir }
func (r Resource) Created() time.Time { return r.created }
func (r Resource) Modified() time.Time { return r.modified }
func (r Resource) ContentSize() int64 { return r.contentSize }
func (r Resource) ContentSHA256() string { return r.contentSHA256 }
func (r Resource) ContentType() string { return r.contentType }
func (r Resource) OpenRead(start, length int64) (io.ReadCloser, error) {
return r.f.cs.OpenRead(r.id, start, length)

View File

@@ -1,8 +1,6 @@
package user
import (
"time"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/core/db"
"github.com/shroff/phylum/server/internal/core/fs"
@@ -17,7 +15,7 @@ func (m manager) SharedResources(u User) ([]fs.ResourceInfo, error) {
result := make([]fs.ResourceInfo, len(res))
for i, r := range res {
result[i] = resourceFromDB(r)
result[i] = fs.ResourceInfoFromDBResource(r)
}
return result, err
}
@@ -39,33 +37,7 @@ func (m manager) Bookmarks(u User) ([]fs.ResourceInfo, error) {
result := make([]fs.ResourceInfo, len(res))
for i, r := range res {
result[i] = resourceFromDB(r)
result[i] = fs.ResourceInfoFromDBResource(r)
}
return result, err
}
func resourceFromDB(r db.Resource) fs.ResourceInfo {
var deleted *time.Time
if r.Deleted.Valid {
deleted = &r.Deleted.Time
}
return fs.ResourceInfo{
ID: r.ID,
ParentID: r.Parent,
Name: r.Name,
Created: r.Created.Time,
Modified: r.Modified.Time,
Deleted: deleted,
Dir: r.Dir,
ContentSize: r.ContentSize,
ContentType: r.ContentType,
ContentSHA256: r.ContentSha256,
Permissions: string(r.Permissions),
// Not Needed
// f: f,
// Path: "",
// UserPermissions: 0,
// InheritedPermissions: "",
// Publinks: 0,
}
}