Exemplo n.º 1
0
async def upload_picture(field):
    # TODO: should check content for actual picture; should also resize on the client side
    extension = field.filename.split('.')[-1].lower()
    if extension not in ['jpg', 'jpeg', 'png', 'gif']:
        raise web.HTTPBadRequest(
            reason=
            'Picture file type not allowed, please use jpg, jpeg, png or gif')
    base = str(uuid.uuid4())
    orig_filename = 'orig-%s.%s' % (base, extension)
    filename = '%s.jpg' % base
    size = 0
    with open('./data/pictures/' + orig_filename, 'wb') as f:
        while True:
            chunk = await field.read_chunk()  # 8192 bytes by default.
            if not chunk:
                break
            size += len(chunk)
            f.write(chunk)
            if size > 1024 * 1024 * 10:  # max 10M
                fp.close()
                os.unlink('./data/pictures/' + orig_filename)
                raise web.HTTPBadRequest(reason='Picture file too large')
    #status, stdout, stderr = await run_command('./src/make-thumbnail.sh "./data/pictures/%s" "./data/pictures/%s"' % (orig_filename, filename))
    if not thumbnail.make_thumbnail('./data/pictures/' + orig_filename,
                                    './data/pictures/' + filename, 512):
        remove_file('./data/pictures/' + filename)
        #raise web.HTTPBadRequest(reason='Cannot resize picture (status=%d, stdout="%s", stderr="%s")' % (status, stdout, stderr))
        raise web.HTTPBadRequest(reason='Cannot resize picture')
    remove_file('./data/pictures/' + orig_filename)
    return '/pictures/' + filename
Exemplo n.º 2
0
 def process_item(self, item, orig_item = None):
     if isinstance(item, dict) and "subpage" in item:
         item["subpage"] = self.process_item(item["subpage"])
     if type(item) in (str,unicode) and os.path.exists(item) and not os.path.isdir(item):
         if not orig_item: orig_item = {}
         newitem = {"type": get_file_type(item), "mime": get_mimetype(item)}
         if "width" in orig_item: newitem["width"] = orig_item["width"]
         if "height" in orig_item: newitem["height"] = orig_item["height"]
         if newitem["type"] == "image" and ("width" not in newitem or "height" not in newitem):
             content_type, width, height = getImageInfo(open(item).read())
             if content_type and width and height:
                 try:
                     if "width" not in newitem and "height" not in newitem:
                         newitem["width"] = width
                         newitem["height"] = height
                     elif "width" not in newitem:
                         newitem["width"] = width * newitem["height"] / height
                     else:
                         newitem["height"] = height * newitem["width"] / width
                 except TypeError:
                     pass
         newitem["rawpath"] = item
         if self.params["WEB_ROOT"] in item:
             newitem["url"] = item.replace(self.params["WEB_ROOT"],
                                           self.params["WEB_URL"])
         elif self.params["copy_images"]:
             new_name = item.replace(os.sep, "_")
             new_path = os.path.join(self.params["target_dir"], "imgs", new_name)
             if not DEBUG and (not os.path.exists(new_path) or (os.path.getmtime(item) > os.path.getmtime(new_path))): shutil.copy(item, new_path)
             newitem["rawpath"] = new_path
             newitem["url"] = os.path.join("imgs", new_name)
         else:
             newitem["url"] = item.replace(self.params["target_dir"], "")
         return newitem
     elif isinstance(item, dict) and "type" in item:
         if item["type"] in ("image", "video"):
             processed = self.process_item(item["url"], item)
             if isinstance(processed, dict):
                 item.update(processed)
             else:
                 item["url"] = processed
             if "popup" in item and "rawpath" in item:
                 item["fullurl"] = item["url"]
                 item["url"] = make_thumbnail(item)
                 item["type"] = "imagegallery" if item["popup"] == "gallery" else "imagepopup"
             return item
         elif item["type"] == "stack":
             item["stack"] = self.process_items(item["stack"])
             return item
         elif item["type"] in item_processors:
             return item_processors[item["type"]](item, self.params)
         else:
             return item
     elif isinstance(item, list):
         return self.process_items(item)
     else:
         return item
