From 9b70ddb1b4c990e49c821b9237177f015d05ee75 Mon Sep 17 00:00:00 2001 From: Pof Magicfingers Date: Fri, 29 May 2020 10:01:10 +0200 Subject: [PATCH 1/2] fix: add better extension handling --- package-lock.json | 15 +++-- package.json | 1 + src/schema/types/resolvers/enclosure.js | 17 ++++-- test/src/schema/types/enclosure.test.js | 76 +++++++++++++++++++++++-- 4 files changed, 97 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index dd790e81..daa46c86 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5754,11 +5754,18 @@ "integrity": "sha1-dMZD2i3Z1qRTmZY0ZbJtXKfXHwE=" }, "mime-types": { - "version": "2.1.17", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.17.tgz", - "integrity": "sha1-Cdejk/A+mVp5+K+Fe3Cp4KsWVXo=", + "version": "2.1.27", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz", + "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==", "requires": { - "mime-db": "~1.30.0" + "mime-db": "1.44.0" + }, + "dependencies": { + "mime-db": { + "version": "1.44.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz", + "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==" + } } }, "mimic-fn": { diff --git a/package.json b/package.json index f96961c2..637d0e79 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "graphql-date": "^1.0.3", "graphql-tools": "^2.0.0", "marked": "^0.3.12", + "mime-types": "^2.1.27", "moment": "^2.18.1", "mongoose": "^4.4.12", "mongoose-fixture-loader": "^1.0.2", diff --git a/src/schema/types/resolvers/enclosure.js b/src/schema/types/resolvers/enclosure.js index c262f794..3cebcfc6 100644 --- a/src/schema/types/resolvers/enclosure.js +++ b/src/schema/types/resolvers/enclosure.js @@ -1,14 +1,20 @@ import path from "path" +import mime from "mime-types" +import { empty } from "~/utils" const Enclosure = { duration(enclosure) { return Math.max(+enclosure.duration_in_seconds, 0) || 0 }, size(enclosure) { - return parseInt(enclosure.length, 10) + return parseInt(enclosure.length, 10) || 0 }, type(enclosure) { - return enclosure.media_type == "mp3" ? "audio/mpeg" : enclosure.mime_type + return ( + mime.lookup(enclosure.media_type) || + enclosure.mime_type || + "application/octet-stream" + ) }, url(enclosure, args, ctx) { return ( @@ -19,8 +25,11 @@ const Enclosure = { "/" + enclosure.item._slugs[enclosure.item._slugs.length - 1] + "/enclosure." + - +(enclosure.item.updated_at / 1000) + - path.extname(`${enclosure.filename}`).replace(/(.*)\?.*$/, "$1") + + Math.round(enclosure.item.updated_at / 1000) + + ((!empty(enclosure.media_type) + ? `.${enclosure.media_type}` + : path.extname(`${enclosure.filename}`).replace(/(.*)\?.*$/, "$1")) || + ".mp3") + "?p=f" ) }, diff --git a/test/src/schema/types/enclosure.test.js b/test/src/schema/types/enclosure.test.js index bb4a00ea..5eb37f4f 100644 --- a/test/src/schema/types/enclosure.test.js +++ b/test/src/schema/types/enclosure.test.js @@ -18,8 +18,9 @@ describe("Enclosure Graph Object", () => { }, duration_in_seconds: 600, length: "123521", + media_type: null, mime_type: "audio/mpeg+test", - filename: "afile.mp3", + filename: "afile.ext", meta_url: { path: "http://anurl.test/afile.mp3?p=f" } @@ -77,12 +78,31 @@ describe("Enclosure Graph Object", () => { ).to.equals("audio/mpeg") }) - it("should resolve type", () => { + it("should resolve stored mime-type with unknown media type", () => { + expect( + enclosureFields.type.resolve({ + ...enclosureObject, + media_type: "unknown" + }) + ).to.equals(enclosureObject.mime_type) + }) + + it("should resolve stored mime-type when media_type is null or undefined", () => { expect(enclosureFields.type.resolve(enclosureObject)).to.equals( "audio/mpeg+test" ) }) + it("should resolve default mime-type with unknown media type and no stored mime-type", () => { + expect( + enclosureFields.type.resolve({ + ...enclosureObject, + media_type: "unknown", + mime_type: null + }) + ).to.equals("application/octet-stream") + }) + it("should include a required string url", () => { expect(enclosureFields).to.have.property("url") expect(enclosureFields.url.type).to.deep.equals( @@ -90,8 +110,56 @@ describe("Enclosure Graph Object", () => { ) }) - it("should resolve url", () => { - expect(enclosureFields.url.resolve(enclosureObject, {}, context)).to.equals( + it("should resolve url with correct media_type", () => { + expect( + enclosureFields.url.resolve( + { + ...enclosureObject, + media_type: "pouet" + }, + {}, + context + ) + ).to.equals( + "https://" + + context.hosts.stats + + "/blog-de-toto/tata/enclosure." + + 1337 + + ".pouet?p=f" + ) + }) + + it("should resolve url using filename ext with incorrect media_type", () => { + expect( + enclosureFields.url.resolve( + { + ...enclosureObject, + media_type: null + }, + {}, + context + ) + ).to.equals( + "https://" + + context.hosts.stats + + "/blog-de-toto/tata/enclosure." + + 1337 + + ".ext?p=f" + ) + }) + + it("should resolve url using default ext with incorrect media_type and filename", () => { + expect( + enclosureFields.url.resolve( + { + ...enclosureObject, + media_type: null, + filename: null + }, + {}, + context + ) + ).to.equals( "https://" + context.hosts.stats + "/blog-de-toto/tata/enclosure." + From 2858949bf363ec30890fded1b1a954918d81c31f Mon Sep 17 00:00:00 2001 From: Pof Magicfingers Date: Fri, 29 May 2020 10:01:55 +0200 Subject: [PATCH 2/2] chore: bump version 1.3.1 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index daa46c86..21cc85d3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "podcloud-feeds", - "version": "1.3.0", + "version": "1.3.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 637d0e79..1f9c7e39 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "podcloud-feeds", - "version": "1.3.0", + "version": "1.3.1", "description": "GraphQL API for podcloud", "scripts": { "test": "NODE_ENV=test mocha --opts test/mocha.opts",