|
@@ -8,7 +8,9 @@ import (
|
|
"strings"
|
|
"strings"
|
|
"sync"
|
|
"sync"
|
|
|
|
|
|
|
|
+ uuid "github.com/satori/go.uuid"
|
|
"imuslab.com/arozos/mod/database"
|
|
"imuslab.com/arozos/mod/database"
|
|
|
|
+ "imuslab.com/arozos/mod/filesystem"
|
|
fs "imuslab.com/arozos/mod/filesystem"
|
|
fs "imuslab.com/arozos/mod/filesystem"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -62,8 +64,49 @@ func NewShareEntryTable(db *database.Database) *ShareEntryTable {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (s *ShareEntryTable) CreateNewShare(rpath string, username string, usergroups []string) (*ShareOption, error) {
|
|
|
|
+ rpath = filepath.ToSlash(filepath.Clean(rpath))
|
|
|
|
+ //Check if source file exists
|
|
|
|
+ if !fs.FileExists(rpath) {
|
|
|
|
+ return nil, errors.New("Unable to find the file on disk")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Check if the share already exists. If yes, use the previous link
|
|
|
|
+ val, ok := s.FileToUrlMap.Load(rpath)
|
|
|
|
+ if ok {
|
|
|
|
+ //Exists. Send back the old share url
|
|
|
|
+ ShareOption := val.(*ShareOption)
|
|
|
|
+ return ShareOption, nil
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ //Create new link for this file
|
|
|
|
+ shareUUID := uuid.NewV4().String()
|
|
|
|
+
|
|
|
|
+ //Create a share object
|
|
|
|
+ shareOption := ShareOption{
|
|
|
|
+ UUID: shareUUID,
|
|
|
|
+ FileRealPath: rpath,
|
|
|
|
+ Owner: username,
|
|
|
|
+ Accessibles: usergroups,
|
|
|
|
+ Permission: "anyone",
|
|
|
|
+ AllowLivePreview: true,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Store results on two map to make sure O(1) Lookup time
|
|
|
|
+ s.FileToUrlMap.Store(rpath, &shareOption)
|
|
|
|
+ s.UrlToFileMap.Store(shareUUID, &shareOption)
|
|
|
|
+
|
|
|
|
+ //Write object to database
|
|
|
|
+ s.Database.Write("share", shareUUID, shareOption)
|
|
|
|
+
|
|
|
|
+ return s, nil
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
//Delete the share on this vpath
|
|
//Delete the share on this vpath
|
|
func (s *ShareEntryTable) DeleteShare(rpath string) error {
|
|
func (s *ShareEntryTable) DeleteShare(rpath string) error {
|
|
|
|
+ rpath = filepath.ToSlash(filepath.Clean(rpath))
|
|
|
|
+
|
|
//Check if the share already exists. If yes, use the previous link
|
|
//Check if the share already exists. If yes, use the previous link
|
|
val, ok := s.FileToUrlMap.Load(rpath)
|
|
val, ok := s.FileToUrlMap.Load(rpath)
|
|
if ok {
|
|
if ok {
|
|
@@ -160,6 +203,33 @@ func (s *ShareEntryTable) RemoveShareByUUID(uuid string) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (s *ShareEntryTable) ResolveShareOptionFromVpath(vpath string) (*ShareOption, error) {
|
|
|
|
+ vrootID, subpath, err := filesystem.GetIDFromVirtualPath(vpath)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, errors.New("Unable to resolve virtual path")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if vrootID != "share" {
|
|
|
|
+ return nil, errors.New("Given path is not share vroot path")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return s.ResolveShareOptionFromShareSubpath(subpath)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (s *ShareEntryTable) ResolveShareOptionFromShareSubpath(subpath string) (*ShareOption, error) {
|
|
|
|
+ subpathElements := strings.Split(filepath.ToSlash(filepath.Clean(subpath))[1:], "/")
|
|
|
|
+ if len(subpathElements) >= 1 {
|
|
|
|
+ shareObject := s.GetShareObjectFromUUID(subpathElements[0])
|
|
|
|
+ if shareObject == nil {
|
|
|
|
+ return nil, errors.New("Invalid subpath")
|
|
|
|
+ } else {
|
|
|
|
+ return shareObject, nil
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ return nil, errors.New("Invalid subpath")
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
func (s *ShareEntryTable) ResolveShareVrootPath(subpath string, username string, usergroup []string) (string, error) {
|
|
func (s *ShareEntryTable) ResolveShareVrootPath(subpath string, username string, usergroup []string) (string, error) {
|
|
//Get a list of accessible files from this user
|
|
//Get a list of accessible files from this user
|
|
subpathElements := strings.Split(filepath.ToSlash(filepath.Clean(subpath))[1:], "/")
|
|
subpathElements := strings.Split(filepath.ToSlash(filepath.Clean(subpath))[1:], "/")
|
|
@@ -185,7 +255,6 @@ func (s *ShareEntryTable) ResolveShareVrootPath(subpath string, username string,
|
|
shareObject := s.GetShareObjectFromUUID(subpathElements[0])
|
|
shareObject := s.GetShareObjectFromUUID(subpathElements[0])
|
|
folderSubpaths := append([]string{shareObject.FileRealPath}, subpathElements[2:]...)
|
|
folderSubpaths := append([]string{shareObject.FileRealPath}, subpathElements[2:]...)
|
|
targetFolder := filepath.Join(folderSubpaths...)
|
|
targetFolder := filepath.Join(folderSubpaths...)
|
|
- log.Println("Loading folder:", targetFolder)
|
|
|
|
return targetFolder, nil
|
|
return targetFolder, nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -208,22 +277,8 @@ func (s *ShareEntryTable) listRootForUser(username string, usergroup []string) [
|
|
fileRealpath := fp.(string)
|
|
fileRealpath := fp.(string)
|
|
thisShareOption := so.(*ShareOption)
|
|
thisShareOption := so.(*ShareOption)
|
|
if fs.FileExists(fileRealpath) {
|
|
if fs.FileExists(fileRealpath) {
|
|
- //File exists. Check if the share options allow this user access (Only signed in user has username)
|
|
|
|
- if thisShareOption.Permission == "anyone" || thisShareOption.Permission == "signedin" {
|
|
|
|
|
|
+ if thisShareOption.IsAccessibleBy(username, usergroup) {
|
|
userAccessiableShare = append(userAccessiableShare, thisShareOption)
|
|
userAccessiableShare = append(userAccessiableShare, thisShareOption)
|
|
- } else if thisShareOption.Permission == "samegroup" || thisShareOption.Permission == "groups" {
|
|
|
|
- for _, thisUserGroup := range usergroup {
|
|
|
|
- if stringInSlice(thisUserGroup, thisShareOption.Accessibles) {
|
|
|
|
- //User's group is in the allowed group
|
|
|
|
- userAccessiableShare = append(userAccessiableShare, thisShareOption)
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- } else if thisShareOption.Permission == "users" {
|
|
|
|
- if stringInSlice(username, thisShareOption.Accessibles) {
|
|
|
|
- //User's group is in the allowed group
|
|
|
|
- userAccessiableShare = append(userAccessiableShare, thisShareOption)
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
return true
|
|
@@ -233,7 +288,10 @@ func (s *ShareEntryTable) listRootForUser(username string, usergroup []string) [
|
|
for _, thisShareObject := range userAccessiableShare {
|
|
for _, thisShareObject := range userAccessiableShare {
|
|
rpath := thisShareObject.FileRealPath
|
|
rpath := thisShareObject.FileRealPath
|
|
thisFile := fs.GetFileDataFromPath("share:/"+thisShareObject.UUID+"/"+filepath.Base(rpath), rpath, 2)
|
|
thisFile := fs.GetFileDataFromPath("share:/"+thisShareObject.UUID+"/"+filepath.Base(rpath), rpath, 2)
|
|
- thisFile.IsShared = true
|
|
|
|
|
|
+ if thisShareObject.Owner == username {
|
|
|
|
+ thisFile.IsShared = true
|
|
|
|
+ }
|
|
|
|
+
|
|
results = append(results, thisFile)
|
|
results = append(results, thisFile)
|
|
}
|
|
}
|
|
|
|
|