@shopware-ag/app-server-sdk 1.1.26 → 1.1.27
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/dist/commonjs/helper/media.d.ts +17 -0
- package/dist/commonjs/helper/media.d.ts.map +1 -1
- package/dist/commonjs/helper/media.js +41 -0
- package/dist/commonjs/helper/media.js.map +1 -1
- package/dist/esm/helper/media.d.ts +17 -0
- package/dist/esm/helper/media.d.ts.map +1 -1
- package/dist/esm/helper/media.js +40 -0
- package/dist/esm/helper/media.js.map +1 -1
- package/package.json +1 -1
|
@@ -16,6 +16,23 @@ export declare function uploadMediaFile(httpClient: HttpClient, { private: isPri
|
|
|
16
16
|
fileName: string;
|
|
17
17
|
file: Blob | Promise<Blob>;
|
|
18
18
|
}): Promise<string>;
|
|
19
|
+
/**
|
|
20
|
+
* Uploads a media file by URL to the Shopware instance.
|
|
21
|
+
*
|
|
22
|
+
* @param {HttpClient} httpClient - The HTTP client instance.
|
|
23
|
+
* @param {Object} options - The options for uploading the media file.
|
|
24
|
+
* @param {boolean} [options.private] - Whether the media file should be private.
|
|
25
|
+
* @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.
|
|
26
|
+
* @param {string} options.fileName - The name of the file to upload.
|
|
27
|
+
* @param {string} options.url - The URL of the media file to upload.
|
|
28
|
+
* @returns {Promise<string>} - The ID of the uploaded media file.
|
|
29
|
+
*/
|
|
30
|
+
export declare function uploadMediaByUrl(httpClient: HttpClient, { private: isPrivate, mediaFolderId, fileName, url, }: {
|
|
31
|
+
private?: boolean;
|
|
32
|
+
mediaFolderId?: string | null;
|
|
33
|
+
fileName: string;
|
|
34
|
+
url: string;
|
|
35
|
+
}): Promise<string>;
|
|
19
36
|
/**
|
|
20
37
|
* Retrieves the default media folder ID for a given entity.
|
|
21
38
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"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AAC;IAClB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B,GACC,OAAO,CAAC,MAAM,CAAC,CA2CjB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,gBAAgB,CACrC,UAAU,EAAE,UAAU,EACtB,EACC,OAAO,EAAE,SAAiB,EAC1B,aAAoB,EACpB,QAAQ,EACR,GAAG,GACH,EAAE;IACF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;CACZ,GACC,OAAO,CAAC,MAAM,CAAC,CAmCjB;AAGD;;;;;;GAMG;AACH,wBAAsB,6BAA6B,CAClD,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,MAAM,GACZ,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAaxB;AAED;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACzC,UAAU,EAAE,UAAU,EACtB,IAAI,EAAE,MAAM,GACV,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAYxB;AAED,UAAU,wBAAwB;IACjC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACtC,UAAU,EAAE,UAAU,EACtB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,wBAAwB,GAC/B,OAAO,CAAC,MAAM,CAAC,CAejB"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.uploadMediaFile = uploadMediaFile;
|
|
4
|
+
exports.uploadMediaByUrl = uploadMediaByUrl;
|
|
4
5
|
exports.getMediaDefaultFolderByEntity = getMediaDefaultFolderByEntity;
|
|
5
6
|
exports.getMediaFolderByName = getMediaFolderByName;
|
|
6
7
|
exports.createMediaFolder = createMediaFolder;
|
|
@@ -48,6 +49,46 @@ async function uploadMediaFile(httpClient, { private: isPrivate = false, mediaFo
|
|
|
48
49
|
throw e;
|
|
49
50
|
}
|
|
50
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Uploads a media file by URL to the Shopware instance.
|
|
54
|
+
*
|
|
55
|
+
* @param {HttpClient} httpClient - The HTTP client instance.
|
|
56
|
+
* @param {Object} options - The options for uploading the media file.
|
|
57
|
+
* @param {boolean} [options.private] - Whether the media file should be private.
|
|
58
|
+
* @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.
|
|
59
|
+
* @param {string} options.fileName - The name of the file to upload.
|
|
60
|
+
* @param {string} options.url - The URL of the media file to upload.
|
|
61
|
+
* @returns {Promise<string>} - The ID of the uploaded media file.
|
|
62
|
+
*/
|
|
63
|
+
async function uploadMediaByUrl(httpClient, { private: isPrivate = false, mediaFolderId = null, fileName, url, }) {
|
|
64
|
+
const repository = new admin_api_js_1.EntityRepository(httpClient, "media");
|
|
65
|
+
const mediaId = (0, admin_api_js_1.uuid)();
|
|
66
|
+
await repository.upsert([
|
|
67
|
+
{
|
|
68
|
+
id: mediaId,
|
|
69
|
+
private: isPrivate,
|
|
70
|
+
mediaFolderId,
|
|
71
|
+
},
|
|
72
|
+
]);
|
|
73
|
+
const splitFileName = fileName.split(".");
|
|
74
|
+
if (splitFileName.length < 2) {
|
|
75
|
+
throw new Error("Invalid file name, should have an extension");
|
|
76
|
+
}
|
|
77
|
+
const extension = (splitFileName.slice(-1)[0] || "").toLowerCase();
|
|
78
|
+
const baseFileName = splitFileName.slice(0, -1).join(".");
|
|
79
|
+
try {
|
|
80
|
+
await httpClient.post(`/_action/media/${mediaId}/upload`, {
|
|
81
|
+
fileName: baseFileName,
|
|
82
|
+
extension,
|
|
83
|
+
url,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
await repository.delete([{ id: mediaId }]);
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
90
|
+
return mediaId;
|
|
91
|
+
}
|
|
51
92
|
/**
|
|
52
93
|
* Retrieves the default media folder ID for a given entity.
|
|
53
94
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media.js","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":";;AAeA,0CAwDC;
|
|
1
|
+
{"version":3,"file":"media.js","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":";;AAeA,0CAwDC;AAaD,4CAgDC;AAUD,sEAgBC;AASD,oDAeC;AAeD,8CAmBC;AAvND,iDAAwD;AACxD,+CAAyC;AAEzC;;;;;;;;;;GAUG;AACI,KAAK,UAAU,eAAe,CACpC,UAAsB,EACtB,EACC,OAAO,EAAE,SAAS,GAAG,KAAK,EAC1B,aAAa,GAAG,IAAI,EACpB,QAAQ,EACR,IAAI,GAMJ;IAED,MAAM,UAAU,GAAG,IAAI,+BAAgB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAE7D,MAAM,OAAO,GAAG,IAAA,mBAAI,GAAE,CAAC;IAEvB,MAAM,UAAU,CAAC,MAAM,CAAC;QACvB;YACC,EAAE,EAAE,OAAO;YACX,OAAO,EAAE,SAAS;YAClB,aAAa;SACb;KACD,CAAC,CAAC;IAEH,MAAM,aAAa,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE1C,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IAChE,CAAC;IAED,MAAM,SAAS,GAAG,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IACnE,MAAM,YAAY,GAAG,aAAa,CAAC,KAAK,CA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AAU,EACV,cAAc,CACd,CAAC;IAEF,MAAM,QAAQ,GAAG,IAAI,sBAAQ,EAAE,CAAC;IAChC,QAAQ,CAAC,SAAS,CAAC,sBAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAElD,MAAM,OAAO,GAAG,MAAM,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAEnD,OAAO,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,IAAI,CAAC;AACpC,CAAC;AAMD;;;;;;;;GAQG;AACI,KAAK,UAAU,iBAAiB,CACtC,UAAsB,EACtB,IAAY,EACZ,OAAiC;IAEjC,MAAM,UAAU,GAAG,IAAI,+BAAgB,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;IAEpE,MAAM,aAAa,GAAG,IAAA,mBAAI,GAAE,CAAC;IAE7B,MAAM,UAAU,CAAC,MAAM,CAAC;QACvB;YACC,EAAE,EAAE,aAAa;YACjB,IAAI;YACJ,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,IAAI;YAClC,aAAa,EAAE,EAAE;SACjB;KACD,CAAC,CAAC;IAEH,OAAO,aAAa,CAAC;AACtB,CAAC","sourcesContent":["import type { HttpClient } from \"../http-client.js\";\nimport { EntityRepository, uuid } from \"./admin-api.js\";\nimport { Criteria } from \"./criteria.js\";\n\n/**\n * Uploads a media file to the Shopware instance.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {Object} options - The options for uploading the media file.\n * @param {boolean} [options.private] - Whether the media file should be private.\n * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.\n * @param {string} options.fileName - The name of the file to upload.\n * @param {Blob|Promise<Blob>} options.file - The file to upload.\n * @returns {Promise<string>} - The ID of the uploaded media file.\n */\nexport async function uploadMediaFile(\n\thttpClient: HttpClient,\n\t{\n\t\tprivate: isPrivate = false,\n\t\tmediaFolderId = null,\n\t\tfileName,\n\t\tfile,\n\t}: {\n\t\tprivate?: boolean;\n\t\tmediaFolderId?: string | null;\n\t\tfileName: string;\n\t\tfile: Blob | Promise<Blob>;\n\t},\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media\");\n\n\tconst mediaId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaId,\n\t\t\tprivate: isPrivate,\n\t\t\tmediaFolderId,\n\t\t},\n\t]);\n\n\tconst splitFileName = fileName.split(\".\");\n\n\tif (splitFileName.length < 2) {\n\t\tthrow new Error(\"Invalid file name, should have an extension\");\n\t}\n\n\tconst extension = (splitFileName.slice(-1)[0] || \"\").toLowerCase();\n\tconst baseFileName = splitFileName.slice(0, -1).join(\".\");\n\n\tconst params = new URLSearchParams();\n\n\tparams.append(\"extension\", extension);\n\tparams.append(\"fileName\", baseFileName);\n\n\tconst resolved = await file;\n\n\ttry {\n\t\tawait httpClient.post(\n\t\t\t`/_action/media/${mediaId}/upload?${params.toString()}`,\n\t\t\tresolved,\n\t\t\t{\n\t\t\t\t\"Content-Type\": resolved.type,\n\t\t\t},\n\t\t);\n\n\t\treturn mediaId;\n\t} catch (e) {\n\t\tawait repository.delete([{ id: mediaId }]);\n\t\tthrow e;\n\t}\n}\n\n/**\n * Uploads a media file by URL to the Shopware instance.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {Object} options - The options for uploading the media file.\n * @param {boolean} [options.private] - Whether the media file should be private.\n * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.\n * @param {string} options.fileName - The name of the file to upload.\n * @param {string} options.url - The URL of the media file to upload.\n * @returns {Promise<string>} - The ID of the uploaded media file.\n */\nexport async function uploadMediaByUrl(\n\thttpClient: HttpClient,\n\t{\n\t\tprivate: isPrivate = false,\n\t\tmediaFolderId = null,\n\t\tfileName,\n\t\turl,\n\t}: {\n\t\tprivate?: boolean;\n\t\tmediaFolderId?: string | null;\n\t\tfileName: string;\n\t\turl: string;\n\t},\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media\");\n\n\tconst mediaId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaId,\n\t\t\tprivate: isPrivate,\n\t\t\tmediaFolderId,\n\t\t},\n\t]);\n\n\tconst splitFileName = fileName.split(\".\");\n\n\tif (splitFileName.length < 2) {\n\t\tthrow new Error(\"Invalid file name, should have an extension\");\n\t}\n\n\tconst extension = (splitFileName.slice(-1)[0] || \"\").toLowerCase();\n\tconst baseFileName = splitFileName.slice(0, -1).join(\".\");\n\n\ttry {\n\t\tawait httpClient.post(`/_action/media/${mediaId}/upload`, {\n\t\t\tfileName: baseFileName,\n\t\t\textension,\n\t\t\turl,\n\t\t});\n\t} catch (e) {\n\t\tawait repository.delete([{ id: mediaId }]);\n\n\t\tthrow e;\n\t}\n\n\treturn mediaId;\n}\n\n\n/**\n * Retrieves the default media folder ID for a given entity.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} entity - The entity name to get the default folder ID for.\n * @returns {Promise<string|null>} - The ID of the default media folder or null if not found.\n */\nexport async function getMediaDefaultFolderByEntity(\n\thttpClient: HttpClient,\n\tentity: string,\n): Promise<string | null> {\n\tconst mediaDefaultFolder = new EntityRepository<{ folder: { id: string } }>(\n\t\thttpClient,\n\t\t\"media_default_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"entity\", entity));\n\n\tconst folders = await mediaDefaultFolder.search(criteria);\n\n\tconst firstFolder = folders.first();\n\treturn firstFolder?.folder?.id || null;\n}\n\n/**\n * Retrieves the media folder ID by its name.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @returns {Promise<string|null>} - The ID of the media folder or null if not found.\n */\nexport async function getMediaFolderByName(\n\thttpClient: HttpClient,\n\tname: string,\n): Promise<string | null> {\n\tconst mediaFolder = new EntityRepository<{ id: string }>(\n\t\thttpClient,\n\t\t\"media_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"name\", name));\n\n\tconst folders = await mediaFolder.search(criteria);\n\n\treturn folders.first()?.id || null;\n}\n\ninterface CreateMediaFolderOptions {\n\tparentId?: string;\n}\n\n/**\n * Creates a new media folder.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @param {Object} options - Additional options for creating the media folder.\n * @param {string} [options.parentId] - The ID of the parent folder, if any.\n * @returns {Promise<string>} - The ID of the newly created media folder.\n */\nexport async function createMediaFolder(\n\thttpClient: HttpClient,\n\tname: string,\n\toptions: CreateMediaFolderOptions,\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media_folder\");\n\n\tconst mediaFolderId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaFolderId,\n\t\t\tname,\n\t\t\tparentId: options.parentId || null,\n\t\t\tconfiguration: {},\n\t\t},\n\t]);\n\n\treturn mediaFolderId;\n}\n"]}
|
|
@@ -16,6 +16,23 @@ export declare function uploadMediaFile(httpClient: HttpClient, { private: isPri
|
|
|
16
16
|
fileName: string;
|
|
17
17
|
file: Blob | Promise<Blob>;
|
|
18
18
|
}): Promise<string>;
|
|
19
|
+
/**
|
|
20
|
+
* Uploads a media file by URL to the Shopware instance.
|
|
21
|
+
*
|
|
22
|
+
* @param {HttpClient} httpClient - The HTTP client instance.
|
|
23
|
+
* @param {Object} options - The options for uploading the media file.
|
|
24
|
+
* @param {boolean} [options.private] - Whether the media file should be private.
|
|
25
|
+
* @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.
|
|
26
|
+
* @param {string} options.fileName - The name of the file to upload.
|
|
27
|
+
* @param {string} options.url - The URL of the media file to upload.
|
|
28
|
+
* @returns {Promise<string>} - The ID of the uploaded media file.
|
|
29
|
+
*/
|
|
30
|
+
export declare function uploadMediaByUrl(httpClient: HttpClient, { private: isPrivate, mediaFolderId, fileName, url, }: {
|
|
31
|
+
private?: boolean;
|
|
32
|
+
mediaFolderId?: string | null;
|
|
33
|
+
fileName: string;
|
|
34
|
+
url: string;
|
|
35
|
+
}): Promise<string>;
|
|
19
36
|
/**
|
|
20
37
|
* Retrieves the default media folder ID for a given entity.
|
|
21
38
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAIpD;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CACpC,UAAU,EAAE,UAAU,EACtB,EACC,OAAO,EAAE,SAAiB,EAC1B,aAAoB,EACpB,QAAQ,EACR,IAAI,GACJ,EAAE;IACF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B,GACC,OAAO,CAAC,MAAM,CAAC,CA2CjB;AAED;;;;;;GAMG;AACH,wBAAsB,6BAA6B,CAClD,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,MAAM,GACZ,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAaxB;AAED;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACzC,UAAU,EAAE,UAAU,EACtB,IAAI,EAAE,MAAM,GACV,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAYxB;AAED,UAAU,wBAAwB;IACjC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACtC,UAAU,EAAE,UAAU,EACtB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,wBAAwB,GAC/B,OAAO,CAAC,MAAM,CAAC,CAejB"}
|
|
1
|
+
{"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,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ist/esm/helper/media.js
CHANGED
|
@@ -42,6 +42,46 @@ export async function uploadMediaFile(httpClient, { private: isPrivate = false,
|
|
|
42
42
|
throw e;
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Uploads a media file by URL to the Shopware instance.
|
|
47
|
+
*
|
|
48
|
+
* @param {HttpClient} httpClient - The HTTP client instance.
|
|
49
|
+
* @param {Object} options - The options for uploading the media file.
|
|
50
|
+
* @param {boolean} [options.private] - Whether the media file should be private.
|
|
51
|
+
* @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.
|
|
52
|
+
* @param {string} options.fileName - The name of the file to upload.
|
|
53
|
+
* @param {string} options.url - The URL of the media file to upload.
|
|
54
|
+
* @returns {Promise<string>} - The ID of the uploaded media file.
|
|
55
|
+
*/
|
|
56
|
+
export async function uploadMediaByUrl(httpClient, { private: isPrivate = false, mediaFolderId = null, fileName, url, }) {
|
|
57
|
+
const repository = new EntityRepository(httpClient, "media");
|
|
58
|
+
const mediaId = uuid();
|
|
59
|
+
await repository.upsert([
|
|
60
|
+
{
|
|
61
|
+
id: mediaId,
|
|
62
|
+
private: isPrivate,
|
|
63
|
+
mediaFolderId,
|
|
64
|
+
},
|
|
65
|
+
]);
|
|
66
|
+
const splitFileName = fileName.split(".");
|
|
67
|
+
if (splitFileName.length < 2) {
|
|
68
|
+
throw new Error("Invalid file name, should have an extension");
|
|
69
|
+
}
|
|
70
|
+
const extension = (splitFileName.slice(-1)[0] || "").toLowerCase();
|
|
71
|
+
const baseFileName = splitFileName.slice(0, -1).join(".");
|
|
72
|
+
try {
|
|
73
|
+
await httpClient.post(`/_action/media/${mediaId}/upload`, {
|
|
74
|
+
fileName: baseFileName,
|
|
75
|
+
extension,
|
|
76
|
+
url,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
await repository.delete([{ id: mediaId }]);
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
return mediaId;
|
|
84
|
+
}
|
|
45
85
|
/**
|
|
46
86
|
* Retrieves the default media folder ID for a given entity.
|
|
47
87
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media.js","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACpC,UAAsB,EACtB,EACC,OAAO,EAAE,SAAS,GAAG,KAAK,EAC1B,aAAa,GAAG,IAAI,EACpB,QAAQ,EACR,IAAI,GAMJ;IAED,MAAM,UAAU,GAAG,IAAI,gBAAgB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAE7D,MAAM,OAAO,GAAG,IAAI,EAAE,CAAC;IAEvB,MAAM,UAAU,CAAC,MAAM,CAAC;QACvB;YACC,EAAE,EAAE,OAAO;YACX,OAAO,EAAE,SAAS;YAClB,aAAa;SACb;KACD,CAAC,CAAC;IAEH,MAAM,aAAa,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE1C,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IAChE,CAAC;IAED,MAAM,SAAS,GAAG,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IACnE,MAAM,YAAY,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAE1D,MAAM,MAAM,GAAG,IAAI,eAAe,EAAE,CAAC;IAErC,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,SAAS,C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,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAElD,MAAM,OAAO,GAAG,MAAM,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAEnD,OAAO,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,IAAI,CAAC;AACpC,CAAC;AAMD;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACtC,UAAsB,EACtB,IAAY,EACZ,OAAiC;IAEjC,MAAM,UAAU,GAAG,IAAI,gBAAgB,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;IAEpE,MAAM,aAAa,GAAG,IAAI,EAAE,CAAC;IAE7B,MAAM,UAAU,CAAC,MAAM,CAAC;QACvB;YACC,EAAE,EAAE,aAAa;YACjB,IAAI;YACJ,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,IAAI;YAClC,aAAa,EAAE,EAAE;SACjB;KACD,CAAC,CAAC;IAEH,OAAO,aAAa,CAAC;AACtB,CAAC","sourcesContent":["import type { HttpClient } from \"../http-client.js\";\nimport { EntityRepository, uuid } from \"./admin-api.js\";\nimport { Criteria } from \"./criteria.js\";\n\n/**\n * Uploads a media file to the Shopware instance.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {Object} options - The options for uploading the media file.\n * @param {boolean} [options.private] - Whether the media file should be private.\n * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.\n * @param {string} options.fileName - The name of the file to upload.\n * @param {Blob|Promise<Blob>} options.file - The file to upload.\n * @returns {Promise<string>} - The ID of the uploaded media file.\n */\nexport async function uploadMediaFile(\n\thttpClient: HttpClient,\n\t{\n\t\tprivate: isPrivate = false,\n\t\tmediaFolderId = null,\n\t\tfileName,\n\t\tfile,\n\t}: {\n\t\tprivate?: boolean;\n\t\tmediaFolderId?: string | null;\n\t\tfileName: string;\n\t\tfile: Blob | Promise<Blob>;\n\t},\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media\");\n\n\tconst mediaId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaId,\n\t\t\tprivate: isPrivate,\n\t\t\tmediaFolderId,\n\t\t},\n\t]);\n\n\tconst splitFileName = fileName.split(\".\");\n\n\tif (splitFileName.length < 2) {\n\t\tthrow new Error(\"Invalid file name, should have an extension\");\n\t}\n\n\tconst extension = (splitFileName.slice(-1)[0] || \"\").toLowerCase();\n\tconst baseFileName = splitFileName.slice(0, -1).join(\".\");\n\n\tconst params = new URLSearchParams();\n\n\tparams.append(\"extension\", extension);\n\tparams.append(\"fileName\", baseFileName);\n\n\tconst resolved = await file;\n\n\ttry {\n\t\tawait httpClient.post(\n\t\t\t`/_action/media/${mediaId}/upload?${params.toString()}`,\n\t\t\tresolved,\n\t\t\t{\n\t\t\t\t\"Content-Type\": resolved.type,\n\t\t\t},\n\t\t);\n\n\t\treturn mediaId;\n\t} catch (e) {\n\t\tawait repository.delete([{ id: mediaId }]);\n\t\tthrow e;\n\t}\n}\n\n/**\n * Retrieves the default media folder ID for a given entity.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} entity - The entity name to get the default folder ID for.\n * @returns {Promise<string|null>} - The ID of the default media folder or null if not found.\n */\nexport async function getMediaDefaultFolderByEntity(\n\thttpClient: HttpClient,\n\tentity: string,\n): Promise<string | null> {\n\tconst mediaDefaultFolder = new EntityRepository<{ folder: { id: string } }>(\n\t\thttpClient,\n\t\t\"media_default_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"entity\", entity));\n\n\tconst folders = await mediaDefaultFolder.search(criteria);\n\n\tconst firstFolder = folders.first();\n\treturn firstFolder?.folder?.id || null;\n}\n\n/**\n * Retrieves the media folder ID by its name.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @returns {Promise<string|null>} - The ID of the media folder or null if not found.\n */\nexport async function getMediaFolderByName(\n\thttpClient: HttpClient,\n\tname: string,\n): Promise<string | null> {\n\tconst mediaFolder = new EntityRepository<{ id: string }>(\n\t\thttpClient,\n\t\t\"media_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"name\", name));\n\n\tconst folders = await mediaFolder.search(criteria);\n\n\treturn folders.first()?.id || null;\n}\n\ninterface CreateMediaFolderOptions {\n\tparentId?: string;\n}\n\n/**\n * Creates a new media folder.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @param {Object} options - Additional options for creating the media folder.\n * @param {string} [options.parentId] - The ID of the parent folder, if any.\n * @returns {Promise<string>} - The ID of the newly created media folder.\n */\nexport async function createMediaFolder(\n\thttpClient: HttpClient,\n\tname: string,\n\toptions: CreateMediaFolderOptions,\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media_folder\");\n\n\tconst mediaFolderId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaFolderId,\n\t\t\tname,\n\t\t\tparentId: options.parentId || null,\n\t\t\tconfiguration: {},\n\t\t},\n\t]);\n\n\treturn mediaFolderId;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"media.js","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACpC,UAAsB,EACtB,EACC,OAAO,EAAE,SAAS,GAAG,KAAK,EAC1B,aAAa,GAAG,IAAI,EACpB,QAAQ,EACR,IAAI,GAMJ;IAED,MAAM,UAAU,GAAG,IAAI,gBAAgB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAE7D,MAAM,OAAO,GAAG,IAAI,EAAE,CAAC;IAEvB,MAAM,UAAU,CAAC,MAAM,C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esContent":["import type { HttpClient } from \"../http-client.js\";\nimport { EntityRepository, uuid } from \"./admin-api.js\";\nimport { Criteria } from \"./criteria.js\";\n\n/**\n * Uploads a media file to the Shopware instance.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {Object} options - The options for uploading the media file.\n * @param {boolean} [options.private] - Whether the media file should be private.\n * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.\n * @param {string} options.fileName - The name of the file to upload.\n * @param {Blob|Promise<Blob>} options.file - The file to upload.\n * @returns {Promise<string>} - The ID of the uploaded media file.\n */\nexport async function uploadMediaFile(\n\thttpClient: HttpClient,\n\t{\n\t\tprivate: isPrivate = false,\n\t\tmediaFolderId = null,\n\t\tfileName,\n\t\tfile,\n\t}: {\n\t\tprivate?: boolean;\n\t\tmediaFolderId?: string | null;\n\t\tfileName: string;\n\t\tfile: Blob | Promise<Blob>;\n\t},\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media\");\n\n\tconst mediaId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaId,\n\t\t\tprivate: isPrivate,\n\t\t\tmediaFolderId,\n\t\t},\n\t]);\n\n\tconst splitFileName = fileName.split(\".\");\n\n\tif (splitFileName.length < 2) {\n\t\tthrow new Error(\"Invalid file name, should have an extension\");\n\t}\n\n\tconst extension = (splitFileName.slice(-1)[0] || \"\").toLowerCase();\n\tconst baseFileName = splitFileName.slice(0, -1).join(\".\");\n\n\tconst params = new URLSearchParams();\n\n\tparams.append(\"extension\", extension);\n\tparams.append(\"fileName\", baseFileName);\n\n\tconst resolved = await file;\n\n\ttry {\n\t\tawait httpClient.post(\n\t\t\t`/_action/media/${mediaId}/upload?${params.toString()}`,\n\t\t\tresolved,\n\t\t\t{\n\t\t\t\t\"Content-Type\": resolved.type,\n\t\t\t},\n\t\t);\n\n\t\treturn mediaId;\n\t} catch (e) {\n\t\tawait repository.delete([{ id: mediaId }]);\n\t\tthrow e;\n\t}\n}\n\n/**\n * Uploads a media file by URL to the Shopware instance.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {Object} options - The options for uploading the media file.\n * @param {boolean} [options.private] - Whether the media file should be private.\n * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.\n * @param {string} options.fileName - The name of the file to upload.\n * @param {string} options.url - The URL of the media file to upload.\n * @returns {Promise<string>} - The ID of the uploaded media file.\n */\nexport async function uploadMediaByUrl(\n\thttpClient: HttpClient,\n\t{\n\t\tprivate: isPrivate = false,\n\t\tmediaFolderId = null,\n\t\tfileName,\n\t\turl,\n\t}: {\n\t\tprivate?: boolean;\n\t\tmediaFolderId?: string | null;\n\t\tfileName: string;\n\t\turl: string;\n\t},\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media\");\n\n\tconst mediaId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaId,\n\t\t\tprivate: isPrivate,\n\t\t\tmediaFolderId,\n\t\t},\n\t]);\n\n\tconst splitFileName = fileName.split(\".\");\n\n\tif (splitFileName.length < 2) {\n\t\tthrow new Error(\"Invalid file name, should have an extension\");\n\t}\n\n\tconst extension = (splitFileName.slice(-1)[0] || \"\").toLowerCase();\n\tconst baseFileName = splitFileName.slice(0, -1).join(\".\");\n\n\ttry {\n\t\tawait httpClient.post(`/_action/media/${mediaId}/upload`, {\n\t\t\tfileName: baseFileName,\n\t\t\textension,\n\t\t\turl,\n\t\t});\n\t} catch (e) {\n\t\tawait repository.delete([{ id: mediaId }]);\n\n\t\tthrow e;\n\t}\n\n\treturn mediaId;\n}\n\n\n/**\n * Retrieves the default media folder ID for a given entity.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} entity - The entity name to get the default folder ID for.\n * @returns {Promise<string|null>} - The ID of the default media folder or null if not found.\n */\nexport async function getMediaDefaultFolderByEntity(\n\thttpClient: HttpClient,\n\tentity: string,\n): Promise<string | null> {\n\tconst mediaDefaultFolder = new EntityRepository<{ folder: { id: string } }>(\n\t\thttpClient,\n\t\t\"media_default_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"entity\", entity));\n\n\tconst folders = await mediaDefaultFolder.search(criteria);\n\n\tconst firstFolder = folders.first();\n\treturn firstFolder?.folder?.id || null;\n}\n\n/**\n * Retrieves the media folder ID by its name.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @returns {Promise<string|null>} - The ID of the media folder or null if not found.\n */\nexport async function getMediaFolderByName(\n\thttpClient: HttpClient,\n\tname: string,\n): Promise<string | null> {\n\tconst mediaFolder = new EntityRepository<{ id: string }>(\n\t\thttpClient,\n\t\t\"media_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"name\", name));\n\n\tconst folders = await mediaFolder.search(criteria);\n\n\treturn folders.first()?.id || null;\n}\n\ninterface CreateMediaFolderOptions {\n\tparentId?: string;\n}\n\n/**\n * Creates a new media folder.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @param {Object} options - Additional options for creating the media folder.\n * @param {string} [options.parentId] - The ID of the parent folder, if any.\n * @returns {Promise<string>} - The ID of the newly created media folder.\n */\nexport async function createMediaFolder(\n\thttpClient: HttpClient,\n\tname: string,\n\toptions: CreateMediaFolderOptions,\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media_folder\");\n\n\tconst mediaFolderId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaFolderId,\n\t\t\tname,\n\t\t\tparentId: options.parentId || null,\n\t\t\tconfiguration: {},\n\t\t},\n\t]);\n\n\treturn mediaFolderId;\n}\n"]}
|