Exemplo n.º 3
0
def upload(request):
    if 'file' in request.FILES:
        file = request.FILES['file']
        title = request.POST.get("title", "Untitled")
        description = request.POST.get("description", None)
        user = request.user
        if not user.id:
            user = None
        m = Map(title=title, description=description, width=0, height=0, user=user)
        m.file.save(file.name, file)
        im = make_thumbnail(m.file.path)
        m.width = im[0]
        m.height = im[1]
        m.save()
        return HttpResponseRedirect("/rectifier/rectify/%s" % m.id)
Exemplo n.º 4
0
def upload(request):
    if 'file' in request.FILES:
        file = request.FILES['file']
        title = request.POST.get("title", "Untitled")
        description = request.POST.get("description", None)
        user = request.user
        if not user.id:
            user = None
        m = Map(title=title,
                description=description,
                width=0,
                height=0,
                user=user)
        m.file.save(file.name, file)
        im = make_thumbnail(m.file.path)
        m.width = im[0]
        m.height = im[1]
        m.save()
        return HttpResponseRedirect("/rectifier/rectify/%s" % m.id)
Exemplo n.º 5
0
 def takePictureCallback(pic_file):
     debug("got picture callback")
     # create a thumbnail
     make_thumbnail(pic_file, pic_file+".thumb", 90)
     # notify change
     send_directory_list()
Exemplo n.º 6
0
 def process_item(self, item, orig_item=None, in_media_item=False):
     if isinstance(item, dict) and "subpage" in item:
         item["subpage"] = self.process_item(item["subpage"])
     if type(item) in (str, unicode) and os.path.exists(item) and not os.path.isdir(item):
         if not orig_item:
             orig_item = {}
         newitem = {"type": get_file_type(item), "mime": get_mimetype(item)}
         if "width" in orig_item:
             newitem["width"] = orig_item["width"]
         if "height" in orig_item:
             newitem["height"] = orig_item["height"]
         if newitem["type"] == "image" and ("width" not in newitem or "height" not in newitem):
             width, height = get_image_size(item)
             if width and height:
                 try:
                     if "width" not in newitem and "height" not in newitem:
                         newitem["width"] = width
                         newitem["height"] = height
                     elif "width" not in newitem:
                         newitem["width"] = width * newitem["height"] / height
                     else:
                         newitem["height"] = height * newitem["width"] / width
                 except TypeError:
                     pass
         # Should all images should be converted to thumbnails?
         do_thumbnail = False
         if newitem["type"] == "image" and self.params["allthumbs"]:
             # If we are in a media item, do not make thumbnail now, it will
             # be done (if needed) in the media item
             if not in_media_item:
                 adjust_thumb_size(newitem, self.params)
                 do_thumbnail = True
         newitem["rawpath"] = item
         if self.params['WEB_ROOT'] and self.params['WEB_URL'] and self.params["WEB_ROOT"] in item:
             newitem["url"] = item.replace(self.params["WEB_ROOT"],
                                           self.params["WEB_URL"])
         elif self.params["copy_images"] or do_thumbnail:
             new_name = item.replace(os.sep, "_")
             new_path = os.path.join(self.params["target_dir"], "imgs", new_name)
             if not do_thumbnail and not orig_item.get("type") == "thumbnail":
                 if not DEBUG and (not os.path.exists(new_path) or (os.path.getmtime(item) > os.path.getmtime(new_path))):
                     shutil.copy(item, new_path)
             newitem["rawpath"] = new_path
             newitem["url"] = os.path.join("imgs", new_name)
             if do_thumbnail:
                 newitem["url"] = make_thumbnail(newitem, orig_path=item)
         else:
             newitem["url"] = item.replace(self.params["target_dir"], "")
         if "type" in orig_item:
             newitem["type"] = orig_item["type"]
         return newitem
     elif isinstance(item, dict) and "type" in item:
         if item["type"] in ("image", "video", "thumbnail"):
             original_item = dict(item)
             processed = self.process_item(item["url"], item, in_media_item=True)
             if isinstance(processed, dict):
                 item.update(processed)
             else:
                 item["url"] = processed
             if item["type"] == "image" and self.params["allthumbs"]:
                 item["type"] = "thumbnail"
                 if not original_item.get("width") and not original_item.get("height"):
                     adjust_thumb_size(item, self.params)
             if ("popup" in item or item["type"] == "thumbnail") and "rawpath" in item:
                 item["url"] = make_thumbnail(item, orig_path=original_item["url"])
                 if item["type"] != "thumbnail":
                     item["fullurl"] = item["url"]
                     item["type"] = "imagegallery" if item["popup"] == "gallery" else "imagepopup"
                 else:
                     item["type"] = "image"
             return item
         elif item["type"] == "stack":
             item["stack"] = self.process_items(item["stack"])
             return item
         elif item["type"] in item_processors:
             return item_processors[item["type"]](item, self.params)
         else:
             return item
     elif isinstance(item, list):
         return self.process_items(item)
     else:
         return item
