1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00

Use js-utility-belt's file utilities

This commit is contained in:
Brett Sun 2016-06-13 14:55:27 +02:00
parent 4784dff694
commit f814a6ceb5

View File

@ -6,22 +6,8 @@ import Moment from 'moment';
import { getLangText } from './lang'; import { getLangText } from './lang';
/** // Re-export related utilities from js-utility-belt for easier access
* Takes a string, creates a text file and returns the URL export { extractFileExtensionFromString, extractFileExtensionFromUrl } from 'js-utility-belt/es6/file';
*
* @param {string} text regular javascript string
* @return {string} regular javascript string
*/
function makeTextFile(text, file) {
let textFileBlob = new Blob([text], {type: 'text/plain'});
let textFile = new File([textFileBlob], 'hash-of-' + file.name + '.txt', {
lastModifiedDate: file.lastModifiedDate,
lastModified: file.lastModified,
type: 'text/plain'
});
return textFile;
}
/** /**
* Takes a file Object, computes the MD5 hash and returns the URL of the textfile with the hash * Takes a file Object, computes the MD5 hash and returns the URL of the textfile with the hash
@ -87,32 +73,18 @@ export function computeHashOfFile(file) {
} }
/** /**
* Extracts a file extension from a string, by splitting by dot and taking * Takes a string, creates a text file and returns the URL
* the last substring
* *
* If a file without an extension is submitted (file), then * @param {string} text regular javascript string
* this method just returns an empty string. * @return {string} regular javascript string
* @param {string} s file's name + extension
* @return {string} file extension
*
* Via: http://stackoverflow.com/a/190878/1263876
*/ */
export function extractFileExtensionFromString(s) { function makeTextFile(text, file) {
const explodedFileName = s.split('.'); const textFileBlob = new Blob([text], { type: 'text/plain' });
return explodedFileName.length > 1 ? explodedFileName.pop() const textFile = new File([textFileBlob], `hash-of-${file.name}.txt`, {
: ''; lastModifiedDate: file.lastModifiedDate,
} lastModified: file.lastModified,
type: 'text/plain'
});
return textFile;
/**
* Extracts a file extension from a url.
*
* If a file without an extension is submitted (file), then
* this method just returns an empty string.
* @param {string} url A url ending in a file
* @return {string} a file extension
*/
export function extractFileExtensionFromUrl(url) {
const fileName = url.split('/').pop();
return extractFileExtensionFromString(fileName);
} }