Esempio n. 1
0
    def get_from_file(self, file_path, upload_to=None, preview_w=None, preview_h=None):
        from cropduster.models import Image
        from cropduster.views.forms import clean_upload_data

        image_file = VirtualFieldFile(file_path)
        md5 = hashlib.md5()
        image_contents = image_file.read()
        md5.update(image_contents)
        basepath, basename = os.path.split(file_path)
        basefile, extension = os.path.splitext(basename)
        if basefile == 'original':
            basepath, basename = os.path.split(basepath)
            basename += extension
        standalone, created = self.get_or_create(md5=md5.hexdigest().lower())
        if created or not standalone.image:
            file_data = clean_upload_data({
                'image': SimpleUploadedFile(basename, image_contents),
                'upload_to': upload_to,
            })
            file_path = get_relative_media_url(file_data['image'].name)
            standalone.image = file_path
            standalone.save()
        else:
            file_path = get_relative_media_url(standalone.image.name)

        cropduster_image, created = Image.objects.get_or_create(
            content_type=ContentType.objects.get_for_model(StandaloneImage),
            object_id=standalone.pk)
        standalone.image.related_object = cropduster_image
        cropduster_image.image = file_path
        cropduster_image.save()
        cropduster_image.save_preview(preview_w, preview_h)
        return standalone
Esempio n. 2
0
    def download_image_url(self, url):
        from cropduster.models import StandaloneImage
        from cropduster.views.forms import clean_upload_data

        image_contents = urlopen(url).read()
        md5_hash = hashlib.md5()
        md5_hash.update(image_contents)
        try:
            standalone_image = StandaloneImage.objects.get(md5=md5_hash.hexdigest())
        except StandaloneImage.DoesNotExist:
            pass
        else:
            return get_relative_media_url(standalone_image.image.name)

        parse_result = urlparse.urlparse(url)

        fake_upload = SimpleUploadedFile(os.path.basename(parse_result.path), image_contents)
        file_data = clean_upload_data({
            'image': fake_upload,
            'upload_to': self.upload_to,
        })
        return get_relative_media_url(file_data['image'].name)
Esempio n. 3
0
    def download_image_url(self, url):
        from cropduster.models import StandaloneImage
        from cropduster.views.forms import clean_upload_data

        image_contents = urlopen(url).read()
        md5_hash = hashlib.md5()
        md5_hash.update(image_contents)
        try:
            standalone_image = StandaloneImage.objects.get(
                md5=md5_hash.hexdigest())
        except StandaloneImage.DoesNotExist:
            pass
        else:
            return get_relative_media_url(standalone_image.image.name)

        parse_result = urlparse.urlparse(url)

        fake_upload = SimpleUploadedFile(os.path.basename(parse_result.path),
                                         image_contents)
        file_data = clean_upload_data({
            'image': fake_upload,
            'upload_to': self.upload_to,
        })
        return get_relative_media_url(file_data['image'].name)