def upload(request): res = Result() uploadfile = request.FILES.get("file") if uploadfile: filename = uploadfile.name path = request.POST.get("path", None) if path: foreignPath = path.replace("'", '"') else: foreignPath = filename galleries = request.POST.get("galleries", "1").split(",") tags = [ _.strip() for _ in request.POST.get("tags", "").split(",") if _ ] title = request.POST.get("title") description = request.POST.get("description", "") force = request.POST.get("force") try: username = request.POST.get("user", False) if username: user = User.objects.get(username=username) else: user = request.user uniqueName = request.POST.get("uid", Piece.getUniqueID(foreignPath, user)) if galleries and Gallery.objects.filter( pk__in=[int(g) for g in galleries], uploads=False): raise PermissionDenied() extension = Path(filename).ext.lower() if extension in FILE_TYPES["image"]: model = Image elif extension in FILE_TYPES["video"]: model = Video elif extension in FILE_TYPES["marmoset"]: model = Marmoset else: raise MediaTypeError( "{} is not a supported file type".format(extension)) obj, created = model.objects.get_or_create(unique_id=uniqueName, defaults={ "author": user, "hidden": False }) guid = obj.getGuid() hashVal = getHashForFile(uploadfile) if hashVal == obj.hash and not force: for gal in galleries: g = Gallery.objects.get(pk=int(gal)) obj.gallery_set.add(g) res.append(obj.json()) res.message = "Files were the same" return JsonResponse(res.asDict()) objPath = getRoot() / guid.guid[-2:] / guid.guid / filename hashPath = objPath.parent / hashVal + objPath.ext if not objPath.parent.exists(): objPath.parent.makedirs() # Save uploaded files to asset folder for key, uploadfile in request.FILES.items(): if key == "file": handle_uploaded_file(hashPath, uploadfile) else: dest = objPath.parent / uploadfile.name handle_uploaded_file(dest, uploadfile) if key == "thumbnail": thumbnail = saveAsPng(dest) # Resize image = pilImage.open(thumbnail) width, height = squareCropDimensions(*image.size) image.thumbnail((width, height), pilImage.ANTIALIAS) # Crop from center box = cropBox(*image.size) image.crop(box).save(thumbnail) obj.custom_thumbnail = obj.getPath( True) / thumbnail.name obj.save() obj.hash = hashVal obj.foreign_path = foreignPath obj.title = title or objPath.namebase obj.description = description obj.export(hashVal, hashPath, tags=tags, galleries=galleries) res.append(obj.json()) except MediaTypeError as err: res.isError = True res.message = str(err) return JsonResponse(res.asDict()) else: res.isError = True res.message = "No file found" return JsonResponse(res.asDict())
def handle(self, *args, **options): users = {} p = path.path(options['dirname']) if not MOD_FILE.exists(): MOD_FILE.write_text('{}') data = json.loads(MOD_FILE.text()) auth = auth_ldap.LDAPAuthBackend() for username in p.dirs(): username = username.namebase.lower() user = auth.get_or_create(username) users[username] = user for f in p.walkfiles(): username = f.relpath(p).split('/')[0].lower() user = users[username] uniqueid = models.Image.getUniqueID(f, user) created = False tags = [] galleries = [1] if f.ext.lower() in EXT['image']: model = models.Image elif f.ext.lower() in EXT['video']: model = models.Video else: #self.stderr.write('Filetype "{}" not supported'.format(f.ext)) continue obj, created = model.objects.get_or_create(unique_id=uniqueid, defaults={'author': user}) guid = obj.getGuid() with f.open('rb') as fh: hashVal = getHashForFile(fh) if hashVal == obj.hash: continue objPath = models.ROOT if models.FROG_PATH: objPath = objPath / models.FROG_PATH objPath = objPath / guid.guid[-2:] / guid.guid / f.name hashPath = objPath.parent / hashVal + objPath.ext if not objPath.parent.exists(): objPath.parent.makedirs() try: f.copy(hashPath) except: pass obj.hash = hashVal obj.foreign_path = f obj.title = objPath.namebase self.stdout.write('Exporting {}'.format(f)) obj.export(hashVal, hashPath, tags=tags, galleries=galleries) if created: self.stdout.write('Added {}'.format(f)) else: self.stdout.write('Updated {}'.format(f))
def handle(self, *args, **options): users = {} p = path.path(options['dirname']) if not MOD_FILE.exists(): MOD_FILE.write_text('{}') data = json.loads(MOD_FILE.text()) auth = auth_ldap.LDAPAuthBackend() for username in p.dirs(): username = username.namebase.lower() user = auth.get_or_create(username) users[username] = user for f in p.walkfiles(): username = f.relpath(p).split('/')[0].lower() user = users[username] uniqueid = models.Image.getUniqueID(f, user) created = False tags = [] galleries = [1] if f.ext.lower() in EXT['image']: model = models.Image elif f.ext.lower() in EXT['video']: model = models.Video else: #self.stderr.write('Filetype "{}" not supported'.format(f.ext)) continue obj, created = model.objects.get_or_create( unique_id=uniqueid, defaults={'author': user}) guid = obj.getGuid() with f.open('rb') as fh: hashVal = getHashForFile(fh) if hashVal == obj.hash: continue objPath = models.ROOT if models.FROG_PATH: objPath = objPath / models.FROG_PATH objPath = objPath / guid.guid[-2:] / guid.guid / f.name hashPath = objPath.parent / hashVal + objPath.ext if not objPath.parent.exists(): objPath.parent.makedirs() try: f.copy(hashPath) except: pass obj.hash = hashVal obj.foreign_path = f obj.title = objPath.namebase self.stdout.write('Exporting {}'.format(f)) obj.export(hashVal, hashPath, tags=tags, galleries=galleries) if created: self.stdout.write('Added {}'.format(f)) else: self.stdout.write('Updated {}'.format(f))
def upload(request): res = Result() uploadfile = request.FILES.get('file') if uploadfile: filename = uploadfile.name path = request.POST.get('path', None) if path: foreignPath = path.replace("'", "\"") else: foreignPath = filename galleries = request.POST.get('galleries', '1').split(',') tags = [_.strip() for _ in request.POST.get('tags', '').split(',') if _] title = request.POST.get('title') try: username = request.POST.get('user', False) if username: user = User.objects.get(username=username) else: user = request.user uniqueName = request.POST.get('uid', models.Piece.getUniqueID(foreignPath, user)) if galleries and models.Gallery.objects.filter(pk__in=[int(g) for g in galleries], uploads=False): raise PermissionDenied() extension = Path(filename).ext.lower() if extension in models.FILE_TYPES['image']: model = models.Image elif extension in models.FILE_TYPES['video']: model = models.Video else: raise MediaTypeError('{} is not a supported file type'.format(extension)) obj, created = model.objects.get_or_create(unique_id=uniqueName, defaults={'author': user}) guid = obj.getGuid() hashVal = getHashForFile(uploadfile) if hashVal == obj.hash: for gal in galleries: g = models.Gallery.objects.get(pk=int(gal)) obj.gallery_set.add(g) res.append(obj.json()) res.message = "Files were the same" return JsonResponse(res.asDict()) objPath = models.ROOT if models.FROG_PATH: objPath = objPath / models.FROG_PATH objPath = objPath / guid.guid[-2:] / guid.guid / filename hashPath = objPath.parent / hashVal + objPath.ext if not objPath.parent.exists(): objPath.parent.makedirs() handle_uploaded_file(hashPath, uploadfile) obj.hash = hashVal obj.foreign_path = foreignPath obj.title = title or objPath.namebase obj.export(hashVal, hashPath, tags=tags, galleries=galleries) res.append(obj.json()) for key, uploadfile in request.FILES.items(): if key != 'file': dest = objPath.parent / uploadfile.name handle_uploaded_file(dest, uploadfile) except MediaTypeError as err: res.isError = True res.message = str(err) return JsonResponse(res.asDict()) else: res.isError = True res.message = "No file found" return JsonResponse(res.asDict())
def upload(request): res = Result() uploadfile = request.FILES.get('file') if uploadfile: filename = uploadfile.name path = request.POST.get('path', None) if path: foreignPath = path.replace("'", "\"") else: foreignPath = filename galleries = request.POST.get('galleries', '1').split(',') tags = [ _.strip() for _ in request.POST.get('tags', '').split(',') if _ ] title = request.POST.get('title') try: username = request.POST.get('user', False) if username: user = User.objects.get(username=username) else: user = request.user uniqueName = request.POST.get( 'uid', models.Piece.getUniqueID(foreignPath, user)) if galleries and models.Gallery.objects.filter( pk__in=[int(g) for g in galleries], uploads=False): raise PermissionDenied() extension = Path(filename).ext.lower() if extension in models.FILE_TYPES['image']: model = models.Image elif extension in models.FILE_TYPES['video']: model = models.Video else: raise MediaTypeError( '{} is not a supported file type'.format(extension)) obj, created = model.objects.get_or_create( unique_id=uniqueName, defaults={'author': user}) guid = obj.getGuid() hashVal = getHashForFile(uploadfile) if hashVal == obj.hash: for gal in galleries: g = models.Gallery.objects.get(pk=int(gal)) obj.gallery_set.add(g) res.append(obj.json()) res.message = "Files were the same" return JsonResponse(res.asDict()) objPath = models.ROOT if models.FROG_PATH: objPath = objPath / models.FROG_PATH objPath = objPath / guid.guid[-2:] / guid.guid / filename hashPath = objPath.parent / hashVal + objPath.ext if not objPath.parent.exists(): objPath.parent.makedirs() handle_uploaded_file(hashPath, uploadfile) obj.hash = hashVal obj.foreign_path = foreignPath obj.title = title or objPath.namebase obj.export(hashVal, hashPath, tags=tags, galleries=galleries) res.append(obj.json()) for key, uploadfile in request.FILES.items(): if key != 'file': dest = objPath.parent / uploadfile.name handle_uploaded_file(dest, uploadfile) except MediaTypeError as err: res.isError = True res.message = str(err) return JsonResponse(res.asDict()) else: res.isError = True res.message = "No file found" return JsonResponse(res.asDict())