def from_upload(file, force_mime=False): stream = getattr(file, "stream", file) filename = getattr(file, "filename", None) sha256 = stream_sha256(stream) try: return AttachedFile.objects.get(sha256=sha256) except DoesNotExist: # First, make sure the storage dir exists try: os.makedirs(STORAGE_ROOT) except: pass fd = open(os.path.join(STORAGE_ROOT, sha256), 'wb') fd.write(stream.read()) fd.close() if filename: f = AttachedFile(filename=filename, content_type=force_mime or file.content_type, sha256=sha256) f.save() return f else: return None
def from_upload(file, force_mime=False): stream = getattr(file, "stream", file) filename = getattr(file, "filename", None) sha256 = stream_sha256(stream) return AttachedFile.get_or_create(sha256, stream, filename, force_mime or file.content_type)
def from_upload(file, force_mime=False): stream = getattr(file, "stream", file) filename = getattr(file, "filename", None) sha256 = stream_sha256(stream) return AttachedFile.get_or_create( sha256, stream, filename, force_mime or file.content_type)
def save_file(uploaded_file, filename=None): value = "FILE:{}".format(stream_sha256(uploaded_file)) mime_type = magic.from_buffer(uploaded_file.read(100), mime=True) uploaded_file.seek(0) body = AttachedFile.from_upload(uploaded_file, force_mime=mime_type) f = observables.File.get_or_create(value=value, body=body, mime_type=mime_type) if not filename: filename = uploaded_file.filename if filename not in f.filenames: f.filenames.append(filename) return f.save()
def save_file(uploaded_file, filename=None): value = "FILE:{}".format(stream_sha256(uploaded_file)) mime_type = magic.from_buffer(uploaded_file.read(100), mime=True) uploaded_file.seek(0) body = AttachedFile.from_upload(uploaded_file, force_mime=mime_type) f = observables.File.get_or_create( value=value, body=body, mime_type=mime_type) if not filename: filename = uploaded_file.filename if filename not in f.filenames: f.filenames.append(filename) return f.save()
def from_upload(file): sha256 = stream_sha256(file.stream) try: return AttachedFile.objects.get(sha256=sha256) except DoesNotExist: # First, make sure the storage dir exists try: os.makedirs(STORAGE_ROOT) except: pass fd = open(os.path.join(STORAGE_ROOT, sha256), 'wb') fd.write(file.stream.read()) fd.close() print repr(file.filename), bool(file.filename) if file.filename: f = AttachedFile(filename=file.filename, content_type=file.content_type, sha256=sha256) f.save() return f else: return None
def from_content(content, filename, content_type): sha256 = stream_sha256(content) return AttachedFile.get_or_create(sha256, content, filename, content_type)
def from_content(content, filename, content_type): sha256 = stream_sha256(content) return AttachedFile.get_or_create( sha256, content, filename, content_type)