@adminforth/upload 2.0.0 → 2.0.1

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/README.md CHANGED
@@ -4,4 +4,4 @@
4
4
 
5
5
  Allows to upload files to Amazon S3 bucket from adminforth application.
6
6
 
7
- ## For usage, see [AdminForth Upload Documentation](https://adminforth.dev/docs/tutorial/Plugins/upload/)
7
+ ## For usage, see [AdminForth Upload Documentation](https://adminforth.dev/docs/tutorial/Plugins/upload/)
package/build.log CHANGED
@@ -11,5 +11,5 @@ custom/preview.vue
11
11
  custom/tsconfig.json
12
12
  custom/uploader.vue
13
13
 
14
- sent 43,158 bytes received 134 bytes 86,584.00 bytes/sec
15
- total size is 42,676 speedup is 0.99
14
+ sent 44,046 bytes received 134 bytes 88,360.00 bytes/sec
15
+ total size is 43,557 speedup is 0.99
@@ -58,7 +58,7 @@
58
58
  } */
59
59
  </style>
60
60
  <script setup>
61
- import { ref, computed , onMounted, watch} from 'vue'
61
+ import { ref, computed , onMounted, watch, nextTick} from 'vue'
62
62
  import mediumZoom from 'medium-zoom'
63
63
  import { useRoute } from 'vue-router'
64
64
 
@@ -71,6 +71,30 @@ const props = defineProps({
71
71
  meta: Object,
72
72
  })
73
73
 
74
+ const trueContentType = ref(null);
75
+
76
+ onMounted(async () => {
77
+ // try to get HEAD request
78
+ try {
79
+ const response = await fetch(url.value, {
80
+ method: 'HEAD',
81
+ });
82
+ const ct = response.headers.get('Content-Type');
83
+ if (ct) {
84
+ trueContentType.value = ct;
85
+ }
86
+ } catch (e) {
87
+ console.error('fetch error for getting content type, please check CORS allowed for HEAD request', e);
88
+ }
89
+ });
90
+
91
+ const contentType = computed(() => {
92
+ if (trueContentType.value) {
93
+ return trueContentType.value;
94
+ }
95
+ return guessedContentType.value;
96
+ });
97
+
74
98
  const route = useRoute();