Exemplo n.º 7
0
 def process_item(self, item, orig_item=None, in_media_item=False):
     if isinstance(item, dict) and "subpage" in item:
         item["subpage"] = self.process_item(item["subpage"])
     if type(item) in (
             str,
             unicode) and os.path.exists(item) and not os.path.isdir(item):
         if not orig_item:
             orig_item = {}
         newitem = {"type": get_file_type(item), "mime": get_mimetype(item)}
         if "width" in orig_item:
             newitem["width"] = orig_item["width"]
         if "height" in orig_item:
             newitem["height"] = orig_item["height"]
         if newitem["type"] == "image" and ("width" not in newitem
                                            or "height" not in newitem):
             width, height = get_image_size(item)
             if width and height:
                 try:
                     if "width" not in newitem and "height" not in newitem:
                         newitem["width"] = width
                         newitem["height"] = height
                     elif "width" not in newitem:
                         newitem[
                             "width"] = width * newitem["height"] / height
                     else:
                         newitem[
                             "height"] = height * newitem["width"] / width
                 except TypeError:
                     pass
         # Should all images should be converted to thumbnails?
         do_thumbnail = False
         if newitem["type"] == "image" and self.params["allthumbs"]:
             # If we are in a media item, do not make thumbnail now, it will
             # be done (if needed) in the media item
             if not in_media_item:
                 adjust_thumb_size(newitem, self.params)
                 do_thumbnail = True
         newitem["rawpath"] = item
         if self.params['WEB_ROOT'] and self.params[
                 'WEB_URL'] and self.params["WEB_ROOT"] in item:
             newitem["url"] = item.replace(self.params["WEB_ROOT"],
                                           self.params["WEB_URL"])
         elif self.params["copy_images"] or do_thumbnail:
             new_name = item.replace(os.sep, "_")
             new_path = os.path.join(self.params["target_dir"], "imgs",
                                     new_name)
             if not do_thumbnail and not orig_item.get(
                     "type") == "thumbnail":
                 if not DEBUG and (
                         not os.path.exists(new_path) or
                     (os.path.getmtime(item) > os.path.getmtime(new_path))):
                     shutil.copy(item, new_path)
             newitem["rawpath"] = new_path
             newitem["url"] = os.path.join("imgs", new_name)
             if do_thumbnail:
                 newitem["url"] = make_thumbnail(newitem, orig_path=item)
         else:
             newitem["url"] = item.replace(self.params["target_dir"], "")
         if "type" in orig_item:
             newitem["type"] = orig_item["type"]
         return newitem
     elif isinstance(item, dict) and "type" in item:
         if item["type"] in ("image", "video", "thumbnail"):
             original_item = dict(item)
             processed = self.process_item(item["url"],
                                           item,
                                           in_media_item=True)
             if isinstance(processed, dict):
                 item.update(processed)
             else:
                 item["url"] = processed
             if item["type"] == "image" and self.params["allthumbs"]:
                 item["type"] = "thumbnail"
                 if not original_item.get(
                         "width") and not original_item.get("height"):
                     adjust_thumb_size(item, self.params)
             if ("popup" in item
                     or item["type"] == "thumbnail") and "rawpath" in item:
                 item["url"] = make_thumbnail(
                     item, orig_path=original_item["url"])
                 if item["type"] != "thumbnail":
                     item["fullurl"] = item["url"]
                     item["type"] = "imagegallery" if item[
                         "popup"] == "gallery" else "imagepopup"
                 else:
                     item["type"] = "image"
             return item
         elif item["type"] == "stack":
             item["stack"] = self.process_items(item["stack"])
             return item
         elif item["type"] in item_processors:
             return item_processors[item["type"]](item, self.params)
         else:
             return item
     elif isinstance(item, list):
         return self.process_items(item)
     else:
         return item