Example #1
0
def new_folder(request, case_id):
    """Load files from a local directory."""
    case = get_object_or_404(Case, pk=case_id)

    # Security check.
    if not(request.user.is_superuser or request.user in case.users.all()):
        return render_to_response("error.html",
                                  {"error": "You are not authorized to add image to this."},
                                  context_instance=RequestContext(request))

    if case.state == "C":
        return render_to_response("error.html",
                                  {"error": "You cannot add an image to a closed case."},
                                  context_instance=RequestContext(request))

    if request.method == "POST":
        form = forms.ImageFolderForm(request.POST)
        if form.is_valid():
            # Check.
            if not os.path.exists(request.POST.get("path")):
                return render_to_response("error.html",
                    {"error": "Folder does not exist."},
                    context_instance=RequestContext(request))
            elif not os.path.isdir(request.POST.get("path")):
                return render_to_response("error.html",
                    {"error": "Folder is not a directory."},
                    context_instance=RequestContext(request))
            # Add all files in directory.
            mime = magic.Magic(mime=True)
            for file in os.listdir(request.POST.get("path")):
                content_type = mime.from_file(os.path.join(request.POST.get("path"), file))
                # Check if content type is allowed.
                if not check_allowed_content(content_type):
                    # TODO: add some kind of feedback.
                    pass

                task = Analysis()
                task.owner = request.user
                task.case = case
                task.file_name = file
                task.image_id = save_file(file_path=os.path.join(request.POST.get("path"), file),
                                          content_type=content_type)
                task.thumb_id = create_thumb(os.path.join(request.POST.get("path"), file))
                task.save()

                # Auditing.
                log_activity("I",
                             "Created new analysis {0}".format(task.file_name),
                             request)
            return HttpResponseRedirect(reverse("analyses.views.show_case", args=(case.id, "list")))
    else:
        form = forms.ImageFolderForm()

    return render_to_response("analyses/images/new_folder.html",
                              {"form": form, "case": case},
                              context_instance=RequestContext(request))
Example #2
0
 def _add_task(self, file, case, user):
     """Adds a new task to database.
     @param file: file path
     @param case: case id
     @param user: user id
     """
     task = Analysis()
     task.owner = user
     task.case = case
     task.file_name = os.path.basename(file)
     mime = magic.Magic(mime=True)
     task.image_id = save_file(file_path=file, content_type=mime.from_file(file))
     task.thumb_id = create_thumb(file)
     task.save()
Example #3
0
 def _add_task(self, file, case, user):
     """Adds a new task to database.
     @param file: file path
     @param case: case id
     @param user: user id
     """
     # File type check.
     mime = magic.Magic(mime=True)
     content_type = mime.from_file(file)
     if not check_allowed_content(content_type):
         print "WARNING: Skipping %s: file type not allowed." % file
     else:
         # Add to analysis queue.
         task = Analysis()
         task.owner = user
         task.case = case
         task.file_name = os.path.basename(file)
         task.image_id = save_file(file_path=file, content_type=content_type)
         task.thumb_id = create_thumb(file)
         task.save()
Example #4
0
def new_url(request, case_id):
    """Upload a new image via URL."""
    case = get_object_or_404(Case, pk=case_id)

    # Security check.
    if not request.user.is_superuser and not request.user in case.users.all():
        return render_to_response("error.html",
            {"error": "You are not authorized to add image to this."},
            context_instance=RequestContext(request))

    if case.state == "C":
        return render_to_response("error.html",
            {"error": "You cannot add an image to a closed case."},
            context_instance=RequestContext(request))

    if request.method == "POST":
        form = forms.UrlForm(request.POST)

        if form.is_valid():
            # Download file.
            try:
                url = urllib2.urlopen(request.POST.get("url"), timeout=5)
            except urllib2.URLError as e:
                if hasattr(e, "reason"):
                    return render_to_response("error.html",
                        {"error": "We failed to reach a server, reason: %s" % e.reason},
                        context_instance=RequestContext(request))
                elif hasattr(e, "code"):
                    return render_to_response("error.html",
                        {"error": "The remote server couldn't fulfill the request, HTTP error code %s" % e.code},
                        context_instance=RequestContext(request))

            # Store temp file.
            url_temp = NamedTemporaryFile(delete=True)
            url_temp.write(url.read())
            url_temp.flush()

            # Convert to File object.
            url_file = File(url_temp).name

            # Check content type.
            mime = magic.Magic(mime=True)
            content_type = mime.from_file(url_file)
            if not check_allowed_content(content_type):
                return render_to_response("error.html",
                    {"error": "File type not supported"},
                    context_instance=RequestContext(request))

            # Create analysis task.
            task = Analysis()
            task.owner = request.user
            task.case = case
            task.file_name = os.path.basename(urlparse.urlparse(request.POST.get("url")).path)
            task.image_id = save_file(file_path=url_file, content_type=content_type)
            task.thumb_id = create_thumb(url_file)
            task.save()
            # Auditing.
            log_activity("I",
                "Created new analysis {0} from URL {1}".format(task.file_name, request.POST.get("url")),
                request)
            return HttpResponseRedirect(reverse("analyses.views.show_case", args=(case.id, "list")))
    else:
        # Request is not a POST.
        form = forms.UrlForm()

    return render_to_response("analyses/images/new_url.html",
        {"form": form, "case": case},
        context_instance=RequestContext(request))