def callbackReal(*args, **kwargs): try: passed, hint = self._firewall(request) if not passed: return Template(filename=h.makePath( h.ROOT_FOLDER, "templates", "error.mako"), lookup=self.tplLookup).render( e=hint, **self._makeBaseNamspace(), le=None, lt=None) r = callback(*args, **kwargs) if noCache: r = self._noCache(r) return r except Exception as e: le, lt = h.getLastExceptionAndTrace() print(le) print(lt) h.logWarning("Error processing request", request.data.decode("utf-8"), request.endpoint, h.formatException(e)) return Template(filename=h.makePath(h.ROOT_FOLDER, "templates", "error.mako"), lookup=self.tplLookup).render( e=e, **self._makeBaseNamspace(), le=le, lt=lt)
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)
def _saveTrackings(self): if self.trackingsSaved: return try: self.trackingsLock.acquire() trackingFile = h.makePath(self.basePath, ".tracking") tmpTrackingFile = h.makePath(self.basePath, ".tracking.tmp") h.logDebug("Saving trackings saved to file", len(self.trackings), trackingFile) headers, datas = [ "path", "authorized", "password", "ip", "date", "protected", "location" ], [] for t in self.trackings: datas.append([ t.path, t.authorized, t.password, t.ip, t.date, t.protected, t.location ]) h.writeToCSV(datas, tmpTrackingFile, headers=headers, append=True) h.delete(trackingFile) os.rename(tmpTrackingFile, trackingFile) if self.user is not None: h.changeFileOwner(trackingFile, self.user) self.trackingsSaved = True h.logDebug("Trackings saved to file", len(self.trackings), trackingFile) except: le, lt = h.getLastExceptionAndTrace() h.logWarning("Can't save trackings", le) finally: self.trackingsLock.release()
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)
def run(self): while not self._interrupt and not self._exitEvent.isSet(): try: self.tp._saveTrackings() except: print(h.getLastExceptionAndTrace()) finally: self._exitEvent.wait(self.frequency) h.logDebug("SaveTracking thread finished")
def run(self): while not self._interrupt and not self._exitEvent.isSet(): try: items = h.listDirectoryItems(self.tmpFolder) for item in items: if h.getFileModified(item) + self.tmpDuration < h.now(): h.delete(item) h.logInfo("Item too old deleted %s" % item) except: print(h.getLastExceptionAndTrace()) finally: self._exitEvent.wait(500) h.logDebug("CleanTmp thread finished")
def addLeaf(self, path, file): path = h.cleanPath(path) try: if not self.doesItemExists(path): return False, "Container does not exists" filename = self.getPotentialLeafName(file) if self.isItemLeaf(h.makePath(path, filename)): return False, "Item already exists" if self.isItemContainer(h.makePath(path, filename)): return False, "Item already exists" filePath = h.makePath(self.basePath, path, filename) file.save(filePath) h.touchFile(filePath) return True, filename except: le, lt = h.getLastExceptionAndTrace() return False, le
def _loadTrackings(self): try: self.trackingsLock.acquire() trackingFile = h.makePath(self.basePath, ".tracking") datas = h.readCSV(trackingFile) for d in datas: self.trackings.append( tracking(d[0], d[2], h.parseBool(d[1], False, trueValue="True"), d[3], h.parseInt(d[4], 0), h.parseBool(d[5], False, trueValue="True"), d[6])) h.logDebug("Trackings loaded from file", trackingFile, len(self.trackings)) except: le, lt = h.getLastExceptionAndTrace() h.logWarning("Can't load trackings", le) finally: self.trackingsLock.release()
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)
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)