Skip to content

Commit

Permalink
Merge tag '1.3.1' into develop
Browse files Browse the repository at this point in the history
Better file extension handling
  • Loading branch information
PofMagicfingers committed May 29, 2020
2 parents e5d51a9 + fa2868c commit 7513ca4
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 14 deletions.
17 changes: 12 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
17 changes: 13 additions & 4 deletions src/schema/types/resolvers/enclosure.js
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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"
)
},
Expand Down
76 changes: 72 additions & 4 deletions test/src/schema/types/enclosure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -77,21 +78,88 @@ 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(
new graphql.GraphQLNonNull(graphql.GraphQLString)
)
})

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." +
Expand Down

0 comments on commit 7513ca4

Please sign in to comment.