diff --git a/package.json b/package.json index 7882e917..faee4db6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "podverse-api", - "version": "4.16.11", + "version": "4.16.12", "description": "Data API, database migration scripts, and backend services for all Podverse models.", "contributors": [ "Mitch Downey" diff --git a/src/routes/tools.ts b/src/routes/tools.ts index e3081b7a..a298baab 100644 --- a/src/routes/tools.ts +++ b/src/routes/tools.ts @@ -1,7 +1,8 @@ import * as Router from 'koa-router' import { config } from '~/config' import { emitRouterError } from '~/lib/errors' -import { getFinalRedirectedUrl } from '~/lib/request' +import { getFinalRedirectedUrl, request } from '~/lib/request' +const createError = require('http-errors') const router = new Router({ prefix: `${config.apiPrefix}${config.apiVersion}/tools` }) @@ -28,7 +29,20 @@ const extractHTTPSFromURL = async (url, tries) => { return url } else { const redirectedUrl = await getFinalRedirectedUrl(url) - return redirectedUrl + let finalUrl = redirectedUrl + + if (redirectedUrl.startsWith('http://')) { + try { + const attemptHttpsUrl = redirectedUrl.replace('http://', 'https://') + // If no error is thrown,then assume it is a valid url. + await request(attemptHttpsUrl, { followRedirect: true, method: 'head' }) + finalUrl = attemptHttpsUrl + } catch (error) { + throw new createError.NotFound('Secure URL for ' + url + ' was not found.') + } + } + + return finalUrl } } catch (error) { if (['301', '302', '303', '307', '308'].includes(String(error.statusCode))) { diff --git a/src/services/stats.ts b/src/services/stats.ts index 9f6fff6b..fe1ed1d4 100644 --- a/src/services/stats.ts +++ b/src/services/stats.ts @@ -227,7 +227,7 @@ const generateResetToZeroQueryString = (finalPagePath: string, timeRange, id: st queryString = ` UPDATE "episodes" AS e SET "${TimeRanges[timeRange]}" = 0 - WHERE id = ${id} + WHERE id = '${id}' AND "podcastId" IN ( SELECT p.id @@ -248,7 +248,7 @@ const generateResetToZeroQueryString = (finalPagePath: string, timeRange, id: st queryString = ` UPDATE "episodes" AS e SET "${TimeRanges[timeRange]}" = 0 - WHERE id = ${id} + WHERE id = '${id}' AND "podcastId" IN ( SELECT p.id