2021-12-10 02:37:01 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
2024-09-20 15:23:15 +00:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2021-12-10 02:37:01 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-12-09 05:44:17 +00:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
import {Subject} from 'rxjs';
|
|
|
|
|
|
2020-02-18 21:13:57 +00:00
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root',
|
|
|
|
|
})
|
|
|
|
|
export class FileApiService {
|
|
|
|
|
uploadedData: Subject<any> = new Subject();
|
|
|
|
|
|
2025-03-20 10:38:03 +00:00
|
|
|
publishFileUpload(parentEvent: Event): void {
|
2020-02-18 21:13:57 +00:00
|
|
|
const reader = new FileReader();
|
2020-05-15 02:31:10 +00:00
|
|
|
reader.onload = (event) => {
|
2020-02-27 22:07:22 +00:00
|
|
|
try {
|
|
|
|
|
this.uploadedData.next(JSON.parse((event.target as any).result));
|
|
|
|
|
} catch (e) {
|
2021-12-09 05:44:17 +00:00
|
|
|
this.uploadedData.next({error: e});
|
2020-02-27 22:07:22 +00:00
|
|
|
}
|
2020-02-18 21:13:57 +00:00
|
|
|
(parentEvent.target as any).value = '';
|
|
|
|
|
};
|
|
|
|
|
reader.readAsText((parentEvent.target as any).files[0]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 22:07:22 +00:00
|
|
|
saveObjectAsJSON(object: object): void {
|
2021-03-04 20:15:09 +00:00
|
|
|
const downloadLink = document.createElement('a');
|
2024-01-19 20:01:59 +00:00
|
|
|
const isoString = new Date().toISOString().slice(0, -5); // remove milliseconds
|
|
|
|
|
downloadLink.download = `NgDevTools-Profile-${isoString}.json`;
|
2021-12-09 05:44:17 +00:00
|
|
|
downloadLink.href = URL.createObjectURL(
|
|
|
|
|
new Blob([JSON.stringify(object)], {type: 'application/json'}),
|
|
|
|
|
);
|
2021-03-04 20:15:09 +00:00
|
|
|
downloadLink.click();
|
|
|
|
|
setTimeout(() => URL.revokeObjectURL(downloadLink.href));
|
2020-02-18 21:13:57 +00:00
|
|
|
}
|
|
|
|
|
}
|