Exemplo n.º 1
0
    def _save_attachments(self, db):
        self.log_info("Saving attachments")

        with os.scandir(self.dir_attach_incoming) as iterator:
            for entry in iterator:
                self.log_info("Processing attachment “%s”" % entry.name)

                try:
                    with open(entry.path, "r") as fil:
                        attachment = json.load(fil)
                except (OSError, ValueError) as ex:
                    self.log_warn("Failed to load attachment: {0}".format(
                        str(ex)))
                    self._move_attachment_to_deferred(entry.name)
                    continue

                try:
                    validate_attachment(attachment)
                except FafError as ex:
                    self.log_warn("Attachment is invalid: {0}".format(str(ex)))
                    self._move_attachment_to_deferred(entry.name)
                    continue

                try:
                    save_attachment(db, attachment)
                except FafError as ex:
                    self.log_warn("Failed to save attachment: {0}".format(
                        str(ex)))
                    self._move_attachment_to_deferred(entry.name)
                    continue

                self._move_attachment_to_saved(entry.name)
Exemplo n.º 2
0
Arquivo: reports.py Projeto: abrt/faf
def attach():
    form = NewAttachmentForm()
    if request.method == "POST":
        try:
            if not form.validate() or form.file.name not in request.files:
                raise InvalidUsage("Invalid form data.", 400)
            raw_data = request.files[form.file.name].read()

            try:
                data = json.loads(raw_data)
            except:
                raise InvalidUsage("Invalid JSON file", 400)

            try:
                ureport.validate_attachment(data)
            except Exception as ex:
                raise InvalidUsage("Validation failed: %s" % ex, 400)

            attachment = data

            max_attachment_length = 2048

            if len(str(attachment)) > max_attachment_length:
                err = "uReport attachment may only be {0} bytes long" \
                      .format(max_attachment_length)
                raise InvalidUsage(err, 413)

            fname = str(uuid.uuid4())
            fpath = os.path.join(paths["attachments_incoming"], fname)
            with open(fpath, "w") as file:
                file.write(raw_data.decode("utf-8"))

            if request_wants_json():
                json_response = jsonify({"result": True})
                json_response.status_code = 202
                return json_response

            flash("The attachment was saved successfully. Thank you.",
                  "success")
            return render_template("reports/attach.html",
                                   form=form), 202

        except InvalidUsage as e:
            if request_wants_json():
                response = jsonify({"error": e.message})
                response.status_code = e.status_code
                return response

            flash(e.message, "danger")
            return render_template("reports/attach.html",
                                   form=form), e.status_code

    return render_template("reports/attach.html",
                           form=form)
Exemplo n.º 3
0
def attach():
    form = NewAttachmentForm()
    if request.method == "POST":
        try:
            if not form.validate() or form.file.name not in request.files:
                raise InvalidUsage("Invalid form data.", 400)
            raw_data = request.files[form.file.name].read()

            try:
                data = json.loads(raw_data)
            except:
                raise InvalidUsage("Invalid JSON file", 400)

            try:
                ureport.validate_attachment(data)
            except Exception as ex:
                raise InvalidUsage("Validation failed: %s" % ex, 400)

            attachment = data

            max_attachment_length = 2048

            if len(str(attachment)) > max_attachment_length:
                err = "uReport attachment may only be {0} bytes long" \
                      .format(max_attachment_length)
                raise InvalidUsage(err, 413)

            fname = str(uuid.uuid4())
            fpath = os.path.join(paths["attachments_incoming"], fname)
            with open(fpath, "w") as file:
                file.write(raw_data)

            if request_wants_json():
                json_response = jsonify({"result": True})
                json_response.status_code = 202
                return json_response
            else:
                flash("The attachment was saved successfully. Thank you.",
                      "success")
                return render_template("reports/attach.html",
                                       form=form), 202

        except InvalidUsage as e:
            if request_wants_json():
                response = jsonify({"error": e.message})
                response.status_code = e.status_code
                return response
            else:
                flash(e.message, "danger")
                return render_template("reports/attach.html",
                                       form=form), e.status_code

    return render_template("reports/attach.html",
                           form=form)
Exemplo n.º 4
0
    def clean_file(self):
        raw_data = self.cleaned_data['file'].read()

        try:
            data = json.loads(raw_data)
        except:
            raise forms.ValidationError('Invalid JSON file')

        try:
            ureport.validate_attachment(data)
        except Exception as ex:
            raise forms.ValidationError("Validation failed: %s" % ex)

        return dict(json=raw_data)
Exemplo n.º 5
0
    def _save_attachments(self, db):
        self.log_info("Saving attachments")

        attachment_filenames = os.listdir(self.dir_attach_incoming)

        i = 0
        for fname in sorted(attachment_filenames):
            i += 1

            filename = os.path.join(self.dir_attach_incoming, fname)
            self.log_info("[{0} / {1}] Processing file '{2}'"
                          .format(i, len(attachment_filenames), filename))

            try:
                with open(filename, "r") as fil:
                    attachment = json.load(fil)
            except (OSError, ValueError) as ex:
                self.log_warn("Failed to load attachment: {0}".format(str(ex)))
                self._move_attachment_to_deferred(fname)
                continue

            try:
                validate_attachment(attachment)
            except FafError as ex:
                self.log_warn("Attachment is invalid: {0}".format(str(ex)))
                self._move_attachment_to_deferred(fname)
                continue

            try:
                save_attachment(db, attachment)
            except FafError as ex:
                self.log_warn("Failed to save attachment: {0}".format(str(ex)))
                self._move_attachment_to_deferred(fname)
                continue

            self._move_attachment_to_saved(fname)
Exemplo n.º 6
0
    def _save_attachments(self, db):
        self.log_info("Saving attachments")

        attachment_filenames = os.listdir(self.dir_attach_incoming)

        i = 0
        for fname in sorted(attachment_filenames):
            i += 1

            filename = os.path.join(self.dir_attach_incoming, fname)
            self.log_info("[{0} / {1}] Processing file '{2}'"
                          .format(i, len(attachment_filenames), filename))

            try:
                with open(filename, "r") as fil:
                    attachment = json.load(fil)
            except (OSError, ValueError) as ex:
                self.log_warn("Failed to load attachment: {0}".format(str(ex)))
                self._move_attachment_to_deferred(fname)
                continue

            try:
                validate_attachment(attachment)
            except FafError as ex:
                self.log_warn("Attachment is invalid: {0}".format(str(ex)))
                self._move_attachment_to_deferred(fname)
                continue

            try:
                save_attachment(db, attachment)
            except FafError as ex:
                self.log_warn("Failed to save attachment: {0}".format(str(ex)))
                self._move_attachment_to_deferred(fname)
                continue

            self._move_attachment_to_saved(fname)
Exemplo n.º 7
0
    def test_attachment_validation(self):
        """
        Check if attachment validation works correctly.
        """

        validate_attachment(self.bugzilla_attachment)