@social-mail/social-mail-client 1.2.39 → 1.2.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@social-mail/social-mail-client",
3
- "version": "1.2.39",
3
+ "version": "1.2.41",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -3,11 +3,22 @@ import { IAppFile } from "../../../../model/model";
3
3
  import XNode from "@web-atoms/core/dist/core/XNode";
4
4
  import Action from "@web-atoms/core/dist/view-model/Action";
5
5
  import { UMD } from "@web-atoms/core/dist/core/types";
6
+ import FetchBuilder from "@web-atoms/core/dist/services/FetchBuilder";
7
+ import { Inject } from "@web-atoms/core/dist/di/Inject";
8
+ import InjectProperty from "@web-atoms/core/dist/core/InjectProperty";
9
+ import CloudFileService from "../../../../web/page/files/service/CloudFileService";
10
+ import MimeTypeService from "../../../../services/files/MimeTypeService";
6
11
 
7
12
  export default class ChooseWebSiteTemplatePage extends PopupWindowPage {
8
13
 
9
14
  parameters: IAppFile;
10
15
 
16
+ @InjectProperty
17
+ cloudFileService: CloudFileService;
18
+
19
+ @InjectProperty
20
+ mimeTypeService: MimeTypeService;
21
+
11
22
  async init() {
12
23
  this.renderer = <div>
13
24
  <button
@@ -19,20 +30,27 @@ export default class ChooseWebSiteTemplatePage extends PopupWindowPage {
19
30
  @Action({ onEvent: "create-template", close: true})
20
31
  async createTemplate() {
21
32
 
22
- // upload a simple html...
23
- const path = UMD.resolvePath("@social-mail/social-mail-client/content/templates/plain/index.html");
24
- const rs = await fetch(path);
25
- const text = await rs.text();
33
+ const template = "plain";
26
34
 
27
35
  // upload
28
- await this.upload("index.html", text);
36
+ await this.upload("index.html", template);
37
+ await this.upload("vars.css", template, "styles");
29
38
  }
30
39
 
31
- async upload(fileName: string, content: string) {
32
- const url = `/api/upload?parentID=${this.parameters.appFileID}`;
33
- const body = new FormData();
34
- body.append(fileName, new Blob([content], { type: "text/html"}), fileName);
35
- const rs = await fetch(url, { method: "POST", body });
40
+ async upload(fileName, template, folder = void 0) {
41
+
42
+ const templatePath = folder ? `${folder}/${fileName}` : fileName;
43
+ const path = UMD.resolvePath(`@social-mail/social-mail-client/content/templates/${template}/${templatePath}`);
44
+ const content = await FetchBuilder.get(path).responseAsText();
45
+
46
+ const contentType = await this.mimeTypeService.getMimeType(fileName);
47
+
48
+ await this.cloudFileService.saveFile({
49
+ content: new File([content], fileName, { type: contentType }),
50
+ parentID: this.parameters.appFileID,
51
+ folder
52
+ });
53
+
36
54
  }
37
55
 
38
56
  }
@@ -0,0 +1,45 @@
1
+ import DISingleton from "@web-atoms/core/dist/di/DISingleton";
2
+ import FetchBuilder from "@web-atoms/core/dist/services/FetchBuilder";
3
+
4
+ export interface IMimeMap {
5
+ [key: string]: {
6
+ extensions?: string[]
7
+ }
8
+ }
9
+
10
+ let mimeMap = void 0 as Promise<Map<string,string>>;
11
+
12
+ @DISingleton()
13
+ export default class MimeTypeService {
14
+
15
+ async getMimeType(fileName: string) {
16
+ mimeMap ??= this.createMap();
17
+ const map = await mimeMap;
18
+ const group = /\.([^\.]+)$/.exec(fileName);
19
+ if (group) {
20
+ const ct = map.get(group[1]);
21
+ if (ct) {
22
+ return ct;
23
+ }
24
+ }
25
+ return "application/octat-stream";
26
+ }
27
+
28
+ async createMap(): Promise<Map<string, string>> {
29
+ const json = await FetchBuilder.get("https://cdn.jsdelivr.net/gh/jshttp/mime-db@master/db.json").responseAsJson<IMimeMap>();
30
+ const map = new Map<string,string>();
31
+ setTimeout(() => mimeMap = null, 60000);
32
+ for (const key in json) {
33
+ if (Object.prototype.hasOwnProperty.call(json, key)) {
34
+ const { extensions } = json[key];
35
+ if (extensions) {
36
+ for (const iterator of extensions) {
37
+ map.set(iterator, key);
38
+ }
39
+ }
40
+ }
41
+ }
42
+ return map;
43
+ }
44
+
45
+ }