forked from bleenco/ngx-uploader
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
410 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
import { NgFileDropDirective } from './src/directives/ng-file-drop'; | ||
import { NgFileSelectDirective } from './src/directives/ng-file-select'; | ||
|
||
export * from './src/directives/ng-file-drop'; | ||
export * from './src/directives/ng-file-select'; | ||
export * from './src/services/ng2-uploader'; | ||
|
||
export { Ng2UploaderModule } from './src/module/ng2-uploader.module'; | ||
export { Ng2UploaderOptions, UploadedFile, UploadRejected } from './src/classes'; | ||
|
||
export { Ng2UploaderModule } from './src/module/ng2-uploader.module'; | ||
|
||
export const UPLOAD_DIRECTIVES: any[] = [ | ||
NgFileSelectDirective, | ||
NgFileDropDirective | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './ng2-uploader-options.class'; | ||
export * from './uploaded-file.class'; | ||
export * from './upload-rejected.class'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
export interface INg2UploaderOptions { | ||
url: string; | ||
cors?: boolean; | ||
withCredentials?: boolean; | ||
multiple?: boolean; | ||
maxUploads?: number; | ||
data?: any; | ||
autoUpload?: boolean; | ||
multipart?: any; | ||
method?: 'POST' | 'GET'; | ||
customHeaders?: any; | ||
encodeHeaders?: boolean; | ||
authTokenPrefix?: string; | ||
authToken?: string; | ||
fieldName?: string; | ||
fieldReset?: boolean; | ||
previewUrl?: string; | ||
calculateSpeed?: boolean; | ||
filterExtensions?: boolean; | ||
allowedExtensions?: string[]; | ||
} | ||
|
||
export class Ng2UploaderOptions implements INg2UploaderOptions { | ||
url: string; | ||
cors?: boolean; | ||
withCredentials?: boolean; | ||
multiple?: boolean; | ||
maxUploads?: number; | ||
data?: any; | ||
autoUpload?: boolean; | ||
multipart?: any; | ||
method?: 'POST' | 'GET'; | ||
customHeaders?: any; | ||
encodeHeaders?: boolean; | ||
authTokenPrefix?: string; | ||
authToken?: string; | ||
fieldName?: string; | ||
fieldReset?: boolean; | ||
previewUrl?: string; | ||
calculateSpeed?: boolean; | ||
filterExtensions?: boolean; | ||
allowedExtensions?: string[]; | ||
|
||
constructor(obj: INg2UploaderOptions) { | ||
this.url = obj.url != null ? obj.url : ''; | ||
this.cors = obj.cors != null ? obj.cors : true; | ||
this.withCredentials = obj.withCredentials != null ? obj.withCredentials : this.withCredentials; | ||
this.multiple = obj.multiple != null ? obj.multiple : true; | ||
this.maxUploads = obj.maxUploads != null ? obj.maxUploads : 10; | ||
this.data = obj.data != null ? obj.data : {}; | ||
this.autoUpload = obj && obj.autoUpload ? obj.autoUpload : true; | ||
this.multipart = obj.multipart != null ? obj.multipart : false; | ||
this.method = obj.method != null ? obj.method : 'POST'; | ||
this.customHeaders = obj.customHeaders != null ? obj.customHeaders : { }; | ||
this.encodeHeaders = obj.encodeHeaders != null ? obj.encodeHeaders : false; | ||
this.authTokenPrefix = obj.authTokenPrefix != null ? obj.authTokenPrefix : 'Bearer'; | ||
this.authToken = obj.authToken != null ? obj.authToken : null; | ||
this.fieldName = obj.fieldName != null ? obj.fieldName : 'file'; | ||
this.fieldReset = obj.fieldReset != null ? obj.fieldReset : null; | ||
this.previewUrl = obj.previewUrl != null ? obj.previewUrl : null; | ||
this.calculateSpeed = obj.calculateSpeed != null ? obj.calculateSpeed : true; | ||
this.filterExtensions = obj.filterExtensions != null ? obj.filterExtensions : false; | ||
this.allowedExtensions = obj && obj.allowedExtensions ? obj.allowedExtensions : []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class UploadRejected { | ||
public static get EXTENSION_NOT_ALLOWED():string { return 'ExtensionNotAllowed'; } | ||
public static get MAX_SIZE_EXCEEDED():string { return 'MaxSizeExceeded'; } | ||
|
||
file: any; | ||
reason: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
export class UploadedFile { | ||
id: string; | ||
status: number; | ||
statusText: string; | ||
progress: Object; | ||
originalName: string; | ||
size: number; | ||
response: string; | ||
done: boolean; | ||
error: boolean; | ||
abort: boolean; | ||
startTime: number; | ||
endTime: number; | ||
speedAverage: number; | ||
speedAverageHumanized: string; | ||
|
||
constructor(id: string, originalName: string, size: number) { | ||
this.id = id; | ||
this.originalName = originalName; | ||
this.size = size; | ||
this.progress = { | ||
loaded: 0, | ||
total: 0, | ||
percent: 0, | ||
speed: 0, | ||
speedHumanized: null | ||
}; | ||
this.done = false; | ||
this.error = false; | ||
this.abort = false; | ||
this.startTime = new Date().getTime(); | ||
this.endTime = 0; | ||
this.speedAverage = 0; | ||
this.speedAverageHumanized = null; | ||
} | ||
|
||
setProgres(progress: Object): void { | ||
this.progress = progress; | ||
} | ||
|
||
setError(): void { | ||
this.error = true; | ||
this.done = true; | ||
} | ||
|
||
setAbort(): void { | ||
this.abort = true; | ||
this.done = true; | ||
} | ||
|
||
onFinished(status: number, statusText: string, response: string): void { | ||
this.endTime = new Date().getTime(); | ||
this.speedAverage = this.size / (this.endTime - this.startTime) * 1000; | ||
this.speedAverage = parseInt(<any>this.speedAverage, 10); | ||
this.speedAverageHumanized = this.humanizeBytes(this.speedAverage); | ||
this.status = status; | ||
this.statusText = statusText; | ||
this.response = response; | ||
this.done = true; | ||
} | ||
|
||
humanizeBytes(bytes: number): string { | ||
if (bytes === 0) { | ||
return '0 Byte'; | ||
} | ||
let k = 1024; | ||
const sizes: string[] = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB']; | ||
let i: number = Math.floor(Math.log(bytes) / Math.log(k)); | ||
|
||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i] + '/s'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.