|
1 | 1 | const _ = require('lodash'); |
2 | 2 | const path = require('path'); |
| 3 | +const url = require('url'); |
3 | 4 | const utils = require('../utils'); |
4 | 5 | const resourceTypes = require('../config/resource-types'); |
5 | 6 | const resourceTypeExtensions = require('../config/resource-ext-by-type'); |
6 | 7 |
|
7 | 8 | module.exports = function generateFilename (resource, {defaultFilename}) { |
8 | 9 | const resourceUrl = resource.getUrl(); |
9 | 10 | const host = utils.getHostFromUrl(resourceUrl); |
| 11 | + const urlParsed = url.parse(resourceUrl); |
10 | 12 | let filePath = utils.getFilepathFromUrl(resourceUrl); |
11 | 13 | const extension = utils.getFilenameExtension(filePath); |
12 | 14 |
|
13 | 15 | filePath = path.join(host.replace(':', '_'), filePath); |
14 | 16 |
|
| 17 | + // If have query string |
| 18 | + if (urlParsed.query) { |
| 19 | + const parsed = path.parse(filePath); |
| 20 | + const basename = path.join(parsed.dir, parsed.name); |
| 21 | + // Use the query string as file name in the site structure directory |
| 22 | + if (!extension) { |
| 23 | + // Without extension: http://example.com/path?q=test => path/q=test |
| 24 | + filePath = `${basename}${path.sep}${urlParsed.query}`; |
| 25 | + } else { |
| 26 | + // With extension: http://example.com/path/picture.png?q=test => path/picture_q=test.png |
| 27 | + filePath = `${basename}_${urlParsed.query}${extension}`; |
| 28 | + } |
| 29 | + } |
| 30 | + |
15 | 31 | // If we have HTML from 'http://example.com/path' => set 'path/index.html' as filepath |
16 | 32 | if (resource.isHtml()) { |
17 | 33 | const htmlExtensions = resourceTypeExtensions[resourceTypes.html]; |
18 | 34 | const resourceHasHtmlExtension = _.includes(htmlExtensions, extension); |
19 | 35 | // add index.html only if filepath has ext != html '/path/test.com' => '/path/test.com/index.html' |
20 | 36 | if (!resourceHasHtmlExtension) { |
21 | | - filePath = path.join(filePath, defaultFilename); |
| 37 | + if (!urlParsed.query) { |
| 38 | + // Without query string: http://example.com/path => path/index.html |
| 39 | + filePath = path.join(filePath, defaultFilename); |
| 40 | + } else { |
| 41 | + // With query string: http://example.com/path?q=test => path/q=test.html |
| 42 | + filePath = `${filePath}.html`; |
| 43 | + } |
22 | 44 | } |
23 | 45 | } |
24 | 46 |
|
|
0 commit comments