Skip to content

Commit

Permalink
fix: use downloadUrl, cleanup, testing
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel committed Dec 7, 2023
1 parent 3a5a1d5 commit 420c175
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 24 deletions.
2 changes: 0 additions & 2 deletions addon/adapters/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import ApplicationAdapter from "./application";

export default class FileAdapter extends ApplicationAdapter {
ajaxOptions(url, type, options) {
console.log(url, type, options);
const ajaxOptions = super.ajaxOptions(url, type, options);

if (type === "PUT") {
Expand All @@ -14,7 +13,6 @@ export default class FileAdapter extends ApplicationAdapter {
if (type === "PUT" || type === "POST") {
// Remove content type for updating and creating records so the content
// type will be defined by the passed form data
console.log("removing content-type header from", ajaxOptions.headers);
delete ajaxOptions.headers["content-type"];
}

Expand Down
2 changes: 1 addition & 1 deletion addon/models/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export default class DocumentModel extends LocalizedModel {
const thumbnail = this.files.filter(
(file) => file.variant === "thumbnail",
)[0];
return thumbnail && thumbnail.content;
return thumbnail && thumbnail.downloadUrl;
}
}
1 change: 1 addition & 0 deletions addon/models/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Model, { attr, belongsTo, hasMany } from "@ember-data/model";
export default class FileModel extends Model {
@attr variant;
@attr name;
@attr downloadUrl;
@attr metainfo;
@attr content;
@attr checksum;
Expand Down
2 changes: 1 addition & 1 deletion addon/serializers/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class TemplateSerializer extends JSONSerializer {

formData.append("name", name);
formData.append("variant", variant);
formData.append("document", snapshot.belongsTo("document").id);
formData.append("document", snapshot.belongsTo("document")?.id);

if (content instanceof File) {
formData.append("content", content);
Expand Down
13 changes: 1 addition & 12 deletions addon/services/documents.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { action } from "@ember/object";
import Service, { inject as service } from "@ember/service";
import { tracked } from "@glimmer/tracking";
import fetch from "fetch";

export default class DocumentsService extends Service {
@service store;
Expand Down Expand Up @@ -90,19 +89,9 @@ export default class DocumentsService extends Service {
document,
createdByGroup: this.config.activeGroup,
modifiedByGroup: this.config.activeGroup,
content: file,
});

await fileModel.save();

const response = await fetch(fileModel.uploadUrl, {
method: "PUT",
body: file,
headers: { "content-type": "application/octet-stream" },
});

if (!response.ok) {
throw new Error(response.statusText, response.status);
}
}

/**
Expand Down
7 changes: 4 additions & 3 deletions tests/acceptance/documents-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ module("Acceptance | documents", function (hooks) {
assert.dom("[data-test-file]").doesNotExist();

this.assertRequest("POST", "/api/v1/files", (request) => {
const { attributes } = JSON.parse(request.requestBody).data;
assert.strictEqual(attributes.name, "test-file.txt");
assert.strictEqual(attributes.variant, "original");
const name = request.requestBody.get("name");
const variant = request.requestBody.get("variant");
assert.strictEqual(name, "test-file.txt");
assert.strictEqual(variant, "original");
});
await triggerEvent("[data-test-replace]", "change", {
files: [new File(["Ember Rules!"], "test-file.txt")],
Expand Down
6 changes: 3 additions & 3 deletions tests/dummy/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export default function makeServer(config) {

this.get("/marks");

this.post("/files", function (schema) {
const attrs = this.normalizedRequestAttrs();
this.post("/files", function (schema, request) {
const attrs = Object.fromEntries(request.requestBody.entries());
return schema.files.create({
...attrs,
uploadUrl: "/api/v1/file-upload",
document: schema.documents.find(attrs.document),
});
});

Expand Down
11 changes: 9 additions & 2 deletions tests/unit/serializers/file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ module("Unit | Serializer | file", function (hooks) {

test("it serializes records", function (assert) {
const store = this.owner.lookup("service:store");
const record = store.createRecord("file", {});
const file = {
name: "foo",
variant: "original",
};
const record = store.createRecord("file", file);

const serializedRecord = record.serialize();

assert.ok(serializedRecord);
assert.deepEqual(Object.fromEntries(serializedRecord.entries()), {
...file,
document: "undefined",
});
});
});

0 comments on commit 420c175

Please sign in to comment.