Updating Angular Version to 6
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
{
|
||||
"project": {
|
||||
"name": "ng2-chunk-file-upload"
|
||||
},
|
||||
"apps": [
|
||||
{
|
||||
"root": "demo/src",
|
||||
"outDir": "demo/dist",
|
||||
"assets": ["assets"],
|
||||
"index": "index.html",
|
||||
"main": "main.ts",
|
||||
"test": "../../scripts/test.ts",
|
||||
"tsconfig": "tsconfig.json",
|
||||
"prefix": "",
|
||||
"mobile": false,
|
||||
"styles": [],
|
||||
"scripts": [],
|
||||
"environmentSource": "environments/environment.ts",
|
||||
"environments": {
|
||||
"dev": "environments/environment.ts",
|
||||
"prod": "environments/environment.prod.ts"
|
||||
}
|
||||
}
|
||||
],
|
||||
"addons": [],
|
||||
"packages": [],
|
||||
"e2e": {
|
||||
"protractor": {
|
||||
"config": "protractor.conf.js"
|
||||
}
|
||||
},
|
||||
"test": {
|
||||
"karma": {
|
||||
"config": "karma.conf.js"
|
||||
}
|
||||
},
|
||||
"defaults": {
|
||||
"styleExt": "css",
|
||||
"prefixInterfaces": false,
|
||||
"inline": {
|
||||
"style": false,
|
||||
"template": false
|
||||
},
|
||||
"spec": {
|
||||
"class": false,
|
||||
"component": true,
|
||||
"directive": true,
|
||||
"module": false,
|
||||
"pipe": true,
|
||||
"service": true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
# http://editorconfig.org
|
||||
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
114
angular.json
Normal file
114
angular.json
Normal file
@@ -0,0 +1,114 @@
|
||||
{
|
||||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
||||
"version": 1,
|
||||
"newProjectRoot": "projects",
|
||||
"projects": {
|
||||
"ng2-chunk-file-upload": {
|
||||
"root": "",
|
||||
"sourceRoot": "src",
|
||||
"projectType": "application",
|
||||
"architect": {
|
||||
"build": {
|
||||
"builder": "@angular-devkit/build-angular:browser",
|
||||
"options": {
|
||||
"outputPath": "demo/dist",
|
||||
"index": "demo/src/index.html",
|
||||
"main": "demo/src/main.ts",
|
||||
"tsConfig": "demo/src/tsconfig.json",
|
||||
"assets": [
|
||||
"demo/src/assets"
|
||||
],
|
||||
"styles": [],
|
||||
"scripts": []
|
||||
},
|
||||
"configurations": {
|
||||
"production": {
|
||||
"optimization": true,
|
||||
"outputHashing": "all",
|
||||
"sourceMap": false,
|
||||
"extractCss": true,
|
||||
"namedChunks": false,
|
||||
"aot": true,
|
||||
"extractLicenses": true,
|
||||
"vendorChunk": false,
|
||||
"buildOptimizer": true,
|
||||
"fileReplacements": [
|
||||
{
|
||||
"replace": "demo/src/environments/environment.ts",
|
||||
"with": "demo/src/environments/environment.prod.ts"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"serve": {
|
||||
"builder": "@angular-devkit/build-angular:dev-server",
|
||||
"options": {
|
||||
"browserTarget": "ng2-chunk-file-upload:build"
|
||||
},
|
||||
"configurations": {
|
||||
"production": {
|
||||
"browserTarget": "ng2-chunk-file-upload:build:production"
|
||||
}
|
||||
}
|
||||
},
|
||||
"extract-i18n": {
|
||||
"builder": "@angular-devkit/build-angular:extract-i18n",
|
||||
"options": {
|
||||
"browserTarget": "ng2-chunk-file-upload:build"
|
||||
}
|
||||
},
|
||||
"test": {
|
||||
"builder": "@angular-devkit/build-angular:karma",
|
||||
"options": {
|
||||
"main": "demo/src/../../scripts/test.ts",
|
||||
"karmaConfig": "karma.conf.js",
|
||||
"scripts": [],
|
||||
"styles": [],
|
||||
"assets": [
|
||||
"demo/src/assets"
|
||||
]
|
||||
}
|
||||
},
|
||||
"lint": {
|
||||
"builder": "@angular-devkit/build-angular:tslint",
|
||||
"options": {
|
||||
"tsConfig": [],
|
||||
"exclude": []
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"ng2-chunk-file-upload-e2e": {
|
||||
"root": "",
|
||||
"sourceRoot": "",
|
||||
"projectType": "application",
|
||||
"architect": {
|
||||
"e2e": {
|
||||
"builder": "@angular-devkit/build-angular:protractor",
|
||||
"options": {
|
||||
"protractorConfig": "protractor.conf.js",
|
||||
"devServerTarget": "ng2-chunk-file-upload:serve"
|
||||
}
|
||||
},
|
||||
"lint": {
|
||||
"builder": "@angular-devkit/build-angular:tslint",
|
||||
"options": {
|
||||
"tsConfig": [],
|
||||
"exclude": []
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultProject": "ng2-chunk-file-upload",
|
||||
"schematics": {
|
||||
"@schematics/angular:component": {
|
||||
"prefix": "",
|
||||
"styleext": "css"
|
||||
},
|
||||
"@schematics/angular:directive": {
|
||||
"prefix": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,16 +36,16 @@ let gettingStarted = require('html-loader!markdown-loader!../getting-started.md'
|
||||
is maintained by <a href="https://github.com/valor-software">valor-software</a>.</p>
|
||||
</div>
|
||||
</footer>
|
||||
`
|
||||
`,
|
||||
})
|
||||
export class AppComponent {
|
||||
public gettingStarted: string = gettingStarted;
|
||||
public ngAfterContentInit(): any {
|
||||
setTimeout(() => {
|
||||
if (typeof PR !== 'undefined') {
|
||||
// google code-prettify
|
||||
PR.prettyPrint();
|
||||
}
|
||||
if (typeof PR !== 'undefined') {
|
||||
// google code-prettify
|
||||
PR.prettyPrint();
|
||||
}
|
||||
}, 150);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,17 @@ import { FormsModule } from '@angular/forms';
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
|
||||
import { TabsModule } from 'ngx-bootstrap/tabs';
|
||||
import { FileUploadModule } from 'ng2-chunk-file-upload';
|
||||
import { FileUploadModule } from '../../../src/file-upload/file-upload.module';
|
||||
|
||||
import { AppComponent } from './app.component';
|
||||
|
||||
import { FileUploadSectionComponent } from './components/file-upload-section';
|
||||
import { SimpleDemoComponent } from './components/file-upload/simple-demo';
|
||||
import { HttpModule } from '@angular/http';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
HttpModule,
|
||||
BrowserModule,
|
||||
CommonModule,
|
||||
FileUploadModule,
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
import { Component } from "@angular/core";
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
let doc = require("html-loader!markdown-loader!../../doc.md");
|
||||
let doc = require('html-loader!markdown-loader!../../doc.md');
|
||||
|
||||
let tabDesc: Array<any> = [
|
||||
{
|
||||
heading: 'Simple',
|
||||
ts: require("!!raw-loader?lang=typescript!./file-upload/simple-demo.ts"),
|
||||
html: require("!!raw-loader?lang=markup!./file-upload/simple-demo.html"),
|
||||
js: require("!!raw-loader?lang=javascript!./file-upload/file-catcher.js")
|
||||
},
|
||||
{
|
||||
heading: 'Simple',
|
||||
ts: require('!!raw-loader?lang=typescript!./file-upload/simple-demo.ts'),
|
||||
html: require('!!raw-loader?lang=markup!./file-upload/simple-demo.html'),
|
||||
js: require('!!raw-loader?lang=javascript!./file-upload/file-catcher.js')
|
||||
}
|
||||
];
|
||||
|
||||
@Component({
|
||||
selector: "file-upload-section",
|
||||
templateUrl: "./file-upload-section.html",
|
||||
selector: 'file-upload-section',
|
||||
templateUrl: './file-upload-section.html'
|
||||
})
|
||||
export class FileUploadSectionComponent {
|
||||
public name: string = 'File Upload';
|
||||
public currentHeading: string = 'Simple';
|
||||
public doc: string = doc;
|
||||
public tabs: any = tabDesc;
|
||||
public name: string = 'File Upload';
|
||||
public currentHeading: string = 'Simple';
|
||||
public doc: string = doc;
|
||||
public tabs: any = tabDesc;
|
||||
|
||||
public select(e: any): void {
|
||||
if (e.heading) {
|
||||
this.currentHeading = e.heading;
|
||||
}
|
||||
}
|
||||
public select(e: any): void {
|
||||
if (e.heading) {
|
||||
this.currentHeading = e.heading;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"declaration": false,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"noEmitHelpers": false,
|
||||
"lib": ["es6", "dom"],
|
||||
"types": [
|
||||
"jasmine",
|
||||
"webpack"
|
||||
],
|
||||
"mapRoot": "./",
|
||||
"module": "es6",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../temp/out-tsc",
|
||||
"sourceMap": true,
|
||||
"target": "es5",
|
||||
"typeRoots": [
|
||||
"../node_modules/@types"
|
||||
]
|
||||
}
|
||||
"compilerOptions": {
|
||||
"declaration": false,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"noEmitHelpers": false,
|
||||
"lib": ["es6", "dom"],
|
||||
"types": ["jasmine", "webpack"],
|
||||
"mapRoot": "./",
|
||||
"module": "es6",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../temp/out-tsc",
|
||||
"sourceMap": true,
|
||||
"target": "es5",
|
||||
"typeRoots": ["../node_modules/@types"]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,21 +6,21 @@ const customLaunchers = require('./scripts/sauce-browsers').customLaunchers;
|
||||
module.exports = function (config) {
|
||||
const configuration = {
|
||||
basePath: '',
|
||||
frameworks: ['jasmine', '@angular/cli'],
|
||||
frameworks: ['jasmine', '@angular-devkit/build-angular'],
|
||||
plugins: [
|
||||
require('karma-jasmine'),
|
||||
require('karma-chrome-launcher'),
|
||||
require('karma-coverage-istanbul-reporter'),
|
||||
require('@angular/cli/plugins/karma')
|
||||
require('@angular-devkit/build-angular/plugins/karma')
|
||||
],
|
||||
files: [
|
||||
{pattern: './scripts/test.ts', watched: false}
|
||||
],
|
||||
preprocessors: {
|
||||
'./scripts/test.ts': ['@angular/cli']
|
||||
'./scripts/test.ts': ['@angular-devkit/build-angular']
|
||||
},
|
||||
coverageIstanbulReporter: {
|
||||
reports: [ 'html', 'lcovonly' ],
|
||||
dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ],
|
||||
fixWebpackSourcePaths: false
|
||||
},
|
||||
angularCli: {
|
||||
|
||||
10202
package-lock.json
generated
10202
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
123
package.json
123
package.json
@@ -31,9 +31,9 @@
|
||||
"angularjs",
|
||||
"twitter-bootstrap",
|
||||
"file-upload",
|
||||
"angular-file-upload",
|
||||
"chunk-file-upload",
|
||||
"azure-blob-storage"
|
||||
"angular-file-upload",
|
||||
"chunk-file-upload",
|
||||
"azure-blob-storage"
|
||||
],
|
||||
"author": "Paulo Peres Jr <paulo@myog.io>",
|
||||
"license": "MIT",
|
||||
@@ -47,75 +47,82 @@
|
||||
"homepage": "https://github.com/PauloPeres/ng2-chunk-file-upload#readme",
|
||||
"dependencies": {},
|
||||
"peerDependencies": {
|
||||
"@angular/common": "^2.3.1 || >=4.0.0",
|
||||
"@angular/core": "^2.3.1 || >=4.0.0"
|
||||
"@angular/common": "^4.3.0 || >=4.3.0",
|
||||
"@angular/core": "^4.3.0 || >=4.3.0",
|
||||
"@angular/http": "^4.3.0 || >=4.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@angular/cli": "1.0.0",
|
||||
"@angular/common": "2.4.3",
|
||||
"@angular/compiler": "2.4.3",
|
||||
"@angular/compiler-cli": "2.4.3",
|
||||
"@angular/core": "2.4.3",
|
||||
"@angular/forms": "2.4.3",
|
||||
"@angular/http": "2.4.3",
|
||||
"@angular/language-service": "2.4.3",
|
||||
"@angular/platform-browser": "2.4.3",
|
||||
"@angular/platform-browser-dynamic": "2.4.3",
|
||||
"@angular/router": "3.4.3",
|
||||
"@angular/tsc-wrapped": "0.5.1",
|
||||
"@types/jasmine": "2.5.40",
|
||||
"@types/marked": "0.0.28",
|
||||
"@types/node": "7.0.0",
|
||||
"@types/webpack": "^2.2.1",
|
||||
"bootstrap": "3.3.7",
|
||||
"@angular-devkit/build-angular": "~0.6.3",
|
||||
"@angular/cdk": "^6.1.0",
|
||||
"@angular/cli": "^6.0.3",
|
||||
"@angular/common": "^6.0.1",
|
||||
"@angular/compiler": "^6.0.1",
|
||||
"@angular/compiler-cli": "^6.0.1",
|
||||
"@angular/core": "^6.0.1",
|
||||
"@angular/forms": "^6.0.1",
|
||||
"@angular/http": "^6.0.1",
|
||||
"@angular/language-service": "^6.0.1",
|
||||
"@angular/material": "^6.1.0",
|
||||
"@angular/platform-browser": "^6.0.1",
|
||||
"@angular/platform-browser-dynamic": "^6.0.1",
|
||||
"@angular/router": "^6.0.1",
|
||||
"@angular/tsc-wrapped": "4.4.6",
|
||||
"@types/jasmine": "2.8.7",
|
||||
"@types/marked": "0.3.0",
|
||||
"@types/node": "10.1.2",
|
||||
"@types/webpack": "^4.4.0",
|
||||
"ajv": "^6.0.0",
|
||||
"bootstrap": "4.1.1",
|
||||
"chokidar-cli": "1.2.0",
|
||||
"classlist-polyfill": "1.0.3",
|
||||
"codecov": "1.0.1",
|
||||
"codelyzer": "~2.0.0-beta.4",
|
||||
"conventional-changelog-cli": "1.2.0",
|
||||
"conventional-github-releaser": "1.1.3",
|
||||
"classlist-polyfill": "1.2.0",
|
||||
"codecov": "3.0.2",
|
||||
"codelyzer": "~4.3.0",
|
||||
"conventional-changelog-cli": "1.3.22",
|
||||
"conventional-github-releaser": "2.0.2",
|
||||
"core-js": "^2.4.1",
|
||||
"cpy": "5.0.0",
|
||||
"cpy": "7.0.0",
|
||||
"cpy-cli": "1.0.1",
|
||||
"del-cli": "0.2.1",
|
||||
"gh-pages": "0.12.0",
|
||||
"del-cli": "1.1.0",
|
||||
"gh-pages": "1.1.0",
|
||||
"gitignore-to-glob": "0.3.0",
|
||||
"google-code-prettify": "1.0.5",
|
||||
"html-loader": "0.4.4",
|
||||
"jasmine": "2.5.3",
|
||||
"jasmine-core": "2.5.2",
|
||||
"html-loader": "0.5.5",
|
||||
"jasmine": "3.1.0",
|
||||
"jasmine-core": "3.1.0",
|
||||
"jasmine-data-provider": "2.2.0",
|
||||
"jasmine-spec-reporter": "3.2.0",
|
||||
"karma": "1.4.0",
|
||||
"jasmine-spec-reporter": "4.2.1",
|
||||
"jquery": "^1.9.1",
|
||||
"karma": "2.0.2",
|
||||
"karma-chrome-launcher": "^2.0.0",
|
||||
"karma-cli": "^1.0.1",
|
||||
"karma-coverage-istanbul-reporter": "^1.3.0",
|
||||
"karma-coverage-istanbul-reporter": "^2.0.1",
|
||||
"karma-jasmine": "^1.0.2",
|
||||
"karma-remap-istanbul": "0.4.0",
|
||||
"karma-sauce-launcher": "1.1.0",
|
||||
"lite-server": "2.2.2",
|
||||
"lodash": "4.17.4",
|
||||
"markdown-loader": "^0.1.7",
|
||||
"marked": "0.3.6",
|
||||
"ng2-page-scroll": "4.0.0-beta.2",
|
||||
"ngm-cli": "0.4.0",
|
||||
"ngx-bootstrap": "1.6.6",
|
||||
"karma-remap-istanbul": "0.6.0",
|
||||
"karma-sauce-launcher": "1.2.0",
|
||||
"lite-server": "2.3.0",
|
||||
"lodash": "4.17.10",
|
||||
"markdown-loader": "^2.0.2",
|
||||
"marked": "0.4.0",
|
||||
"ngm-cli": "1.0.4",
|
||||
"ngx-bootstrap": "3.0.0",
|
||||
"npm-run-all": "^4.0.1",
|
||||
"popper.js": "^1.14.3",
|
||||
"pre-commit": "1.2.2",
|
||||
"protractor": "5.0.0",
|
||||
"reflect-metadata": "0.1.9",
|
||||
"require-dir": "0.3.1",
|
||||
"rxjs": "5.0.3",
|
||||
"systemjs-builder": "0.15.34",
|
||||
"protractor": "5.3.2",
|
||||
"reflect-metadata": "0.1.12",
|
||||
"require-dir": "1.0.0",
|
||||
"rxjs": "^6.2.0",
|
||||
"rxjs-compat": "^6.1.0",
|
||||
"rxjs-tslint": "^0.1.4",
|
||||
"systemjs-builder": "0.16.13",
|
||||
"ts-helpers": "^1.1.1",
|
||||
"ts-node": "2.0.0",
|
||||
"tslint": "4.3.1",
|
||||
"tslint-config-valorsoft": "1.2.0",
|
||||
"typedoc": "0.5.5",
|
||||
"typescript": "2.1.5",
|
||||
"wallaby-webpack": "0.0.30",
|
||||
"webdriver-manager": "11.1.1",
|
||||
"zone.js": "0.7.5"
|
||||
"ts-node": "6.0.3",
|
||||
"tslint": "5.10.0",
|
||||
"typedoc": "0.11.1",
|
||||
"typescript": "^2.7.2",
|
||||
"wallaby-webpack": "3.9.8",
|
||||
"webdriver-manager": "12.0.6",
|
||||
"zone.js": "0.8.26"
|
||||
},
|
||||
"contributors": [
|
||||
{
|
||||
|
||||
@@ -1,134 +1,141 @@
|
||||
import { map } from 'rxjs/operators/map';
|
||||
import { Observable } from 'rxjs';
|
||||
import { HttpErrorResponse } from '@angular/common/http/src/response';
|
||||
import { FileUploaderOptions, FileUploader } from './file-uploader.class';
|
||||
import { FileItem } from './file-item.class';
|
||||
import { HttpClient, HttpRequest, HttpEventType, HttpEvent, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http';
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { tap, last, catchError } from 'rxjs/operators';
|
||||
import { of } from 'rxjs/observable/of';
|
||||
import { HttpClient, HttpHeaders, HttpRequest, HttpEventType } from '@angular/common/http';
|
||||
import { Observable, of} from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
export interface UploaderLinksOptions {
|
||||
downloadEntry: string,
|
||||
updateEntry: string,
|
||||
createEntry: string,
|
||||
deleteEntry: string,
|
||||
downloadEntry: string;
|
||||
updateEntry: string;
|
||||
createEntry: string;
|
||||
deleteEntry: string;
|
||||
}
|
||||
|
||||
export interface UploaderServiceOptions {
|
||||
createMethod: string;
|
||||
updateMethod: string;
|
||||
authorizationHeaderName?: string;
|
||||
tokenPattern?: string;
|
||||
token?: string;
|
||||
chunkSize?: number;
|
||||
totalChunkParamName?: string;
|
||||
currentChunkParamName?: string;
|
||||
fileParamName?: string;
|
||||
idAttribute?: string;
|
||||
updateMethod: string;
|
||||
authorizationHeaderName?: string;
|
||||
tokenPattern?: string;
|
||||
token?: string;
|
||||
chunkSize?: number;
|
||||
totalChunkParamName?: string;
|
||||
currentChunkParamName?: string;
|
||||
fileParamName?: string;
|
||||
idAttribute?: string;
|
||||
}
|
||||
@Injectable()
|
||||
export class FileUploaderService {
|
||||
public defaultLinks: UploaderLinksOptions = {
|
||||
downloadEntry: '',
|
||||
updateEntry: '',
|
||||
createEntry: '',
|
||||
deleteEntry: '',
|
||||
}
|
||||
public defaultOptions: UploaderServiceOptions = {
|
||||
createMethod: 'POST',
|
||||
updateMethod: 'POST',
|
||||
authorizationHeaderName: "Authorization",
|
||||
tokenPattern: null,
|
||||
token: null,
|
||||
chunkSize: 0,
|
||||
totalChunkParamName: 'total_chunks',
|
||||
currentChunkParamName: 'current_chunk',
|
||||
fileParamName: 'file',
|
||||
idAttribute: 'id',
|
||||
}
|
||||
public additionalHeaders = {};
|
||||
protected cancelError = "UPLOAD CANCELED";
|
||||
protected uploadSubscription: any = null;
|
||||
public links:UploaderLinksOptions;
|
||||
public options:UploaderServiceOptions;
|
||||
private _uploader:FileUploader = null;
|
||||
constructor(
|
||||
protected http: HttpClient
|
||||
){
|
||||
this.links = Object.assign({}, this.defaultLinks, this.links);
|
||||
this.options = Object.assign({}, this.defaultOptions, this.options);
|
||||
}
|
||||
get uploader():FileUploader {
|
||||
return this._uploader;
|
||||
}
|
||||
set uploader(theUploader:FileUploader) {
|
||||
this._uploader = theUploader;
|
||||
}
|
||||
public onBeforeUpload(item:FileItem, options:FileUploaderOptions):Promise<any>{
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
resolve(true);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
public uploadFile(item:FileItem, options:FileUploaderOptions):void{
|
||||
this.onBeforeUpload(item,options).then(
|
||||
() => {
|
||||
this._uploadFile(item, options);
|
||||
}
|
||||
)
|
||||
}
|
||||
public onBeforeGetDefaultHeaders(): Promise<any> {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
resolve(true);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
protected _getDefaultHeaders(): Promise<any>{
|
||||
return new Promise((resolve,reject) => {
|
||||
this.onBeforeGetDefaultHeaders().then(
|
||||
(result) => {
|
||||
const h:any = {}
|
||||
if (this.options.tokenPattern && this.options.token) {
|
||||
h[this.options.authorizationHeaderName] = this.options.tokenPattern.replace("#token#", this.options.token);
|
||||
}
|
||||
for (const key in this.additionalHeaders) {
|
||||
if (this.additionalHeaders.hasOwnProperty(key)) {
|
||||
h[key] = this.additionalHeaders[key];
|
||||
}
|
||||
}
|
||||
resolve(h);
|
||||
},
|
||||
(error) => {
|
||||
reject(error);
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
protected _getRequestHeaders(item: FileItem, options: FileUploaderOptions): Promise<any> {
|
||||
return new Promise((resolve,reject) => {
|
||||
this._getDefaultHeaders().then(
|
||||
(h) => {
|
||||
if (options.headers) {
|
||||
for (let header of options.headers) {
|
||||
h[header.name] = header.value;
|
||||
}
|
||||
}
|
||||
if (item.headers.length) {
|
||||
for (let header of item.headers) {
|
||||
h[header.name] = header.value;
|
||||
}
|
||||
}
|
||||
resolve(h);
|
||||
},
|
||||
(error) => {
|
||||
reject(error)
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
public defaultLinks: UploaderLinksOptions = {
|
||||
downloadEntry: '',
|
||||
updateEntry: '',
|
||||
createEntry: '',
|
||||
deleteEntry: ''
|
||||
};
|
||||
public defaultOptions: UploaderServiceOptions = {
|
||||
createMethod: 'POST',
|
||||
updateMethod: 'POST',
|
||||
authorizationHeaderName: 'Authorization',
|
||||
tokenPattern: null,
|
||||
token: null,
|
||||
chunkSize: 0,
|
||||
totalChunkParamName: 'total_chunks',
|
||||
currentChunkParamName: 'current_chunk',
|
||||
fileParamName: 'file',
|
||||
idAttribute: 'id'
|
||||
};
|
||||
public additionalHeaders:any = {};
|
||||
protected cancelError = 'UPLOAD CANCELED';
|
||||
protected uploadSubscription: any = null;
|
||||
public links: UploaderLinksOptions;
|
||||
public options: UploaderServiceOptions;
|
||||
private _uploader: FileUploader = null;
|
||||
constructor(protected http: HttpClient) {
|
||||
this.links = Object.assign({}, this.defaultLinks, this.links);
|
||||
this.options = Object.assign({}, this.defaultOptions, this.options);
|
||||
}
|
||||
get uploader(): FileUploader {
|
||||
return this._uploader;
|
||||
}
|
||||
set uploader(theUploader: FileUploader) {
|
||||
this._uploader = theUploader;
|
||||
}
|
||||
public onBeforeUpload(
|
||||
item: FileItem,
|
||||
options: FileUploaderOptions
|
||||
): Promise<any> {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
resolve(true);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
public uploadFile(item: FileItem, options: FileUploaderOptions): void {
|
||||
this.onBeforeUpload(item, options).then(() => {
|
||||
this._uploadFile(item, options);
|
||||
});
|
||||
}
|
||||
public onBeforeGetDefaultHeaders(): Promise<any> {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
resolve(true);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
protected _getDefaultHeaders(): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.onBeforeGetDefaultHeaders().then(
|
||||
result => {
|
||||
const h: any = {};
|
||||
if (this.options.tokenPattern && this.options.token) {
|
||||
h[
|
||||
this.options.authorizationHeaderName
|
||||
] = this.options.tokenPattern.replace(
|
||||
'#token#',
|
||||
this.options.token
|
||||
);
|
||||
}
|
||||
for (const key in this.additionalHeaders) {
|
||||
if (this.additionalHeaders.hasOwnProperty(key)) {
|
||||
h[key] = this.additionalHeaders[key];
|
||||
}
|
||||
}
|
||||
resolve(h);
|
||||
},
|
||||
error => {
|
||||
reject(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
protected _getRequestHeaders(
|
||||
item: FileItem,
|
||||
options: FileUploaderOptions
|
||||
): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._getDefaultHeaders().then(
|
||||
h => {
|
||||
if (options.headers) {
|
||||
for (let header of options.headers) {
|
||||
h[header.name] = header.value;
|
||||
}
|
||||
}
|
||||
if (item.headers.length) {
|
||||
for (let header of item.headers) {
|
||||
h[header.name] = header.value;
|
||||
}
|
||||
}
|
||||
resolve(h);
|
||||
},
|
||||
error => {
|
||||
reject(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public buildPackageToSend(item:FileItem, options:FileUploaderOptions){
|
||||
let sendable: FormData = new FormData();
|
||||
public buildPackageToSend(item: FileItem, options: FileUploaderOptions) {
|
||||
let sendable: FormData = new FormData();
|
||||
this.uploader._onBuildItemForm(item, sendable);
|
||||
let file: any = null;
|
||||
if (this.options.chunkSize > 0) {
|
||||
@@ -136,7 +143,8 @@ export class FileUploaderService {
|
||||
} else {
|
||||
file = item._file;
|
||||
}
|
||||
const appendFile = () => sendable.append(this.options.fileParamName, file, item.file.name);
|
||||
const appendFile = () =>
|
||||
sendable.append(this.options.fileParamName, file, item.file.name);
|
||||
if (!options.parametersBeforeFiles) {
|
||||
appendFile();
|
||||
}
|
||||
@@ -146,191 +154,205 @@ export class FileUploaderService {
|
||||
Object.keys(options.additionalParameter).forEach((key: string) => {
|
||||
let paramVal = options.additionalParameter[key];
|
||||
// Allow an additional parameter to include the filename
|
||||
if (typeof paramVal === 'string' && paramVal.indexOf('{{file_name}}') >= 0) {
|
||||
if (
|
||||
typeof paramVal === 'string' &&
|
||||
paramVal.indexOf('{{file_name}}') >= 0
|
||||
) {
|
||||
paramVal = paramVal.replace('{{file_name}}', item.file.name);
|
||||
}
|
||||
sendable.append(key, paramVal);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (this.options.chunkSize > 0 && this.options.totalChunkParamName) {
|
||||
sendable.append(this.options.totalChunkParamName, item.getTotalChunks().toString());
|
||||
}
|
||||
sendable.append(
|
||||
this.options.totalChunkParamName,
|
||||
item.getTotalChunks().toString()
|
||||
);
|
||||
}
|
||||
if (this.options.chunkSize > 0 && this.options.currentChunkParamName) {
|
||||
sendable.append(this.options.currentChunkParamName, (item.getCurrentChunk() + 1).toString() );
|
||||
}
|
||||
sendable.append(
|
||||
this.options.currentChunkParamName,
|
||||
(item.getCurrentChunk() + 1).toString()
|
||||
);
|
||||
}
|
||||
|
||||
if (options.parametersBeforeFiles) {
|
||||
appendFile();
|
||||
}
|
||||
|
||||
return sendable;
|
||||
}
|
||||
}
|
||||
|
||||
protected _uploadFile(item:FileItem, options:FileUploaderOptions):void{
|
||||
this._getRequestHeaders(item, options).then(
|
||||
(headers) => {
|
||||
let request_method = this.options.createMethod;
|
||||
let link = this.links.createEntry;
|
||||
item.setIsUploading(true);
|
||||
if(this.options.chunkSize > 0){
|
||||
try{
|
||||
item.getCurrentChunk()
|
||||
}catch(err){
|
||||
item.createFileChunk(this.options.chunkSize);
|
||||
}
|
||||
request_method = item.getCurrentChunk() > 0 ? this.options.updateMethod : this.options.createMethod;
|
||||
link = item.getCurrentChunk() > 0 ? this.links.updateEntry : this.links.createEntry;
|
||||
}
|
||||
if (item.getId()){
|
||||
link = link.replace("#id#", item.getId());
|
||||
}
|
||||
const data = this.buildPackageToSend(item, options);
|
||||
let request = new HttpRequest(request_method, link, data, {
|
||||
headers: new HttpHeaders(headers),
|
||||
reportProgress: true,
|
||||
withCredentials: item.withCredentials,
|
||||
});
|
||||
this.uploadSubscription = this.http.request(request).subscribe(
|
||||
(event) => {
|
||||
this.getEventMessage(event, item);
|
||||
},
|
||||
(error) => {
|
||||
if(this.cancelError === error){
|
||||
this.uploader.onAbort(error, item);
|
||||
}else{
|
||||
this.uploader.onError(error, item);
|
||||
}
|
||||
|
||||
},
|
||||
)
|
||||
},
|
||||
(error) =>{
|
||||
protected _uploadFile(item: FileItem, options: FileUploaderOptions): void {
|
||||
this._getRequestHeaders(item, options).then(
|
||||
headers => {
|
||||
let request_method = this.options.createMethod;
|
||||
let link = this.links.createEntry;
|
||||
item.setIsUploading(true);
|
||||
if (this.options.chunkSize > 0) {
|
||||
try {
|
||||
item.getCurrentChunk();
|
||||
} catch (err) {
|
||||
item.createFileChunk(this.options.chunkSize);
|
||||
}
|
||||
request_method =
|
||||
item.getCurrentChunk() > 0
|
||||
? this.options.updateMethod
|
||||
: this.options.createMethod;
|
||||
link =
|
||||
item.getCurrentChunk() > 0
|
||||
? this.links.updateEntry
|
||||
: this.links.createEntry;
|
||||
}
|
||||
if (item.getId()) {
|
||||
link = link.replace('#id#', item.getId());
|
||||
}
|
||||
const data = this.buildPackageToSend(item, options);
|
||||
const request = new HttpRequest(request_method, link, data, {
|
||||
headers: new HttpHeaders(headers),
|
||||
reportProgress: true,
|
||||
withCredentials: item.withCredentials,
|
||||
});
|
||||
this.uploadSubscription = this.http.request(request).subscribe(
|
||||
(event: any) => {
|
||||
this.getEventMessage(event, item);
|
||||
},
|
||||
(error: any) => {
|
||||
if (this.cancelError === error) {
|
||||
this.uploader.onAbort(error, item);
|
||||
} else {
|
||||
this.uploader.onError(error, item);
|
||||
}
|
||||
}
|
||||
);
|
||||
},
|
||||
error => {}
|
||||
);
|
||||
}
|
||||
public stopUpload() {
|
||||
if (this.uploadSubscription && this.uploadSubscription.unsubscribe) {
|
||||
this.uploadSubscription.error(this.cancelError);
|
||||
}
|
||||
}
|
||||
private getEventMessage(event: any, item: FileItem) {
|
||||
switch (event.type) {
|
||||
case HttpEventType.ResponseHeader:
|
||||
break;
|
||||
case HttpEventType.Sent:
|
||||
this.uploader.onStart(event, item);
|
||||
break;
|
||||
case HttpEventType.UploadProgress:
|
||||
this.uploader.onProgress(event, item);
|
||||
break;
|
||||
case HttpEventType.Response:
|
||||
if (this.options.chunkSize > 0) {
|
||||
if (item.getCurrentChunk() === 0) {
|
||||
const response = event.body;
|
||||
if (response[this.options.idAttribute]) {
|
||||
item.setId(response[this.options.idAttribute]);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.uploader.onLoad(event, item);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
private handleError(item: FileItem) {
|
||||
const userMessage = `${item.file.name} upload failed.`;
|
||||
|
||||
}
|
||||
)
|
||||
}
|
||||
public stopUpload() {
|
||||
if(this.uploadSubscription && this.uploadSubscription.unsubscribe){
|
||||
this.uploadSubscription.error(this.cancelError)
|
||||
}
|
||||
}
|
||||
private getEventMessage(event: any, item: FileItem) {
|
||||
switch (event.type) {
|
||||
case HttpEventType.ResponseHeader:
|
||||
break;
|
||||
case HttpEventType.Sent:
|
||||
this.uploader.onStart(event, item);
|
||||
break;
|
||||
case HttpEventType.UploadProgress:
|
||||
this.uploader.onProgress(event, item);
|
||||
break;
|
||||
case HttpEventType.Response:
|
||||
if(this.options.chunkSize > 0){
|
||||
if(item.getCurrentChunk() === 0) {
|
||||
const response = event.body;
|
||||
if(response[this.options.idAttribute]) {
|
||||
item.setId(response[this.options.idAttribute])
|
||||
}
|
||||
}
|
||||
}
|
||||
this.uploader.onLoad(event, item);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
private handleError(item: FileItem) {
|
||||
const userMessage = `${item.file.name} upload failed.`;
|
||||
|
||||
return (error: HttpErrorResponse) => {
|
||||
this.uploader.onError(error, item);
|
||||
return (error: HttpErrorResponse) => {
|
||||
this.uploader.onError(error, item);
|
||||
|
||||
const message = (error.error instanceof Error) ?
|
||||
error.error.message :
|
||||
`server returned code ${error.status} with body "${error.error}"`;
|
||||
const message =
|
||||
error.error instanceof Error
|
||||
? error.error.message
|
||||
: `server returned code ${error.status} with body "${error.error}"`;
|
||||
|
||||
return of(userMessage);
|
||||
};
|
||||
}
|
||||
return of (userMessage);
|
||||
};
|
||||
}
|
||||
|
||||
public deleteEntry(item: FileItem, options = {}, skipConfirmation = false): Observable<any>{
|
||||
if(item.getId() && this.links['deleteEntry']){
|
||||
let link = this.links['deleteEntry'].replace(/#id#/g, item.getId());
|
||||
let confirmation = false;
|
||||
if(skipConfirmation){
|
||||
confirmation = true;
|
||||
} else {
|
||||
confirmation = confirm("Are you sure you want to delete this entry?")
|
||||
}
|
||||
|
||||
if (confirmation) {
|
||||
return this.delete(link, options).map(
|
||||
(response: Response) => {
|
||||
return response;
|
||||
}
|
||||
)
|
||||
}
|
||||
else{
|
||||
return Observable.of(false);
|
||||
}
|
||||
}else{
|
||||
return Observable.of(false);
|
||||
}
|
||||
}
|
||||
public deleteEntry(
|
||||
item: FileItem,
|
||||
options = {},
|
||||
skipConfirmation = false
|
||||
): Observable <any> {
|
||||
if (item.getId() && this.links['deleteEntry']) {
|
||||
let link = this.links['deleteEntry'].replace(/#id#/g, item.getId());
|
||||
let confirmation = false;
|
||||
if (skipConfirmation) {
|
||||
confirmation = true;
|
||||
} else {
|
||||
confirmation = confirm('Are you sure you want to delete this entry?');
|
||||
}
|
||||
|
||||
protected delete(url: string, options = {}): Observable<any> {
|
||||
return new Observable(
|
||||
(observe) => {
|
||||
this._getDefaultHeaders().then(
|
||||
function(headers){
|
||||
return this.http.delete(url, {headers: new HttpHeaders(headers) }).subscribe(
|
||||
(response: Response) => {
|
||||
observe.next(response);
|
||||
},
|
||||
(error) => {
|
||||
observe.error(error);
|
||||
}
|
||||
);
|
||||
}.bind(this),
|
||||
(error) => {
|
||||
observe.error(error);
|
||||
});
|
||||
}
|
||||
)
|
||||
}
|
||||
if (confirmation) {
|
||||
return this.delete(link, options)
|
||||
} else {
|
||||
return of(false);
|
||||
}
|
||||
} else {
|
||||
return of(false);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
HTTP General methos only bellow
|
||||
*/
|
||||
protected get(url: string): Observable<any>{
|
||||
return new Observable(
|
||||
(observe) => {
|
||||
this._getDefaultHeaders().then(
|
||||
function(headers){
|
||||
return this.http.get(url, {headers: new HttpHeaders(headers) }).subscribe(
|
||||
(response: Response) => {
|
||||
observe.next(response);
|
||||
},
|
||||
(error) => {
|
||||
observe.error(error);
|
||||
}
|
||||
);
|
||||
}.bind(this),
|
||||
(error) => {
|
||||
observe.error(error);
|
||||
});
|
||||
}
|
||||
)
|
||||
}
|
||||
protected delete(url: string, options = {}): Observable<any> {
|
||||
return new Observable((observe:any) => {
|
||||
this._getDefaultHeaders().then(
|
||||
function(headers: any) {
|
||||
return this.http
|
||||
.delete(url, { headers: new HttpHeaders(headers) })
|
||||
.subscribe(
|
||||
(response: Response) => {
|
||||
observe.next(response);
|
||||
},
|
||||
(error: any) => {
|
||||
observe.error(error);
|
||||
}
|
||||
);
|
||||
}.bind(this),
|
||||
error => {
|
||||
observe.error(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public addHeader(name=null, value=null){
|
||||
this.additionalHeaders[name] = value
|
||||
}
|
||||
/*
|
||||
HTTP General methos only bellow
|
||||
*/
|
||||
protected get(url: string): Observable<any> {
|
||||
return new Observable(observe => {
|
||||
this._getDefaultHeaders().then(
|
||||
function(headers:any) {
|
||||
return this.http
|
||||
.get(url, { headers: new HttpHeaders(headers) })
|
||||
.subscribe(
|
||||
(response: Response) => {
|
||||
observe.next(response);
|
||||
},
|
||||
(error: any) => {
|
||||
observe.error(error);
|
||||
}
|
||||
);
|
||||
}.bind(this),
|
||||
error => {
|
||||
observe.error(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public removeHeader(name=null){
|
||||
if(this.additionalHeaders.hasOwnProperty(name)){
|
||||
delete(this.additionalHeaders[name])
|
||||
}
|
||||
}
|
||||
}
|
||||
public addHeader(name:string = null, value:any = null) {
|
||||
this.additionalHeaders[name] = value;
|
||||
}
|
||||
|
||||
public removeHeader(name:string = null) {
|
||||
if (this.additionalHeaders.hasOwnProperty(name)) {
|
||||
delete this.additionalHeaders[name];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,7 @@
|
||||
"eofline": true,
|
||||
"forin": true,
|
||||
"import-blacklist": [
|
||||
true,
|
||||
"rxjs"
|
||||
true
|
||||
],
|
||||
"import-spacing": true,
|
||||
"indent": [
|
||||
|
||||
Reference in New Issue
Block a user