def check_availability(self): """ Perform check against Default Storage. """ try: name = default_storage.get_valid_name('Informer Storage') # Save data. content = ContentFile('File used by StorageInformer checking.') path = default_storage.save(name, content) # Check properties. default_storage.size(path) default_storage.url(path) default_storage.path(path) default_storage.get_accessed_time(path) default_storage.get_available_name(path) default_storage.get_created_time(path) default_storage.get_modified_time(path) default_storage.get_valid_name(path) # And remove file. default_storage.delete(path) storage = default_storage.__class__.__name__ except Exception as error: raise InformerException( f'An error occurred when trying to use your Storage: {error}') else: return True, f'Your {storage} is operational.'
def validate(self, attrs): if attrs.get('filename') is None: attrs['filename'] = attrs['file'].name attrs['full_path'] = os.path.join(attrs['path'], attrs['filename']) try: default_storage.get_available_name(attrs['full_path']) except SuspiciousFileOperation: raise ValidationError(_('forbidden path')) return attrs
def get_upload_filename(upload_name, user): user_path = _get_user_path(user) # Generate date based path to put uploaded file. # If CKEDITOR_RESTRICT_BY_DATE is True upload file to date specific path. if getattr(settings, 'CKEDITOR_RESTRICT_BY_DATE', True): date_path = datetime.now().strftime('%Y/%m/%d') else: date_path = '' # Complete upload path (upload_path + date_path). upload_path = os.path.join(settings.CKEDITOR_UPLOAD_PATH, user_path, date_path) if (getattr(settings, 'CKEDITOR_UPLOAD_SLUGIFY_FILENAME', True) and not hasattr(settings, 'CKEDITOR_FILENAME_GENERATOR')): upload_name = utils.slugify_filename(upload_name) if hasattr(settings, 'CKEDITOR_FILENAME_GENERATOR'): generator = import_string(settings.CKEDITOR_FILENAME_GENERATOR) upload_name = generator(upload_name) return default_storage.get_available_name( os.path.join(upload_path, upload_name))
def key_name(request): """Generate the S3 key name from the filename""" name = request.POST.get('name') foia_id = request.POST.get('foia_id') # total name cannot be longer than 255, but we limit the base name to 100 # to give room for the directory and because that's plenty long max_len = 100 if len(name) > max_len: base, ext = os.path.splitext(name) if len(ext) > max_len: # if someone give us a large extension just cut part of it off name = name[:max_len] else: # otherwise truncate the base and put the extension back on name = base[:max_len - len(ext)] + ext attachment = OutboundAttachment( user=request.user, foia_id=foia_id, ) key = attachment.ffile.field.generate_filename( attachment.ffile.instance, name, ) key = default_storage.get_available_name(key) return JsonResponse({'key': key})
def get_upload_filename(upload_name, user): user_path = _get_user_path(user) # Generate date based path to put uploaded file. # If CKEDITOR_RESTRICT_BY_DATE is True upload file to date specific path. if getattr(settings, 'CKEDITOR_RESTRICT_BY_DATE', True): date_path = datetime.now().strftime('%Y/%m/%d') else: date_path = '' # Complete upload path (upload_path + date_path). upload_path = os.path.join( settings.CKEDITOR_UPLOAD_PATH, user_path, date_path ) if (getattr(settings, 'CKEDITOR_UPLOAD_SLUGIFY_FILENAME', True) and not hasattr(settings, 'CKEDITOR_FILENAME_GENERATOR')): upload_name = utils.slugify_filename(upload_name) if hasattr(settings, 'CKEDITOR_FILENAME_GENERATOR'): generator = import_string(settings.CKEDITOR_FILENAME_GENERATOR) upload_name = generator(upload_name) return default_storage.get_available_name( os.path.join(upload_path, upload_name) )
def save_photo_to_profile(source_image_url: str, user: User, stdout: TextIOBase) -> bool: """Fetch image from source URL, add to storage and assign to User object""" stdout.write('Fetching image; initial url: {0}'.format(source_image_url)) # image URLs in the CSV are given for UI wrappers; we need to # translate them to direct download links g_drive_view_prefix = 'https://drive.google.com/open?id=' direct_download_prefix = 'https://drive.google.com/uc?export=download&id=' target_download_url = source_image_url.replace(g_drive_view_prefix, direct_download_prefix) # download image and save it to temporary in-memory file request = requests.get(target_download_url) if request.status_code != requests.codes.ok: stdout.write( 'Error fetching image from {0}'.format(target_download_url)) return False content_file = ContentFile(request.content) # get source file name and prepend it with a unique string to avoid duplication content_disposition_header = request.headers['content-disposition'] source_file_name = re.findall("filename=(.+)", content_disposition_header) target_file_name = '{0}-{1}'.format(uuid4(), source_file_name) # save file using configured storage (local file in dev, or S3 on staging/prod) user.photo.save(default_storage.get_available_name(target_file_name), content_file) stdout.write('Image saved successfully') return True
def ajax_upload_file(request): max_upload_size = settings.EMAIL_ATTACHMENT_MAX_UPLOAD_SIZE if request.method == "POST" and request.is_ajax( ) and request.user.is_expertA: upload_file = request.FILES.get('upload_file') error = None if upload_file: # BUG #2369 restriction of file content type does not work. (Removed from code now) if upload_file._size > max_upload_size: error = _( 'Please keep file size under {max_size}. Current file size {size}.' ).format({ 'max_size': filesizeformat(max_upload_size), 'size': filesizeformat(upload_file._size) }) else: error = _('No file.') context = {'error': error} if not error: upload_path = settings.EMAIL_ATTACHMENT_UPLOAD_PATH upload_filename = default_storage.get_available_name( join(upload_path, translify(upload_file.name))) saved_path = default_storage.save(upload_filename, upload_file) context = { 'saved_filename': basename(saved_path), 'original_filename': upload_file.name } return JSONResponse(context) raise PermissionDenied
def import_key(key, storage_bucket, comm, log, title=None): """Import a key""" foia = comm.foia file_name = os.path.split(key.name)[1] title = title or file_name access = 'private' if foia.embargo else 'public' foia_file = FOIAFile( comm=comm, title=title, datetime=comm.datetime, source=comm.get_source(), access=access, ) full_file_name = foia_file.ffile.field.generate_filename( foia_file.ffile.instance, file_name, ) full_file_name = default_storage.get_available_name(full_file_name) new_key = key.copy(storage_bucket, full_file_name) new_key.set_acl('public-read') foia_file.ffile.name = full_file_name foia_file.save() if key.size != foia_file.ffile.size: raise SizeError(key.size, foia_file.ffile.size, foia_file) log.append( 'SUCCESS: %s uploaded to FOIA Request %s with a status of %s' % (file_name, foia.pk, foia.status)) upload_document_cloud.apply_async(args=[foia_file.pk, False], countdown=3)
def get_user_avatar(strategy, details, response, uid=None, user=None, is_new=False, *args, **kwargs): image_url = None link = None if not user: return profile = user.person if strategy.backend.name == 'facebook': image_url = 'http://graph.facebook.com/%s/picture?type=large' % response.get( 'id') link = response.get('link') #profile.facebook_profile = link elif strategy.backend.name == 'vk-oauth2': image_url = response.get('user_photo') link = 'http://vk.com/%s' % response.get('screen_name') #profile.vk_profile = link elif strategy.backend.name == 'twitter': image_url = response.get('profile_image_url') if not 'default_profile' in image_url: image_url = image_url.replace('_normal', '_bigger') link = 'http://twitter.com/%s' % response.get('screen_name') #profile.twitter_profile = link profile.save() if image_url: social = kwargs.get('social') or strategy.storage.user.get_social_auth( strategy.backend.name, uid) if social: extra_data = strategy.backend.extra_data(user, uid, response, details) extra_data.update({ 'avatar': image_url, 'link': link, }) social.set_extra_data(extra_data) if image_url and not profile.avatar: try: image_content = urlopen(image_url) if strategy.backend.name == 'facebook' and 'image/gif' in str( image_content.info()): return image_name = default_storage.get_available_name( profile.avatar.field.upload_to( profile, make_upload_path_avatar(image_content.headers.subtype))) profile.avatar.save(image_name, ContentFile(image_content.read())) profile.save() except Exception: pass # Here we completely do not care about errors
def new_temporary(cls, **kw): """ Generate a file set for temporary use. Guaranteed to succeed. """ name = ",temporary" kw['name'] = os.path.basename(default_storage.get_available_name( os.path.join('catalog', name))) return cls(**kw)
def _get_file_path(self, filename): request = get_request() if not request: user = User.objects.get(pk=1) else: user = request.user path = os.path.join(MATERIAL_FILE_PATH, user.username, filename) path = default_storage.get_available_name(path) # dirname will not duplicate. return os.path.join(path, filename)
def join_files(o): left_hand = o.left_hand_data right_hand = o.right_hand_data left_hand_field = o.left_hand_field right_hand_field= o.right_hand_field if not right_hand_field: right_hand_field = left_hand_field resource_left = get_iterable_resource(left_hand.userfile.data_file.path, left_hand.data) resource_right = get_iterable_resource(right_hand.userfile.data_file.path, right_hand.data) fieldnames = resource_left.fieldnames + [x for x in resource_right.fieldnames if x not in resource_left.fieldnames] reader_left = resource_left.get_reader() reader_right = resource_right.get_reader() lookup_dict = {} for row in reader_right: #only first occurrence is kept if row[right_hand_field] not in lookup_dict: lookup_dict[str(row[right_hand_field])] = row out_rows = [] #scan all rows in the left and lookup on the dict, based on left_hand field for row in reader_left: lookup_value = str(row[left_hand_field]) if lookup_value in lookup_dict: joint_row = lookup_dict[lookup_value] row.update(joint_row.item) out_rows.append(row) stream = resource_left.get_stream() resource_left.write_to_stream(out_rows, fieldnames, stream) if o.result_file: o.result_file.delete() new_name = resource_left.get_new_filename() path = default_storage.get_available_name(new_name) out_file = UserFile(user=left_hand.userfile.user) out_file.data_file.save( path, File(stream) ) out_file.save() #saving result file o.result_file = out_file o.save() return out_file.data_file.path
def determine_name(request): if not request.POST: return HttpResponseBadRequest() desired_path = os.path.join(request.POST['upload_to'], request.POST['filename']) path = default_storage.get_available_name(desired_path) data = {'targetpath':path, 'targetname':os.path.split(path)[-1],} backend = get_uploadify_backend() backend(request=request, uploadify_options={'folder':request.POST['upload_to']}).update_post_params(data) return HttpResponse(json.dumps(data))
def post(self, *args, **kwargs): file_url = '' if self.request.FILES: upload_file = self.request.FILES['uploadFile'] path_prefix = datetime.now().strftime('%y/%m/') file_name = default_storage.get_available_name( path_prefix + upload_file.name) default_storage.save(file_name, upload_file) file_url = default_storage.url(file_name) return render_to_response(self.template_name, {'file_url': file_url})
def _get_key(request, model, id_name=None): """Generate the S3 key name from the filename, while guaranteeing uniqueness""" name = request.POST.get("name") attached_id = request.POST.get("id") name = _key_name_trim(name) attachment = (model(user=request.user, **{id_name: attached_id}) if id_name else model()) key = attachment.ffile.field.generate_filename(attachment.ffile.instance, name) return default_storage.get_available_name(key)
def make_permanent_directory(temp_path, instance): public_dir = construct_permanent_path(instance) filename = filename_from_path(temp_path) full_dir = os.path.join(MEDIA_ROOT, public_dir) if not os.path.exists(full_dir): os.makedirs(full_dir) full_path = os.path.join(full_dir, filename) available_full_path = default_storage.get_available_name(full_path) return available_full_path
def get_user_avatar(strategy, details, response, uid=None, user=None, is_new=False, *args, **kwargs): image_url = None link = None if not user: return profile = user.person if strategy.backend.name == 'facebook': image_url = 'http://graph.facebook.com/%s/picture?type=large' % response.get('id') link = response.get('link') #profile.facebook_profile = link elif strategy.backend.name == 'vk-oauth2': image_url = response.get('user_photo') link = 'http://vk.com/%s' % response.get('screen_name') #profile.vk_profile = link elif strategy.backend.name == 'twitter': image_url = response.get('profile_image_url') if not 'default_profile' in image_url: image_url = image_url.replace('_normal', '_bigger') link = 'http://twitter.com/%s' % response.get('screen_name') #profile.twitter_profile = link profile.save() if image_url: social = kwargs.get('social') or strategy.storage.user.get_social_auth( strategy.backend.name, uid ) if social: extra_data = strategy.backend.extra_data(user, uid, response, details) extra_data.update({ 'avatar': image_url, 'link': link, }) social.set_extra_data(extra_data) if image_url and not profile.avatar: try: image_content = urlopen(image_url) if strategy.backend.name == 'facebook' and 'image/gif' in str(image_content.info()): return image_name = default_storage.get_available_name( profile.avatar.field.upload_to( profile, make_upload_path_avatar(image_content.headers.subtype) ) ) profile.avatar.save(image_name, ContentFile(image_content.read())) profile.save() except Exception: pass # Here we completely do not care about errors
def make_permanent_directory(temp_path, instance): public_dir = construct_permanent_path(instance) filename = filename_from_path(temp_path) full_dir = os.path.join(MEDIA_ROOT, public_dir) if not os.path.exists(full_dir): os.makedirs(full_dir) full_path = os.path.join(full_dir, filename) available_full_path = default_storage.get_available_name(full_path) return available_full_path
def key_name_comm(request): """Generate the S3 key name from the filename""" name = request.POST.get('name') name = _key_name_trim(name) file_ = FOIAFile() key = file_.ffile.field.generate_filename( file_.ffile.instance, name, ) key = default_storage.get_available_name(key) return JsonResponse({'key': key})
def _key_name(request, model, id_name): """Generate the S3 key name from the filename""" name = request.POST.get('name') attached_id = request.POST.get('id') name = _key_name_trim(name) attachment = model(user=request.user, **{id_name: attached_id}) key = attachment.ffile.field.generate_filename( attachment.ffile.instance, name, ) key = default_storage.get_available_name(key) return JsonResponse({'key': key})
def get_upload_filename(upload_name): # Generate date based path to put uploaded file. date_path = datetime.now().strftime('%Y/%m/%d') # Complete upload path (upload_path + date_path). upload_path = os.path.join(settings.SIMDITOR_UPLOAD_PATH, date_path) if getattr(settings, 'SIMDITOR_UPLOAD_SLUGIFY_FILENAME', True): upload_name = utils.slugify_filename(upload_name) return default_storage.get_available_name( os.path.join(upload_path, upload_name))
def _save_profile_photo(self, user: Optional[User], photo_uuid: Optional[str]) -> None: if not user or not photo_uuid: return image_file_record: TemporaryFile = get_object_or_404(TemporaryFile, uuid=photo_uuid, uploaded_by=user) content_file = ContentFile(image_file_record.file.read()) target_file_name = image_file_record.file.name user.photo.save(default_storage.get_available_name(target_file_name), content_file) image_file_record.file.close()
def save(self): """ Saves the curve in the database. If the curve is data_read_only The actual datafile will be saved only on the first call of save(). """ tic = profile(PROFILE_SAVE, "start: ") self.set_default_params() tic = profile(PROFILE_SAVE, "set_defaults: ", tic) #models.Model.save(self) #this way, the id is correct tic = profile(PROFILE_SAVE, "other default assignements: ", tic) self.save_params() #this saves the curve with the correct id if not self.data_file: self.data_file = os.path.join( \ self.params["date"].strftime('%Y/%m/%d'), \ slugify(str(self.id) +"_"+ self.name) + '.h5') full_path = self.get_full_filename() dirname = os.path.dirname(full_path) tic = profile(PROFILE_SAVE, "format dirname: ", tic) if not os.path.exists(dirname): os.makedirs(dirname) tic = profile(PROFILE_SAVE, "create dir: ", tic) full_path = default_storage.get_available_name(full_path) tic = profile(PROFILE_SAVE, "get_available_name: ", tic) self.data_file = os.path.relpath(full_path, MEDIA_ROOT) tic = profile(PROFILE_SAVE, "set datafile: ", tic) tic = profile(PROFILE_SAVE, "save_params: ", tic) if not self.params["data_read_only"]: Curve.save(self, self.get_full_filename()) tic = profile(PROFILE_SAVE, "Curve.save(): ", tic) else: if not os.path.exists(self.get_full_filename()): Curve.save(self, self.get_full_filename()) tic = profile(PROFILE_SAVE, "Curve.save(): ", tic) self.save_tags() tic = profile(PROFILE_SAVE, "save tags ", tic) #if self.saved_in_db==False: # self.saved_in_db=True # models.Model.save(self) models.Model.save(self) tic = profile(PROFILE_SAVE, "Model.save(): ", tic)
def make_temp_directory(filename, user): public_dir = construct_temp_path(user) public_path = unify_path(os.path.join(public_dir, filename)) try: full_dir = os.path.join(settings.MEDIA_ROOT, public_dir) if not os.path.exists(full_dir): os.makedirs(full_dir) except EnvironmentError: # deepest dir already exists pass full_path = os.path.join(settings.MEDIA_ROOT, public_path) available_full_path = default_storage.get_available_name(full_path) return available_full_path
def make_temp_directory(filename, user): public_dir = construct_temp_path(user) full_dir = os.path.join(settings.MEDIA_ROOT, public_dir) try: if not os.path.exists(full_dir): os.makedirs(full_dir) except EnvironmentError: # deepest dir already exists pass full_path = os.path.join(full_dir, filename) available_full_path = default_storage.get_available_name(full_path) return available_full_path
def key_name(request): """Generate the S3 key name from the filename""" name = request.POST.get('name') foia_id = request.POST.get('foia_id') attachment = OutboundAttachment( user=request.user, foia_id=foia_id, ) key = attachment.ffile.field.generate_filename( attachment.ffile.instance, name, ) key = default_storage.get_available_name(key) return JsonResponse({'key': key})
def fetch_image_to_image_field(image_url: str, target_field: ImageField) -> None: """Fetches image from url and assigns it to image file field""" response = requests.get(image_url) response.raise_for_status() content = response.content content_file = ContentFile(content) content_type = response.headers.get('content-type') extension = guess_extension(content_type) target_file_name = '{0}{1}'.format(uuid4(), extension) target_field.save( default_storage.get_available_name(target_file_name), content_file )
def get_upload_filename(upload_name, user): # If PAGEDOWN_RESTRICT_BY_USER is True upload file to user specific path. if getattr(settings, 'PAGEDOWN_RESTRICT_BY_USER', False): user_path = user.username else: user_path = '' # Generate date based path to put uploaded file. date_path = datetime.now().strftime('%Y/%m/%d') # Complete upload path (upload_path + date_path). upload_path = os.path.join( settings.PAGEDOWN_UPLOAD_PATH, user_path, date_path) return default_storage.get_available_name(os.path.join(upload_path, upload_name))
def new_from_slug_and_revision(cls, slug, revision, **kw): """ Generate a file set with a path name corresponding to a given slug and revision. Guaranteed to succeed. """ name = os.path.join(_sanitize_name(slug), '%04d' % revision) name = default_storage.get_available_name(os.path.join('catalog', name)) if name.startswith('catalog' + os.path.sep): name = name[7+len(os.path.sep):] else: raise SuspiciousOperation("invalid fileset name from %r:%r" % ( slug, revision)) kw['name'] = name return cls(**kw)
def key_name_dataset(request): """Generate the S3 key name from the filename""" name = request.POST.get('name') name = _key_name_trim(name) today = date.today() key = ( 'dataset_uploads/{username}/{year}/{month:02d}/{day:02d}/{name}'.format( username=request.user.username, year=today.year, month=today.month, day=today.day, name=name, ) ) key = default_storage.get_available_name(key) return JsonResponse({'key': key})
def upload(request): uploaded_file = request.FILES['upload'] ck_func_num = escape(request.GET['CKEditorFuncNum']) date_path = datetime.now().strftime('%Y/%m/%d') upload_path = os.path.join("static/upload/", date_path) filename = default_storage.get_available_name(os.path.join(upload_path, uploaded_file.name)) saved_path = default_storage.save(filename, uploaded_file) url = default_storage.url(saved_path) return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '/{1}'); </script>""".format(ck_func_num, url))
def get_upload_filename(upload_name, user): # If CKEDITOR_RESTRICT_BY_USER is True upload file to user specific path. if getattr(settings, 'CKEDITOR_RESTRICT_BY_USER', False): user_path = user.username else: user_path = '' # Complete upload path (upload_path + date_path). upload_path = os.path.join( settings.CKEDITOR_UPLOAD_PATH, user_path) if getattr(settings, "CKEDITOR_UPLOAD_SLUGIFY_FILENAME", True): upload_name = utils.slugify_filename(upload_name) return default_storage.get_available_name(os.path.join(upload_path, upload_name))
def get_upload_filename(upload_name, user): # If CKEDITOR_RESTRICT_BY_USER is True upload file to user specific path. if getattr(settings, "CKEDITOR_RESTRICT_BY_USER", False): user_path = user.username else: user_path = "" # Generate date based path to put uploaded file. date_path = datetime.now().strftime("%Y/%m/%d") # Complete upload path (upload_path + date_path). upload_path = os.path.join(settings.CKEDITOR_UPLOAD_PATH, user_path, date_path) if getattr(settings, "CKEDITOR_UPLOAD_SLUGIFY_FILENAME", True): upload_name = utils.slugify_filename(upload_name) return default_storage.get_available_name(os.path.join(upload_path, upload_name))
def determine_name(request): if not request.POST: return HttpResponseBadRequest() upload_to = request.POST["upload_to"] upload_to = os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(upload_to)))) desired_path = os.path.join(upload_to, request.POST["filename"]) if desired_path.startswith("/"): desired_path = desired_path[1:] path = default_storage.get_available_name(desired_path) folder = os.path.split(desired_path)[0] data = {"targetpath": path, "targetname": os.path.split(path)[-1]} backend = get_directupload_backend() backend(request=request, options={"folder": folder}).update_post_params(data) return HttpResponse(json.dumps(data))
def save_profile_photo(user: Optional[User], photo_uuid: Optional[str]) -> None: if not user: return if photo_uuid is None: user.photo = None user.save(update_fields=[ 'photo', ]) return image_file_record: TemporaryFile = get_object_or_404(TemporaryFile, uuid=photo_uuid, uploaded_by=user) content_file = ContentFile(image_file_record.file.read()) target_file_name = image_file_record.file.name user.photo.save(default_storage.get_available_name(target_file_name), content_file) image_file_record.file.close()
def get_upload_filename(upload_name, user): # If CKEDITOR_RESTRICT_BY_USER is True upload file to user specific path. if getattr(settings, 'CKEDITOR_RESTRICT_BY_USER', False): user_path = user.username else: user_path = '' # Generate date based path to put uploaded file. date_path = datetime.now().strftime('%Y/%m/%d') # Complete upload path (upload_path + date_path). upload_path = os.path.join( settings.CKEDITOR_UPLOAD_PATH, user_path, date_path) if getattr(settings, "CKEDITOR_UPLOAD_SLUGIFY_FILENAME", True): upload_name = utils.slugify_filename(upload_name) return default_storage.get_available_name(os.path.join(upload_path, upload_name))
def save_profile_picture_and_profile_url(backend, user, response, *args, **kwargs): if user is None or not response: return image_url = None profile_url = None if backend.name == 'vk-oauth2': image_url = response.get('photo_100') profile_url = 'https://vk.com/id{}'.format(response.get('uid')) user.vkontakte = profile_url elif backend.name == 'facebook': image_url = 'http://graph.facebook.com/{0}/picture?type=normal'.format( response['id']) profile_url = response.get('link') user.facebook = profile_url elif backend.name == 'odnoklassniki-oauth2': image_url = response.get('pic_2') if 'stub' in image_url: # No real image image_url = None profile_url = 'http://ok.ru/profile/{}'.format(response.get('uid')) user.odnoklassniki = profile_url elif backend.name == 'linkedin-oauth2': image_url = response.get('pictureUrl') profile_url = response.get('publicProfileUrl') user.linkedin = profile_url if profile_url: user.save() if image_url and not user.avatar: try: image_content = urlopen(image_url) image_name = default_storage.get_available_name( user.avatar.field.upload_to + '/' + str(user.id) + '.' + image_content.headers.subtype) user.avatar.save(image_name, ContentFile(image_content.read())) user.save() except Exception: pass
def get_upload_filename(upload_name, user): # If CKEDITOR_RESTRICT_BY_USER is True upload file to user specific path. if getattr(settings, 'CKEDITOR_RESTRICT_BY_USER', False): if getattr(settings, 'CKEDITOR_RESTRICT_BY_USER_ID', False): user_path = hashlib.md5(str(user.id ** 2).encode()).hexdigest()[:16] else: user_path = user.username else: user_path = '' # Generate date based path to put uploaded file. date_path = datetime.now().strftime('%Y/%m/%d') # Complete upload path (upload_path + date_path). upload_path = os.path.join( settings.CKEDITOR_UPLOAD_PATH, user_path, date_path) if getattr(settings, "CKEDITOR_UPLOAD_SLUGIFY_FILENAME", True): upload_name = utils.slugify_filename(upload_name) return default_storage.get_available_name(os.path.join(upload_path, upload_name))
def _save_attachments(self): # @@@ convert into proper form field with widget? delete_ids = set(self.data.getlist("remove-attachment")) for attachment in self.instance.attachments: if attachment.id in delete_ids: attachment.delete() # if we're saving as new version, bring forward existing attachments # from previous version prior_version = getattr(self, "prior_version", None) if prior_version is not None: for attachment in prior_version.attachments: self.instance.attachments.post(attachment) if not self.files: return for uf in self.files.getlist("attachment"): try: file_name = uf.name file_size = uf.size except AttributeError: continue if not file_name or not file_size: continue storage_name = default_storage.get_available_name( default_storage.get_valid_name(file_name)) default_storage.save(storage_name, uf) attachment = Attachment( name=storage_name, description=file_name, url=default_storage.url(storage_name), size=file_size, attachmentType=AttachmentType.UNSPECIFIED ) self.instance.attachments.post(attachment)
def get_upload_filename(upload_name, user): user_path = '' # If CKEDITOR_RESTRICT_BY_USER is True upload file to user specific path. RESTRICT_BY_USER = getattr(settings, 'CKEDITOR_RESTRICT_BY_USER', False) if RESTRICT_BY_USER: try: user_prop = getattr(user, RESTRICT_BY_USER) except (AttributeError,TypeError): user_prop = getattr(user, 'get_username') if callable(user_prop): user_path = user_prop() else: user_path = user_prop # Generate date based path to put uploaded file. # If CKEDITOR_RESTRICT_BY_DATE is True upload file to date specific path. if getattr(settings, 'CKEDITOR_RESTRICT_BY_DATE', True): date_path = datetime.now().strftime('%Y/%m/%d') else: date_path = '' # Complete upload path (upload_path + date_path). upload_path = os.path.join( settings.CKEDITOR_UPLOAD_PATH, user_path, date_path ) if (getattr(settings, 'CKEDITOR_UPLOAD_SLUGIFY_FILENAME', True) and not hasattr(settings, 'CKEDITOR_FILENAME_GENERATOR')): upload_name = utils.slugify_filename(upload_name) if hasattr(settings, 'CKEDITOR_FILENAME_GENERATOR'): generator = import_string(settings.CKEDITOR_FILENAME_GENERATOR) upload_name = generator(upload_name) return default_storage.get_available_name( os.path.join(upload_path, upload_name) )
def ajax_upload_file(request): max_upload_size = settings.EMAIL_ATTACHMENT_MAX_UPLOAD_SIZE if request.method == "POST" and request.is_ajax() and request.user.is_expertA: upload_file = request.FILES.get('upload_file') error = None if upload_file: # BUG #2369 restriction of file content type does not work. (Removed from code now) if upload_file._size > max_upload_size: error = _('Please keep file size under {max_size}. Current file size {size}.').format({ 'max_size': filesizeformat(max_upload_size), 'size': filesizeformat(upload_file._size)}) else: error = _('No file.') context = {'error': error} if not error: upload_path = settings.EMAIL_ATTACHMENT_UPLOAD_PATH upload_filename = default_storage.get_available_name(join(upload_path, translify(upload_file.name))) saved_path = default_storage.save(upload_filename, upload_file) context = {'saved_filename': basename(saved_path), 'original_filename': upload_file.name} return JSONResponse(context) raise PermissionDenied
def avatar_save(self, upload): while True: if self.is_img_exist: self.avatar_delete() # change the url to reload the cache in user browser # if self.path_prefix: # break sha_hash = generate_sha1() test_prefix = r"%s/%s/%s" % \ (sha_hash[0:2], sha_hash[2:4], sha_hash[4:15], ) test_path = r"%s/%s_%s.jpg" % (AVATARS_DIR, test_prefix, \ str(AVATAR_LARGE_NAME)) if test_path == storage.get_available_name(test_path): self.path_prefix = test_prefix self.is_img_exist = True break self._avatar_save(upload, 'origin') self._avatar_save(upload, 'large') self._avatar_save(upload, 'medium') self._avatar_save(upload, 'small') self.is_img_exist = True self.save()
def _save_attachments(self): # @@@ convert into proper form field with widget? delete_ids = set(self.data.getlist("remove-attachment")) for attachment in self.instance.attachments: if attachment.id in delete_ids: attachment.delete() # if we're saving as new version, bring forward existing attachments # from previous version if self.prior_version is not None: for attachment in self.prior_version.attachments: self.instance.attachments.post(attachment) if not self.files: return for uf in self.files.getlist("attachment"): try: file_name = uf.name file_size = uf.size except AttributeError: continue if not file_name or not file_size: continue storage_name = default_storage.get_available_name( default_storage.get_valid_name(file_name)) default_storage.save(storage_name, uf) attachment = Attachment(name=storage_name, description=file_name, url=default_storage.url(storage_name), size=file_size, attachmentType=AttachmentType.UNSPECIFIED) self.instance.attachments.post(attachment)
def save_profile_picture_and_profile_url(backend, user, response, *args, **kwargs): if user is None or not response: return image_url = None profile_url = None if backend.name == 'vk-oauth2': image_url = response.get('photo_100') profile_url = 'https://vk.com/id{}'.format(response.get('uid')) user.vkontakte = profile_url elif backend.name == 'facebook': image_url = 'http://graph.facebook.com/{0}/picture?type=normal'.format(response['id']) profile_url = response.get('link') user.facebook = profile_url elif backend.name == 'odnoklassniki-oauth2': image_url = response.get('pic_2') if 'stub' in image_url: # No real image image_url = None profile_url = 'http://ok.ru/profile/{}'.format(response.get('uid')) user.odnoklassniki = profile_url elif backend.name == 'linkedin-oauth2': image_url = response.get('pictureUrl') profile_url = response.get('publicProfileUrl') user.linkedin = profile_url if profile_url: user.save() if image_url and not user.avatar: try: image_content = urlopen(image_url) image_name = default_storage.get_available_name(user.avatar.field.upload_to + '/' + str(user.id) + '.' + image_content.headers.subtype) user.avatar.save(image_name, ContentFile(image_content.read())) user.save() except Exception: pass
def get_available_name(name): """ Returns a filename that's free on the target storage system, and available for new content to be written to. """ return default_storage.get_available_name(name)
def uploadable_content_path(instance, filename): return default_storage.get_available_name( Path('content', str(instance.page_id), instance.__class__.__name__, filename))
def _get_thumbnail_path(self, filename): path = os.path.dirname(self.file.name) name, ext = os.path.splitext(filename) thumbnail_name = default_storage.get_available_name('thumbnail%s' % ext) return os.path.join(path, 'thumbnails', thumbnail_name)
def update_recipe_image(self): if 'file' in self._request.FILES.keys(): image = self._request.FILES['file'] self._recipe.image.save( default_storage.get_available_name(image.name), image)
def user_details(strategy, backend, details, response, user=None, *args, **kwargs): """Update user details using data from provider.""" if user: if kwargs["is_new"]: attrs = {"user": user} image_url = None print(response) if backend.name == "facebook": fb_data = {"name": response["name"]} image_url = "http://graph.facebook.com/%s/picture?type=large" % response["id"] if "gender" in response: fb_data["gender"] = response["gender"] if "link" in response: fb_data["website"] = response["link"] if "first_name" in response: user.first_name = response["first_name"] if "last_name" in response: user.last_name = response["last_name"] if "email" in response: user.email = response["email"] user.save() attrs.update(fb_data) elif backend.name == "twitter": tw_data = {"name": response["access_token"]["screen_name"]} if "url" in response: tw_data["website"] = response["url"] image_url = response.get("profile_image_url", "").replace("_normal", "") attrs.update(tw_data) elif backend.name == "google-oauth2": gg_data = {} if "displayName" in response: gg_data["name"] = response["displayName"] if "gender" in response: gg_data["gender"] = response["gender"] if "url" in response: gg_data["website"] = response["url"] if "name" in response: if "givenName" in response["name"]: user.first_name = response["name"]["givenName"] if "familyName" in response["name"]: user.last_name = response["name"]["familyName"] user.save() if "image" in response: if "url" in response["image"]: image_url = response["image"]["url"].replace("?sz=50", "") attrs.update(gg_data) user_profile = UserProfile.objects.create(**attrs) if image_url: try: if backend.name == "twitter" and "default_profile_images" in image_url: pass else: image_content = urlopen(image_url) image_name = default_storage.get_available_name( user_profile.profileImage.field.upload_to + "/" + str(user_profile.user.id) + "." + image_content.info()["content-type"].replace("image/", "") ) path = default_storage.save(image_name, ContentFile(image_content.read())) user_profile.profileImage = path user_profile.save() except Exception: pass
def _save_file(self, f, slug, extension): path = default_storage.get_available_name(f'{slug}.{extension}') cfile = ContentFile(f.read()) default_storage.save(path, cfile) return path, cfile
def get_available_name(self, name, max_length=None): return default_storage.get_available_name(name, max_length=None)