mirror of
https://github.com/balena-io/etcher.git
synced 2025-04-23 14:57:18 +00:00

This allows selection of images without a secondary file extension (i.e. `example.gz`, compared to `example.img.gz`) by defaulting to `img` in the image-stream handlers, should no secondary extension be found. Further this adjusts `.getPenultimateFileExtension()` to return `null` if the detected penultimate extension is not a known file extension. Change-Type: patch
77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
/*
|
|
* Copyright 2017 resin.io
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
'use strict'
|
|
|
|
const mime = require('mime-types')
|
|
const _ = require('lodash')
|
|
|
|
/**
|
|
* @summary Get the extensions of a file
|
|
* @function
|
|
* @public
|
|
*
|
|
* @param {String} filePath - file path
|
|
* @returns {String[]} extensions
|
|
*
|
|
* @example
|
|
* const extensions = fileExtensions.getFileExtensions('path/to/foo.img.gz');
|
|
* console.log(extensions);
|
|
* > [ 'img', 'gz' ]
|
|
*/
|
|
exports.getFileExtensions = _.memoize((filePath) => {
|
|
return _.chain(filePath)
|
|
.split('.')
|
|
.tail()
|
|
.map(_.toLower)
|
|
.value()
|
|
})
|
|
|
|
/**
|
|
* @summary Get the last file extension
|
|
* @function
|
|
* @public
|
|
*
|
|
* @param {String} filePath - file path
|
|
* @returns {(String|Null)} last extension
|
|
*
|
|
* @example
|
|
* const extension = fileExtensions.getLastFileExtension('path/to/foo.img.gz');
|
|
* console.log(extension);
|
|
* > 'gz'
|
|
*/
|
|
exports.getLastFileExtension = (filePath) => {
|
|
return _.last(exports.getFileExtensions(filePath)) || null
|
|
}
|
|
|
|
/**
|
|
* @summary Get the penultimate file extension
|
|
* @function
|
|
* @public
|
|
*
|
|
* @param {String} filePath - file path
|
|
* @returns {(String|Null)} penultimate extension
|
|
*
|
|
* @example
|
|
* const extension = fileExtensions.getPenultimateFileExtension('path/to/foo.img.gz');
|
|
* console.log(extension);
|
|
* > 'img'
|
|
*/
|
|
exports.getPenultimateFileExtension = (filePath) => {
|
|
const ext = _.last(_.initial(exports.getFileExtensions(filePath)))
|
|
return !_.isNil(ext) && mime.lookup(ext) ? ext : null
|
|
}
|