Skip to content

Commit

Permalink
Merge pull request #763 from puzzle/bug/762-fix-file-upload
Browse files Browse the repository at this point in the history
fix file upload issue in folder form
  • Loading branch information
mtnstar authored Feb 8, 2024
2 parents 49e21f5 + c1d0f2d commit 15aa884
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 48 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/env_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def index
private

def last_login_message
Flash::LastLoginMessage.new(session).message
# Flash::LastLoginMessage.new(session).message
end

def fallback_info
Expand Down
6 changes: 1 addition & 5 deletions app/serializers/encryptable/file_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# frozen_string_literal: true

class Encryptable::FileSerializer < ApplicationSerializer
class Encryptable::FileSerializer < EncryptableSerializer
attributes :id, :name, :description, :sender_name, :created_at

def sender_name
object.sender&.label
end

belongs_to :encryptable_credential, if: -> { object.encryptable_credential.present? }
end
19 changes: 4 additions & 15 deletions frontend/app/components/encryptable-file/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ export default class Form extends BaseFormComponent {
);

if (isFileValid) {
this.record.encryptableCredential = this.args.encryptableCredential;
this.record.folder = this.changeset.folder;
const encryptableCredential = this.args.encryptableCredential;
if(encryptableCredential) {
this.changeset.encryptableCredential = encryptableCredential;
}
return this.changeset.isValid;
}
return false;
Expand All @@ -113,19 +115,6 @@ export default class Form extends BaseFormComponent {

handleSubmitSuccess(savedRecords) {
this.abort(true);
if (!this.args.attachment) {
this.saveEditedData(savedRecords);
}
}

saveEditedData(savedRecords) {
if (isPresent(savedRecords)) {
savedRecords[0]
.json()
.then((body) =>
this.router.transitionTo("encryptables.show", body.data.id)
);
}
}

handleSubmitError(response) {
Expand Down
49 changes: 31 additions & 18 deletions frontend/app/models/encryptable-file.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
import Encryptable from "./encryptable";
import { attr, belongsTo } from "@ember-data/model";
import {attr, belongsTo} from "@ember-data/model";

export default class EncryptableFile extends Encryptable {
@attr file;
@belongsTo("encryptable-credential")
encryptableCredential;
@belongsTo("folder") folder;
@belongsTo("folder", {async: false, inverse: "encryptables", as: "encryptable"}) folder;

async save() {
if (this.isDeleted) {
return super.save();
}
const url = `/api/encryptables`;
const credentialId = await this.encryptableCredential.get("id");
const folderId = await this.folder.get("id");
const opts = await this.getRequestConfig();
let promise = this.file.upload(url, opts);
promise
.then((savedRecords) => {
savedRecords.json().then((body) => {
this.id = body.data.id;
this.name = body.data.attributes.name;
this.filename = body.data.attributes.filename;
});
});
return promise;
}

const opts = {
async getRequestConfig() {
const credentialId = await this.encryptableCredential.get("id");
if (this.folder != null) {
const folderId = await this.folder.get("id");
return {
data: {
description: this.description || "",
...(credentialId !== undefined && {credential_id: credentialId}),
...(folderId !== undefined && {folder_id: folderId})
},
headers: {
"X-CSRF-Token": this.csrfToken
}
};
}
return {
data: {
description: this.description || "",
...(credentialId !== undefined && { credential_id: credentialId }),
...(folderId !== undefined && { folder_id: folderId })
...(credentialId !== undefined && {credential_id: credentialId})
},
headers: {
"X-CSRF-Token": this.csrfToken
}
};

let promise = this.file.upload(url, opts);
promise
.then((savedRecords) => {
let data = JSON.parse(savedRecords.body).data;
this.id = data.id;
this.filename = data.attributes.filename;
})
.catch(() => {});

return promise;
}
}
9 changes: 0 additions & 9 deletions spec/system/encryptable_files_without_credential_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@
file.decrypt(plaintext_team_password)
expect(file.cleartext_file).to eq file_content

expect_encryptable_file_page_with(file_name, file_desc)

# Try to upload file with same name to same folder
expect do
create_new_file(file_desc, file_path)
end.to change { Encryptable::File.count }.by(0)

expect(page).to have_text('File name has already been taken')
click_button('Close')

expect(page).to have_text('File: test_file.txt')
end

private
Expand Down Expand Up @@ -72,9 +68,4 @@ def select_team_and_folder
first('ul.ember-power-select-options > li', visible: false).click
end

def expect_encryptable_file_page_with(filename, description)
expect(first('h2')).to have_text("File: #{filename}")
expect(page).to have_text(description)
end

end

0 comments on commit 15aa884

Please sign in to comment.