Commit 9f741f53 authored by lionel.marie-louise's avatar lionel.marie-louise
Browse files

Merge branch 'fix/18034/20.10_TMA1' into '20.10_TMA1'

Fix/18034/20.10 tma1

See merge request maarch/MaarchCourrier!400
parents d71508e1 f63f06c6
......@@ -15,6 +15,6 @@
<img src="assets/spinner.gif"/>
</div>
</app-root>
<script src="runtime-es2015.4ac42fef445445e6f3d0.js" type="module"></script><script src="runtime-es5.4ac42fef445445e6f3d0.js" nomodule defer></script><script src="polyfills-es5.8c2a86db22bc37211976.js" nomodule defer></script><script src="polyfills-es2015.f8f86766446f4344eec7.js" type="module"></script><script src="scripts.3f995a62571f9f668311.js" defer></script><script src="main-es2015.f030bc77c34aad9f93f3.js" type="module"></script><script src="main-es5.f030bc77c34aad9f93f3.js" nomodule defer></script></body>
<script src="runtime-es2015.4ac42fef445445e6f3d0.js" type="module"></script><script src="runtime-es5.4ac42fef445445e6f3d0.js" nomodule defer></script><script src="polyfills-es5.8c2a86db22bc37211976.js" nomodule defer></script><script src="polyfills-es2015.f8f86766446f4344eec7.js" type="module"></script><script src="scripts.3f995a62571f9f668311.js" defer></script><script src="main-es2015.02358cbb72b0e882be43.js" type="module"></script><script src="main-es5.02358cbb72b0e882be43.js" nomodule defer></script></body>
</html>
\ No newline at end of file
......@@ -268,6 +268,9 @@ $app->delete('/folders/{id}/unpin', \Folder\controllers\FolderController::class
// CommitInformation
$app->get('/commitInformation', \SrcCore\controllers\CoreController::class . ':getGitCommitInformation');
// File Utils
$app->post('/mimeAndSize', \SrcCore\controllers\CoreController::class . ':getMimeTypeAndFileSizeREST');
//Groups
$app->get('/groups', \Group\controllers\GroupController::class . ':get');
$app->post('/groups', \Group\controllers\GroupController::class . ':create');
......
......@@ -195,4 +195,58 @@ class CoreController
}
return $response->withJson(['langs' => $arrLanguages]);
}
public static function getMimeTypeAndFileSize(array $args)
{
ValidatorModel::stringType($args, ['encodedFile', 'path']);
if (empty($args['encodedFile']) && empty($args['path'])) {
return ['errors' => 'args needs one of encodedFile or path'];
}
$resource = null;
$size = null;
if (!empty($args['encodedFile'])) {
$resource = fopen('php://temp', 'r+');
$streamFilterBase64 = stream_filter_append($resource, 'convert.base64-decode', STREAM_FILTER_WRITE);
stream_set_chunk_size($resource, 1024*1024);
$size = fwrite($resource, $args['encodedFile']);
stream_filter_remove($streamFilterBase64);
} elseif (!empty($args['path'])) {
if (!is_file($args['path']) || !is_readable($args['path'])) {
return ['errors' => 'args filename does not refer to a regular file or said file is not readable'];
}
$resource = fopen($args['path'], 'r');
$size = filesize($args['path']);
}
if (empty($resource)) {
return ['errors' => 'could not decode encoded data, or open target file'];
}
rewind($resource);
$mimeType = mime_content_type($resource);
$encoding = null;
if (substr($mimeType, 0, 5) === 'text/') {
$encoding = mb_detect_encoding(stream_get_contents($resource));
}
rewind($resource);
fclose($resource);
if (empty($mimeType) || empty($size)) {
return ['errors' => "could not compute mime type ($mimeType) or file size ($size)"];
}
return ['mime' => $mimeType, 'size' => $size, 'encoding' => $encoding];
}
public static function getMimeTypeAndFileSizeREST(Request $request, Response $response)
{
$body = $request->getParsedBody();
// only send encodedFile and not path, to prevent clients from reading data about server files
$result = CoreController::getMimeTypeAndFileSize(['encodedFile' => $body['encodedFile']]);
if (!empty($result['errors'])) {
return $response->withStatus(400)->withJson($result);
}
return $response->withStatus(200)->withJson($result);
}
}
......@@ -21,6 +21,9 @@ import { Papa } from 'ngx-papaparse';
export class ContactImportComponent implements OnInit {
loading: boolean = false;
mimeAllowed: boolean = false;
encoding: string = '';
contactColumns: any[] = [
{
......@@ -192,45 +195,53 @@ export class ContactImportComponent implements OnInit {
}
uploadCsv(fileInput: any) {
if (fileInput.target.files && fileInput.target.files[0] && (fileInput.target.files[0].type === 'text/csv' || fileInput.target.files[0].type === 'application/vnd.ms-excel')) {
this.loading = true;
let rawCsv = [];
const reader = new FileReader();
reader.readAsText(fileInput.target.files[0]);
reader.onload = (value: any) => {
this.papa.parse(value.target.result, {
complete: (result) => {
rawCsv = result.data;
rawCsv = rawCsv.filter(data => data.length === rawCsv[0].length);
let dataCol = [];
let objData = {};
this.setCsvColumns(rawCsv[0]);
this.countAll = this.hasHeader ? rawCsv.length - 1 : rawCsv.length;
for (let index = 0; index < rawCsv.length; index++) {
objData = {};
dataCol = rawCsv[index];
dataCol.forEach((element: any, index2: number) => {
objData[this.csvColumns[index2]] = element;
});
this.csvData.push(objData);
const file: any = fileInput.target.files[0];
const reader = new FileReader();
let encodedFile: string = '';
reader.onload = (async () => {
const result: string = reader.result as string;
encodedFile = result.substring(result.indexOf('base64'));
encodedFile = encodedFile.split(',')[1];
await this.checkMimeType(encodedFile);
if (fileInput.target.files && fileInput.target.files[0] && this.mimeAllowed) {
this.loading = true;
let rawCsv = [];
reader.readAsText(fileInput.target.files[0], this.functionsService.empty(this.encoding) ? 'ISO-8859-1' : this.encoding);
reader.onload = (value: any) => {
this.papa.parse(value.target.result, {
complete: (result) => {
rawCsv = result.data;
rawCsv = rawCsv.filter(data => data.length === rawCsv[0].length);
let dataCol = [];
let objData = {};
this.setCsvColumns(rawCsv[0]);
this.countAll = this.hasHeader ? rawCsv.length - 1 : rawCsv.length;
for (let index = 0; index < rawCsv.length; index++) {
objData = {};
dataCol = rawCsv[index];
dataCol.forEach((element: any, index2: number) => {
objData[this.csvColumns[index2]] = element;
});
this.csvData.push(objData);
}
this.initData();
this.countAdd = this.csvData.filter((data: any, index: number) => index > 0 && this.functionsService.empty(data[this.associatedColmuns['id']])).length;
this.countUp = this.csvData.filter((data: any, index: number) => index > 0 && !this.functionsService.empty(data[this.associatedColmuns['id']])).length;
this.loading = false;
}
this.initData();
this.countAdd = this.csvData.filter((data: any, index: number) => index > 0 && this.functionsService.empty(data[this.associatedColmuns['id']])).length;
this.countUp = this.csvData.filter((data: any, index: number) => index > 0 && !this.functionsService.empty(data[this.associatedColmuns['id']])).length;
this.loading = false;
}
});
};
} else {
this.dialog.open(AlertComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: this.translate.instant('lang.notAllowedExtension') + ' !', msg: this.translate.instant('lang.file') + ' : <b>' + fileInput.target.files[0].name + '</b>, ' + this.translate.instant('lang.type') + ' : <b>' + fileInput.target.files[0].type + '</b><br/><br/><u>' + this.translate.instant('lang.allowedExtensions') + '</u> : <br/>' + 'text/csv' } });
}
});
};
} else {
this.dialog.open(AlertComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: this.translate.instant('lang.notAllowedExtension') + ' !', msg: this.translate.instant('lang.file') + ' : <b>' + fileInput.target.files[0].name + '</b>, ' + this.translate.instant('lang.type') + ' : <b>' + fileInput.target.files[0].type + '</b><br/><br/><u>' + this.translate.instant('lang.allowedExtensions') + '</u> : <br/>' + 'text/csv' } });
}
});
reader.readAsDataURL(file);
}
setCsvColumns(headerData: string[] = null) {
......@@ -344,4 +355,21 @@ export class ContactImportComponent implements OnInit {
})
).subscribe();
}
checkMimeType(base64: string) {
return new Promise((resolve) => {
this.http.post('../rest/mimeAndSize', {encodedFile: base64}).pipe(
tap((data: any) => {
this.mimeAllowed = data.mime === 'text/plain' ? true : false;
this.encoding = data.encoding;
resolve(true);
}),
catchError((err: any) => {
this.mimeAllowed = false;
this.notify.handleSoftErrors(err);
return of (false);
})
).subscribe();
});
}
}
......@@ -32,6 +32,8 @@ export class UsersImportComponent implements OnInit {
csvColumns: string[] = [];
encoding: string = '';
associatedColmuns: any = {};
dataSource = new MatTableDataSource(null);
hasHeader: boolean = true;
......@@ -41,6 +43,8 @@ export class UsersImportComponent implements OnInit {
countAdd: number = 0;
countUp: number = 0;
mimeAllowed: boolean = false;
@ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;
constructor(
......@@ -84,46 +88,53 @@ export class UsersImportComponent implements OnInit {
}
uploadCsv(fileInput: any) {
if (fileInput.target.files && fileInput.target.files[0] && (fileInput.target.files[0].type === 'text/csv' || fileInput.target.files[0].type === 'application/vnd.ms-excel')) {
this.loading = true;
let rawCsv = [];
const reader = new FileReader();
reader.readAsText(fileInput.target.files[0], 'ISO-8859-1');
reader.onload = (value: any) => {
this.papa.parse(value.target.result, {
complete: (result) => {
rawCsv = result.data;
rawCsv = rawCsv.filter(data => data.length === rawCsv[0].length);
let dataCol = [];
let objData = {};
this.setCsvColumns(rawCsv[0]);
this.countAll = this.hasHeader ? rawCsv.length - 1 : rawCsv.length;
for (let index = 0; index < rawCsv.length; index++) {
objData = {};
dataCol = rawCsv[index];
dataCol.forEach((element: any, index2: number) => {
objData[this.csvColumns[index2]] = element;
});
this.csvData.push(objData);
const file: any = fileInput.target.files[0];
const reader = new FileReader();
let encodedFile: string = '';
reader.onload = (async () => {
const result: string = reader.result as string;
encodedFile = result.substring(result.indexOf('base64'));
encodedFile = encodedFile.split(',')[1];
await this.checkMimeType(encodedFile);
if (fileInput.target.files && fileInput.target.files[0] && this.mimeAllowed) {
this.loading = true;
let rawCsv = [];
reader.readAsText(fileInput.target.files[0], this.functionsService.empty(this.encoding) ? 'ISO-8859-1' : this.encoding);
reader.onload = (value: any) => {
this.papa.parse(value.target.result, {
complete: (result) => {
rawCsv = result.data;
rawCsv = rawCsv.filter(data => data.length === rawCsv[0].length);
let dataCol = [];
let objData = {};
this.setCsvColumns(rawCsv[0]);
this.countAll = this.hasHeader ? rawCsv.length - 1 : rawCsv.length;
for (let index = 0; index < rawCsv.length; index++) {
objData = {};
dataCol = rawCsv[index];
dataCol.forEach((element: any, index2: number) => {
objData[this.csvColumns[index2]] = element;
});
this.csvData.push(objData);
}
this.initData();
this.countAdd = this.csvData.filter((data: any, index: number) => index > 0 && this.functionsService.empty(data[this.associatedColmuns['id']])).length;
this.countUp = this.csvData.filter((data: any, index: number) => index > 0 && !this.functionsService.empty(data[this.associatedColmuns['id']])).length;
this.loading = false;
}
this.initData();
this.countAdd = this.csvData.filter((data: any, index: number) => index > 0 && this.functionsService.empty(data[this.associatedColmuns['id']])).length;
this.countUp = this.csvData.filter((data: any, index: number) => index > 0 && !this.functionsService.empty(data[this.associatedColmuns['id']])).length;
this.loading = false;
}
});
};
} else {
this.dialog.open(AlertComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: this.translate.instant('lang.notAllowedExtension') + ' !', msg: this.translate.instant('lang.file') + ' : <b>' + fileInput.target.files[0].name + '</b>, ' + this.translate.instant('lang.type') + ' : <b>' + fileInput.target.files[0].type + '</b><br/><br/><u>' + this.translate.instant('lang.allowedExtensions') + '</u> : <br/>' + 'text/csv' } });
}
});
};
} else {
this.dialog.open(AlertComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: this.translate.instant('lang.notAllowedExtension') + ' !', msg: this.translate.instant('lang.file') + ' : <b>' + fileInput.target.files[0].name + '</b>, ' + this.translate.instant('lang.type') + ' : <b>' + fileInput.target.files[0].type + '</b><br/><br/><u>' + this.translate.instant('lang.allowedExtensions') + '</u> : <br/>' + 'text/csv' } });
}
});
reader.readAsDataURL(file);
}
setCsvColumns(headerData: string[] = null) {
......@@ -242,4 +253,21 @@ export class UsersImportComponent implements OnInit {
})
).subscribe();
}
checkMimeType(base64: string) {
return new Promise((resolve) => {
this.http.post('../rest/mimeAndSize', {encodedFile: base64}).pipe(
tap((data: any) => {
this.mimeAllowed = data.mime === 'text/plain' ? true : false;
this.encoding = data.encoding;
resolve(true);
}),
catchError((err: any) => {
this.mimeAllowed = false;
this.notify.handleSoftErrors(err);
return of (false);
})
).subscribe();
});
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment