-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #763 from puzzle/bug/762-fix-file-upload
fix file upload issue in folder form
- Loading branch information
Showing
5 changed files
with
37 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters