def test_clean_listy_filepath(): expected = [u'dir1', u'dir2', u'linooks.jpg'] assert storage.clean_listy_filepath(['dir1', 'dir2', 'linooks.jpg']) == expected expected = [u'dir1', u'foo_.._nasty', u'linooks.jpg'] assert storage.clean_listy_filepath( ['/dir1/', 'foo/../nasty', 'linooks.jpg']) == expected expected = [u'etc', u'passwd'] assert storage.clean_listy_filepath(['../../../etc/', 'passwd']) == expected assert_raises(storage.InvalidFilepath, storage.clean_listy_filepath, ['../../', 'linooks.jpg'])
def test_clean_listy_filepath(): expected = [u'dir1', u'dir2', u'linooks.jpg'] assert storage.clean_listy_filepath( ['dir1', 'dir2', 'linooks.jpg']) == expected expected = [u'dir1', u'foo_.._nasty', u'linooks.jpg'] assert storage.clean_listy_filepath( ['/dir1/', 'foo/../nasty', 'linooks.jpg']) == expected expected = [u'etc', u'passwd'] assert storage.clean_listy_filepath( ['../../../etc/', 'passwd']) == expected with pytest.raises(storage.InvalidFilepath): storage.clean_listy_filepath(['../../', 'linooks.jpg'])
def file_url(self, filepath): if not self.base_url: raise NoWebServing( "base_url not set, cannot provide file urls") return urlparse.urljoin( self.base_url, '/'.join(clean_listy_filepath(filepath)))
def monkey_create_pub_filepath(entry, filename): if _is_cachefile(filename): filepath = clean_listy_filepath(entry.queued_media_file) else: filepath = list(entry.queued_media_file) filepath[-1] = filename return filepath
def handle(self): #Photo.objects.all().delete() os.chdir(self.base_dir) for top, dirs, files in os.walk(u'.'): # Skip hidden folders if '/.' in top: continue # Skip cache folders if '_cache' in top: print "cache skip", top continue if top == ".": top = "" #folder, new_folder = Folder.objects.select_related("photos") \ # .get_or_create(path=os.path.normpath(top) + "/", # defaults={'name': os.path.basename(top)}) folder_path = os.path.normpath(top) try: cleaned_top = "/".join(clean_listy_filepath(folder_path.split("/"))) except Exception: cleaned_top = top new_folder = not os.path.exists(os.path.join("mg_cache", cleaned_top)) if not new_folder: print u"Skipping folder {0}".format(folder_path).encode("utf-8") continue new_files = list(set(os.path.splitext(i)[0] for i in files)) new_files.sort(reverse=True) for new_filename in new_files: file_url = os.path.join(folder_path, new_filename) # More than one file with the same name but different # extension? exts = [os.path.splitext(f)[1] for f in files if new_filename in f] assert len(exts) > 0, "Couldn't find file extension for %s" % file_url filepath = self.raw_alternative(file_url, exts) try: self.import_file(MockMedia( filename=filepath, stream=open(filepath, "r"))) except Exception as e: print u"file: {0} exception: {1}".format(f, e).encode('utf-8') continue
def mount(self, dirpath, backend): """ Mount a new backend under dirpath """ new_ent = clean_listy_filepath(dirpath) print "Mounting:", repr(new_ent) already, rem_1, table, rem_2 = self._resolve_to_backend(new_ent, True) print "===", repr(already), repr(rem_1), repr(rem_2), len(table) assert (len(rem_2) > 0) or (None not in table), \ "That path is already mounted" assert (len(rem_2) > 0) or (len(table) == 0), \ "A longer path is already mounted here" for part in rem_2: table[part] = {} table = table[part] table[None] = backend
def _resolve_filepath(self, filepath): return '/'.join( clean_listy_filepath(filepath))
def _resolve_filepath(self, filepath): """ Transform the given filepath into a local filesystem filepath. """ return os.path.join( self.base_dir, *clean_listy_filepath(filepath))
def _resolve_filepath(self, filepath): return '/'.join(clean_listy_filepath(filepath))