Skip to content

Commit

Permalink
Extended image checkings (#15)
Browse files Browse the repository at this point in the history
* Extended image checks
  • Loading branch information
EugeneElkin authored and ignatvilesov committed Nov 13, 2017
1 parent 9eff668 commit 764fc75
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 1.4.0
* ADD: isFileImage function was added for checking of files content-type
* UPD: checkIsImageUrlAllowable function was renamed to isImageUrlAllowed and checks was extended

## 1.3.0
* ADD: new module validationHelper and function checkIsImageUrlAllowable for checking of images URLs

Expand Down
12 changes: 12 additions & 0 deletions karma.conf.js
Expand Up @@ -63,6 +63,18 @@ module.exports = (config) => {
pattern: srcOriginalRecursivePath,
included: false,
served: true
},
{
pattern: 'test/images/*.+(png|jpg|gif|svg|bmp)',
watched: false,
included: false,
served: true
},
{
pattern: 'test/data/*.txt',
watched: false,
included: false,
served: true
}
],
preprocessors: {
Expand Down
3 changes: 2 additions & 1 deletion lib/index.d.ts
Expand Up @@ -62,7 +62,8 @@ declare module powerbi.extensibility.utils.dataview {
}
declare module powerbi.extensibility.utils.dataview {
module validationHelper {
function checkIsImageUrlAllowable(url: string): boolean;
function isImageUrlAllowed(url: string): boolean;
function isFileImage(url: string, imageCheckResultCallBack: (isImage: boolean, contentType: string) => void): void;
}
}
declare module powerbi.extensibility.utils.dataview {
Expand Down
26 changes: 21 additions & 5 deletions lib/index.js

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

2 changes: 1 addition & 1 deletion lib/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "powerbi-visuals-utils-dataviewutils",
"version": "1.3.0",
"version": "1.4.0",
"description": "dataviewutils",
"main": "lib/index.js",
"repository": {
Expand Down
28 changes: 24 additions & 4 deletions src/validationHelper.ts
Expand Up @@ -26,10 +26,30 @@

module powerbi.extensibility.utils.dataview {
export module validationHelper {
export function checkIsImageUrlAllowable(url: string): boolean {
// Excludes all URLs that don't contain .gif .jpg .png or .svg extensions.
// Also excludes directives "javascript:" and "data:".
return (/\.(gif|jpg|png|svg)$/i).test(url) && !(/(javascript:|data:)/i).test(url);
export function isImageUrlAllowed(url: string): boolean {
// Excludes all URLs that don't contain .gif .jpg .png or .svg extensions and don't start from "http(s)://".
// Base64 incoded images are allowable too.
return (/^https?:\/\/.+\.(gif|jpg|png|svg)$/i).test(url) || (/^data:image\/(gif|jpeg|png|svg\+xml);base64,/i).test(url);
}

export function isFileImage(url: string, imageCheckResultCallBack: (isImage: boolean, contentType: string) => void) {
let request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState !== this.HEADERS_RECEIVED) {
return;
}

let contentType = request.getResponseHeader("Content-Type"),
supportedTypes = ["image/png", "image/jpeg", "image/gif", "image/svg+xml"];

if (supportedTypes.indexOf(contentType) > -1) {
return imageCheckResultCallBack(true, contentType);
}

return imageCheckResultCallBack(false, contentType);
};
request.open("HEAD", url, true);
request.send();
}
}
}
1 change: 1 addition & 0 deletions test/data/someplaintext.txt
@@ -0,0 +1 @@
Plain text with some data that is used in unit tests
Binary file added test/images/access.bmp
Binary file not shown.
Binary file added test/images/access.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/access.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/access.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/images/justok.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 764fc75

Please sign in to comment.