Ejemplo n.º 1
0
    def getShare(self, shareID, r=None, asAdmin=False):
        sharePath = h.makePath(self.sharesPath, shareID)
        if not os.path.exists(sharePath): return None, None
        lh, lf = None, h.makePath(h.LOCKS_FOLDER,
                                  "_sfl_share%s" % h.clean(shareID))
        try:
            lh = h.getLockShared(lf, 5)
            shareJson = h.loadJsonFile(sharePath)
            files = shareJson.get("files", None)
            if files is None:
                files = shareJson.get("file", None)
                if files is not None: files = [files]
            s = share(shareJson["ID"], shareJson["creation"], files,
                      shareJson.get("views", []), shareJson.get("password"),
                      shareJson.get("duration", 0))
            if not asAdmin and s.duration > 0 and s.duration + s.creation < h.now(
            ):
                rs, rh = None, "Share has expired"
            else:
                rs, rh = s, "ok"
            h.releaseLock(lh)
            lh = None
            if rs is None: return rs, rh
            if r is not None: rs.tag = h.getURLParams(r.url).get("t", None)

            return rs, rh
        except:
            le, lt = h.getLastExceptionAndTrace()
            return None, le
        finally:
            if lh is not None: h.releaseLock(lh)
Ejemplo n.º 2
0
 def addShare(self, shareID, paths, duration, password):
     paths = [path.lstrip("/").rstrip("/") for path in paths]
     sharePath = h.makePath(self.sharesPath, shareID)
     password = "" if password is None else password
     lh, lf = None, h.makePath(h.LOCKS_FOLDER,
                               "_sfl_share%s" % h.clean(shareID))
     try:
         lh = h.getLockExclusive(lf, 5)
         s = share(shareID, h.now(), paths, [], password, duration)
         h.writeJsonFile(
             sharePath, {
                 "ID": s.ID,
                 "files": s.files,
                 "creation": s.creation,
                 "views": s.views,
                 "duration": s.duration,
                 "password": s.password
             })
         if self.user is not None: h.changeFileOwner(sharePath, self.user)
         return s, "ok"
     except:
         le, lt = h.getLastExceptionAndTrace()
         return None, le
     finally:
         if lh is not None: h.releaseLock(lh)
Ejemplo n.º 3
0
    def getPasswords(self, path):
        path = h.cleanPath(path)
        passwordsFile = h.makePath(self.basePath, path, ".password")
        if not os.path.exists(passwordsFile): return set()
        passwordsCacheKey = h.makeKeyFromArguments(path)
        lh, lf = None, h.makePath(h.LOCKS_FOLDER,
                                  "_sfl_password_%s" % h.clean(path))
        try:
            self.passwordsLock.acquire()
            if passwordsCacheKey in self.passwordsCache:
                pc = self.passwordsCache[passwordsCacheKey]
                if h.getFileModified(passwordsFile) == pc["date"]:
                    return pc["passwords"]

            lh = h.getLockShared(lf, 5)
            passwords = set([
                p for p in h.readFromFile(
                    h.makePath(self.basePath, path, ".password")).split("\n")
                if p != ""
            ])
            self.passwordsCache[passwordsCacheKey] = {
                "passwords": passwords,
                "date": h.getFileModified(passwordsFile)
            }
            return passwords
        finally:
            self.passwordsLock.release()
            if lh is not None: h.releaseLock(lh)
Ejemplo n.º 4
0
 def removeShare(self, shareID):
     sharePath = h.makePath(self.sharesPath, shareID)
     if not os.path.exists(sharePath):
         raise Exception("Unknown share", shareID)
     lh, lf = None, h.makePath(h.LOCKS_FOLDER,
                               "_sfl_share%s" % h.clean(shareID))
     try:
         lh = h.getLockExclusive(lf, 5)
         os.remove(sharePath)
         return True
     finally:
         if lh is not None: h.releaseLock(lh)
Ejemplo n.º 5
0
 def addNewPassword(self, path, password):
     path = h.cleanPath(path)
     if password is None: return False
     if self.passwordEditForbidden(path): return False
     lh, lf = None, h.makePath(h.LOCKS_FOLDER,
                               "_sfl_password_%s" % h.clean(path))
     try:
         passwordFile = h.makePath(self.basePath, path, ".password")
         requiredPasswords = list(self.getPasswords(path))
         requiredPasswords.append(password)
         lh = h.getLockExclusive(lf, 5)
         h.writeToFile(passwordFile,
                       "\n".join(list(set(requiredPasswords))))
         return True
     except:
         print(h.getLastExceptionAndTrace())
         return False
     finally:
         if lh is not None: h.releaseLock(lh)
Ejemplo n.º 6
0
 def saveShare(self, s: share):
     lh, lf = None, h.makePath(h.LOCKS_FOLDER,
                               "_sfl_share%s" % h.clean(s.ID))
     try:
         lh = h.getLockExclusive(lf, 5)
         sharePath = h.makePath(self.sharesPath, s.ID)
         h.writeJsonFile(
             sharePath, {
                 "ID": s.ID,
                 "files": s.files,
                 "creation": s.creation,
                 "views": s.views,
                 "duration": s.duration,
                 "password": s.password
             })
         if self.user is not None: h.changeFileOwner(sharePath, self.user)
         return True
     except:
         le, lt = h.getLastExceptionAndTrace()
         return False
     finally:
         if lh is not None: h.releaseLock(lh)