75
99
  const url = computed(() => {
76
100
  return props.record[`previewUrl_${props.meta.pluginInstanceId}`];
@@ -95,11 +119,11 @@ const minWidth = computed(() => {
95
119
  });
96
120
  // since we have no way to know the content type of the file, we will try to guess it from extension
97
121
  // for better experience probably we should check whether user saves content type in the database and use it here
98
- const contentType = computed(() => {
122
+ const guessedContentType = computed(() => {
99
123
  if (!url.value) {
100
124
  return null;
101
125
  }
102
- const u = new URL(url.value);
126
+ const u = new URL(url.value, url.value.startsWith('http') ? undefined : location.origin);
103
127
  return guessContentType(u.pathname);
104
128
  });
105
129
 
@@ -117,14 +141,18 @@ function guessContentType(url) {
117
141
  }
118
142
 
119
143
 
120
- onMounted(async () => {
121
-
122
- if (contentType.value?.startsWith('image')) {
144
+ watch([contentType], async ([contentType]) => {
145
+ // since content type might change after true guessing (HEAD request might be slow) we need to try initializing zoom again
146
+ if (zoom.value) {
147
+ zoom.value.detach();
148
+ }
149
+ await nextTick();
150
+ if (contentType?.startsWith('image')) {
123
151
  zoom.value = mediumZoom(img.value, {
124
152
  margin: 24,
125
153
  });
126
154
  }
127
155
 
128
- });
156
+ }, { immediate: true });
129
157
 
130
158
  </script>
@@ -273,7 +273,7 @@ const onFileChange = async (e) => {
273
273
  });
274
274
  if (!success) {
275
275
  adminforth.alert({
276
- messageHtml: `<div>${t('Sorry but the file was not uploaded because of S3 Request Error:')}</div>
276
+ messageHtml: `<div>${t('Sorry but the file was not uploaded because of internal storage Request Error:')}</div>
277
277
  <pre style="white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; max-width: 100%;">${
278
278
  xhr.responseText.replace(/</g, '&lt;').replace(/>/g, '&gt;')
279
279
  }</pre>`,
@@ -58,7 +58,7 @@
58
58
  } */
59
59
  </style>
60
60
  <script setup>
61
- import { ref, computed , onMounted, watch} from 'vue'
61
+ import { ref, computed , onMounted, watch, nextTick} from 'vue'
62
62
  import mediumZoom from 'medium-zoom'
63
63
  import { useRoute } from 'vue-router'
64
64
 
@@ -71,6 +71,30 @@ const props = defineProps({
71
71
  meta: Object,
72
72
  })
73
73
 
74
+ const trueContentType = ref(null);
75
+
76
+ onMounted(async () => {
77
+ // try to get HEAD request
78
+ try {
79
+ const response = await fetch(url.value, {
80
+ method: 'HEAD',
81
+ });
82
+ const ct = response.headers.get('Content-Type');
83
+ if (ct) {
84
+ trueContentType.value = ct;
85
+ }
86
+ } catch (e) {
87
+ console.error('fetch error for getting content type, please check CORS allowed for HEAD request', e);
88
+ }
89
+ });
90
+
91
+ const contentType = computed(() => {
92
+ if (trueContentType.value) {
93
+ return trueContentType.value;
94
+ }
95
+ return guessedContentType.value;
96
+ });
97
+
74
98
  const route = useRoute();
75
99
  const url = computed(() => {
76
100
  return props.record[`previewUrl_${props.meta.pluginInstanceId}`];
@@ -95,11 +119,11 @@ const minWidth = computed(() => {
95
119
  });
96
120
  // since we have no way to know the content type of the file, we will try to guess it from extension
97
121
  // for better experience probably we should check whether user saves content type in the database and use it here
98
- const contentType = computed(() => {
122
+ const guessedContentType = computed(() => {
99
123
  if (!url.value) {
100
124
  return null;
101
125
  }
102
- const u = new URL(url.value);
126
+ const u = new URL(url.value, url.value.startsWith('http') ? undefined : location.origin);
103
127
  return guessContentType(u.pathname);
104
128
  });
105
129
 
@@ -117,14 +141,18 @@ function guessContentType(url) {
117
141
  }
118
142
 
119
143
 
120
- onMounted(async () => {
121
-
122
- if (contentType.value?.startsWith('image')) {
144
+ watch([contentType], async ([contentType]) => {
145
+ // since content type might change after true guessing (HEAD request might be slow) we need to try initializing zoom again
146
+ if (zoom.value) {
147
+ zoom.value.detach();
148
+ }
149
+ await nextTick();
150
+ if (contentType?.startsWith('image')) {
123
151
  zoom.value = mediumZoom(img.value, {
124
152
  margin: 24,
125
153
  });
126
154
  }
127
155
 
128
- });
156
+ }, { immediate: true });
129
157
 
130
158
  </script>
@@ -273,7 +273,7 @@ const onFileChange = async (e) => {
273
273
  });
274
274
  if (!success) {
275
275
  adminforth.alert({
276
- messageHtml: `<div>${t('Sorry but the file was not uploaded because of S3 Request Error:')}</div>
276
+ messageHtml: `<div>${t('Sorry but the file was not uploaded because of internal storage Request Error:')}</div>
277
277
  <pre style="white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; max-width: 100%;">${
278
278
  xhr.responseText.replace(/</g, '&lt;').replace(/>/g, '&gt;')
279
279
  }</pre>`,
package/dist/index.js CHANGED
@@ -24,7 +24,8 @@ export default class UploadPlugin extends AdminForthPlugin {
24
24
  }
25
25
  setupLifecycleRule() {
26
26
  return __awaiter(this, void 0, void 0, function* () {
27
- this.options.storageAdapter.setupLifecycle();
27
+ const adapterUserUniqueRepresentation = `${this.resourceConfig.resourceId}-${this.pluginInstanceId}`;
28
+ this.options.storageAdapter.setupLifecycle(adapterUserUniqueRepresentation);
28
29
  });
29
30
  }
30
31
  genPreviewUrl(record) {
@@ -45,6 +46,7 @@ export default class UploadPlugin extends AdminForthPlugin {
45
46
  return __awaiter(this, void 0, void 0, function* () {
46
47
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
47
48
  _super.modifyResourceConfig.call(this, adminforth, resourceConfig);
49
+ this.resourceConfig = resourceConfig;
48
50
  // after column to store the path of the uploaded file, add new VirtualColumn,
49
51
  // show only in edit and create views
50
52
  // use component uploader.vue
@@ -316,7 +318,7 @@ export default class UploadPlugin extends AdminForthPlugin {
316
318
  if (!record) {
317
319
  return { error: `Record with id ${recordId} not found` };
318
320
  }
319
- attachmentFiles = this.options.generation.attachFiles({ record, adminUser });
321
+ attachmentFiles = yield this.options.generation.attachFiles({ record, adminUser });
320
322
  // if files is not array, make it array
321
323
  if (!Array.isArray(attachmentFiles)) {
322
324
  attachmentFiles = [attachmentFiles];
package/index.ts CHANGED
@@ -14,6 +14,8 @@ export default class UploadPlugin extends AdminForthPlugin {
14
14
  totalCalls: number;
15
15
  totalDuration: number;
16
16
 
17
+ resourceConfig: AdminForthResource;
18
+
17
19
  constructor(options: PluginOptions) {
18
20
  super(options, import.meta.url);
19
21
  this.options = options;
@@ -28,7 +30,8 @@ export default class UploadPlugin extends AdminForthPlugin {
28
30
  }
29
31
 
30
32
  async setupLifecycleRule() {
31
- this.options.storageAdapter.setupLifecycle();
33
+ const adapterUserUniqueRepresentation = `${this.resourceConfig.resourceId}-${this.pluginInstanceId}`;
34
+ this.options.storageAdapter.setupLifecycle(adapterUserUniqueRepresentation);
32
35
  }
33
36
 
34
37
  async genPreviewUrl(record: any) {
@@ -43,6 +46,7 @@ export default class UploadPlugin extends AdminForthPlugin {
43
46
 
44
47
  async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
45
48
  super.modifyResourceConfig(adminforth, resourceConfig);
49
+ this.resourceConfig = resourceConfig;
46
50
  // after column to store the path of the uploaded file, add new VirtualColumn,
47
51
  // show only in edit and create views
48
52
  // use component uploader.vue
@@ -361,7 +365,7 @@ export default class UploadPlugin extends AdminForthPlugin {
361
365
  return { error: `Record with id ${recordId} not found` };
362
366
  }
363
367
 
364
- attachmentFiles = this.options.generation.attachFiles({ record, adminUser });
368
+ attachmentFiles = await this.options.generation.attachFiles({ record, adminUser });
365
369
  // if files is not array, make it array
366
370
  if (!Array.isArray(attachmentFiles)) {
367
371
  attachmentFiles = [attachmentFiles];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/upload",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Plugin for uploading files for adminforth",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/types.ts CHANGED
@@ -131,7 +131,7 @@ export type PluginOptions = {
131
131
  attachFiles?: ({ record, adminUser }: {
132
132
  record: any,
133
133
  adminUser: AdminUser,
134
- }) => string[],
134
+ }) => string[] | Promise<string[]>,
135
135
 
136
136
 
137
137
  /**