@e-mc/cloud
Advanced tools
Comparing version 0.8.3 to 0.8.4
72
index.js
@@ -26,13 +26,15 @@ "use strict"; | ||
function getFiles(file, data) { | ||
const grouped = file.localUri ? [file.localUri] : []; | ||
const localUri = file.localUri; | ||
const grouped = [localUri && core_1.ClientDb.isPath(localUri, true) ? localUri : '']; | ||
if (data.all) { | ||
const individual = new Set(); | ||
if (!file.cloudUrl) { | ||
file.transforms?.forEach(value => individual.add(value)); | ||
} | ||
file.torrentFiles?.forEach(value => individual.add(value)); | ||
if (file.descendants) { | ||
grouped.push(...file.descendants); | ||
} | ||
return [grouped, Array.from(individual)]; | ||
const individual = !file.cloudUrl && file.transforms || []; | ||
if (file.torrentFiles) { | ||
individual.push(...file.torrentFiles); | ||
} | ||
if (individual.length) { | ||
return [grouped, Array.from(new Set(individual))]; | ||
} | ||
} | ||
@@ -165,3 +167,3 @@ return [grouped]; | ||
for (const [item, data] of localStorage) { | ||
getFiles(item, data).forEach(group => group.forEach(value => this.deleteFile(value, { emptyDir: true }))); | ||
getFiles(item, data).forEach(group => group.forEach(value => value && this.deleteFile(value, { emptyDir: true }))); | ||
} | ||
@@ -177,3 +179,2 @@ for (const item of this.assets) { | ||
const { pathname, filename, waitStatus, overwrite } = download; | ||
let active = download.active; | ||
if (!filename) { | ||
@@ -183,3 +184,3 @@ continue; | ||
const localUri = item.localUri; | ||
let downloadUri; | ||
let active = download.active, downloadUri; | ||
if (pathname && path.isAbsolute(pathname)) { | ||
@@ -203,2 +204,3 @@ downloadUri = path.join(pathname, filename); | ||
else if (!Cloud.createDir(destDir)) { | ||
instance.writeFail(["Unable to create directory" /* ERR_MESSAGE.CREATE_DIRECTORY */, filename], (0, types_1.errorValue)("Path is not a directory" /* ERR_MESSAGE.NOT_DIRECTORY */, destDir), { type: 64 /* LOG_TYPE.CLOUD */, fatal: !!active, startTime }); | ||
continue; | ||
@@ -277,7 +279,8 @@ } | ||
const { host, instance } = state; | ||
if (instance.aborted) { | ||
const cloudStorage = file.cloudStorage; | ||
if (instance.aborted || !Array.isArray(cloudStorage)) { | ||
return []; | ||
} | ||
const tasks = []; | ||
for (const storage of file.cloudStorage) { | ||
for (const storage of cloudStorage) { | ||
if (!instance.hasStorage('upload', storage)) { | ||
@@ -287,3 +290,3 @@ continue; | ||
const upload = storage.upload; | ||
const active = storage === instance.getStorage('upload', file.cloudStorage); | ||
const active = storage === instance.getStorage('upload', cloudStorage); | ||
if (active && upload.localStorage === false) { | ||
@@ -311,26 +314,33 @@ state.localStorage.set(file, upload); | ||
let fileGroup; | ||
if (index === 0 && group.length > 1) { | ||
if (SERVICE_CLIENT.get(service)?.CLOUD_UPLOAD_FROMDISK) { | ||
fileGroup = group.slice(1).filter(value => this.isPath(value)).map(value => [value, path.extname(value), value]); | ||
if (index === 0) { | ||
if (!group[0]) { | ||
instance.writeFail("Unable to read file" /* ERR_MESSAGE.READ_FILE */, (0, types_1.errorValue)("File not found" /* ERR_MESSAGE.NOTFOUND_FILE */, service), { type: 64 /* LOG_TYPE.CLOUD */, fatal: true }); | ||
return; | ||
} | ||
else { | ||
fileGroup = []; | ||
for (let i = 1; i < group.length; ++i) { | ||
const value = group[i]; | ||
try { | ||
if (fs.existsSync(value)) { | ||
fileGroup.push([typeof minStreamSize === 'number' ? await this.streamFile(value, { minStreamSize, cache: false, signal: instance.signal }) : fs.readFileSync(value), path.extname(value), value]); | ||
if (group.length > 1) { | ||
if (SERVICE_CLIENT.get(service)?.CLOUD_UPLOAD_FROMDISK) { | ||
fileGroup = group.slice(1).filter(value => this.isPath(value, true)).map(value => [value, path.extname(value), value]); | ||
} | ||
else { | ||
fileGroup = []; | ||
for (let i = 1; i < group.length; ++i) { | ||
const value = group[i]; | ||
if (this.isPath(value, true)) { | ||
try { | ||
fileGroup.push([typeof minStreamSize === 'number' ? await this.streamFile(value, { minStreamSize, cache: false, signal: instance.signal }) : fs.readFileSync(value), path.extname(value), value]); | ||
} | ||
catch (err) { | ||
instance.writeFail(["Unable to read file" /* ERR_MESSAGE.READ_FILE */, path.basename(value)], err, { type: 32 /* LOG_TYPE.FILE */, fatal: false }); | ||
} | ||
} | ||
} | ||
catch (err) { | ||
instance.writeFail(["Unable to read file" /* ERR_MESSAGE.READ_FILE */, path.basename(value)], err, { type: 32 /* LOG_TYPE.FILE */, fatal: false }); | ||
} | ||
} | ||
group = [group[0]]; | ||
} | ||
group = [group[0]]; | ||
} | ||
for (const localUri of group) { | ||
const exists = this.isPath(localUri); | ||
for (let i = 0; i < group.length; ++i) { | ||
const localUri = group[i]; | ||
const exists = index === 0 || this.isPath(localUri, true); | ||
if (!exists || !instance.canRead(localUri, { ownPermissionOnly: true })) { | ||
instance.writeFail(["Unable to read file" /* ERR_MESSAGE.READ_FILE */, path.basename(localUri)], (0, types_1.errorValue)(exists ? "Not permitted to read file" /* ERR_MESSAGE.UNSUPPORTED_READ */ : "File not found" /* ERR_MESSAGE.NOTFOUND_FILE */, localUri), { type: 64 /* LOG_TYPE.CLOUD */, fatal: index === 0 }); | ||
instance.writeFail(["Unable to read file" /* ERR_MESSAGE.READ_FILE */, path.basename(localUri)], (0, types_1.errorValue)(exists ? "Not permitted to read file" /* ERR_MESSAGE.UNSUPPORTED_READ */ : "File not found" /* ERR_MESSAGE.NOTFOUND_FILE */, localUri), { type: 64 /* LOG_TYPE.CLOUD */, fatal: i === 0 && index === 0 }); | ||
continue; | ||
@@ -359,3 +369,3 @@ } | ||
uploading.push(instance.uploadObject(service, { ...credential }, bucket, options, localUri, callback) | ||
.catch(err => instance.writeFail(["Upload failed" /* ERR_CLOUD.UPLOAD_FAIL */, path.basename(localUri)], err, { type: 64 /* LOG_TYPE.CLOUD */, fatal: index === 0 }))); | ||
.catch(err => instance.writeFail(["Upload failed" /* ERR_CLOUD.UPLOAD_FAIL */, path.basename(localUri)], err, { type: 64 /* LOG_TYPE.CLOUD */, fatal: i === 0 && index === 0 }))); | ||
} | ||
@@ -362,0 +372,0 @@ }); |
{ | ||
"name": "@e-mc/cloud", | ||
"version": "0.8.3", | ||
"version": "0.8.4", | ||
"description": "Cloud constructor for E-mc.", | ||
@@ -23,7 +23,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"@e-mc/core": "0.8.3", | ||
"@e-mc/db": "0.8.3", | ||
"@e-mc/types": "0.8.3", | ||
"@e-mc/core": "0.8.4", | ||
"@e-mc/db": "0.8.4", | ||
"@e-mc/types": "0.8.4", | ||
"mime-types": "^2.1.35" | ||
} | ||
} |
@@ -1,7 +0,80 @@ | ||
### @e-mc/cloud | ||
# @e-mc/cloud | ||
https://e-mc.readthedocs.io | ||
* NodeJS 14 | ||
* ES2020 | ||
### LICENSE | ||
## General Usage | ||
* [Read the Docs](https://e-mc.readthedocs.io) | ||
## Interface | ||
- https://www.unpkg.com/@e-mc/types@0.8.4/lib/index.d.ts | ||
```typescript | ||
import type { IHost, IScopeOrigin } from "./index"; | ||
import type { ExternalAsset } from "./asset"; | ||
import type { BucketWebsiteOptions, CloudDatabase, CloudFeatures, CloudFunctions, CloudService, CloudStorage, CloudStorageDownload, CloudStorageUpload } from "./cloud"; | ||
import type { ClientDbConstructor, IClientDb } from "./core"; | ||
import type { BatchQueryResult, QueryResult } from "./db"; | ||
import type { LogMessageOptions } from "./logger"; | ||
import type { CloudModule, CloudServiceOptions, CloudSettings, DbCoerceSettings } from "./settings"; | ||
interface ICloud extends IClientDb<IHost, CloudModule, CloudDatabase, CloudServiceOptions, DbCoerceSettings> { | ||
module: CloudModule; | ||
readonly uploaded: string[]; | ||
readonly downloaded: string[]; | ||
createBucket(service: string, credential: unknown, bucket: string, acl?: unknown, options?: unknown): Promise<boolean>; | ||
createBucket(service: string, credential: unknown, bucket: string, publicRead?: boolean): Promise<boolean>; | ||
setBucketPolicy(service: string, credential: unknown, bucket: string, options: unknown): Promise<boolean>; | ||
setBucketWebsite(service: string, credential: unknown, bucket: string, options: BucketWebsiteOptions): Promise<boolean>; | ||
deleteObjects(service: string, credential: unknown, bucket: string, recursive?: boolean): Promise<void>; | ||
uploadObject(service: string, credential: unknown, bucket: string, upload: CloudStorageUpload, localUri: string, beforeResolve?: (value: string) => Promise<void> | void): Promise<string>; | ||
downloadObject(service: string, credential: unknown, bucket: string, download: CloudStorageDownload, beforeResolve?: (value: Buffer | string | null) => Promise<string | undefined> | void): Promise<Buffer | string>; | ||
getStorage(action: CloudFunctions, data: CloudStorage[] | undefined): CloudStorage | undefined; | ||
hasStorage(action: CloudFunctions, storage: CloudStorage): CloudStorageUpload | false; | ||
getDatabaseRows(item: CloudDatabase, ignoreErrors: boolean, sessionKey?: string): Promise<QueryResult>; | ||
getDatabaseRows(item: CloudDatabase, sessionKey?: string): Promise<QueryResult>; | ||
getDatabaseBatchRows(batch: CloudDatabase[], ignoreErrors: boolean, sessionKey?: string): Promise<BatchQueryResult>; | ||
getDatabaseBatchRows(batch: CloudDatabase[], sessionKey?: string): Promise<BatchQueryResult>; | ||
hasCredential(feature: CloudFeatures, data: CloudService, credential?: unknown): boolean; | ||
getCredential(item: CloudService, unused?: boolean): Record<string | number | symbol, unknown>; | ||
getSettings(service: string): Record<string, unknown> | undefined; | ||
settingsOf(service: string, name: "cache"): unknown; | ||
settingsOf(service: string, name: "coerce", component: keyof DbCoerceSettings): unknown; | ||
getUploadHandler(service: string, credential: unknown): (...args: unknown[]) => void; | ||
getDownloadHandler(service: string, credential: unknown): (...args: unknown[]) => void; | ||
resolveService(service: string, folder?: string): string; | ||
getUserSettings(): unknown; | ||
get settings(): CloudSettings; | ||
} | ||
interface CloudConstructor extends ClientDbConstructor<IHost> { | ||
LOG_CLOUD_FAIL: LogMessageOptions; | ||
LOG_CLOUD_COMMAND: LogMessageOptions; | ||
LOG_CLOUD_WARN: LogMessageOptions; | ||
LOG_CLOUD_UPLOAD: LogMessageOptions; | ||
LOG_CLOUD_DOWNLOAD: LogMessageOptions; | ||
LOG_CLOUD_DELETE: LogMessageOptions; | ||
LOG_CLOUD_DELAYED: LogMessageOptions; | ||
finalize(this: IHost, instance: ICloud): Promise<unknown>; | ||
uploadAsset(state: IScopeOrigin<IFileManager, ICloud<IFileManager>>, file: ExternalAsset, ignoreProcess: boolean): Promise<unknown>[]; | ||
uploadAsset(state: IScopeOrigin<IFileManager, ICloud<IFileManager>>, file: ExternalAsset, contentType?: string, ignoreProcess?: boolean): Promise<unknown>[]; | ||
sanitizeAssets(assets: ExternalAsset[]): ExternalAsset[]; | ||
readonly prototype: ICloud; | ||
new(module?: CloudModule, database?: CloudDatabase[], ...args: unknown[]): ICloud; | ||
} | ||
``` | ||
## References | ||
- https://www.unpkg.com/@e-mc/types@0.8.4/lib/asset.d.ts | ||
- https://www.unpkg.com/@e-mc/types@0.8.4/lib/cloud.d.ts | ||
- https://www.unpkg.com/@e-mc/types@0.8.4/lib/core.d.ts | ||
- https://www.unpkg.com/@e-mc/types@0.8.4/lib/db.d.ts | ||
- https://www.unpkg.com/@e-mc/types@0.8.4/lib/logger.d.ts | ||
- https://www.unpkg.com/@e-mc/types@0.8.4/lib/settings.d.ts | ||
## LICENSE | ||
BSD 3-Clause |
40
util.js
@@ -45,28 +45,26 @@ "use strict"; | ||
for (let [content, ext, localFile] of items) { | ||
try { | ||
switch (typeof content) { | ||
case 'string': | ||
if (fs.existsSync(content)) { | ||
content = fs.readFileSync(content); | ||
} | ||
else if (path.isAbsolute(content)) { | ||
continue; | ||
} | ||
else { | ||
content = Buffer.from(content); | ||
} | ||
break; | ||
case 'object': | ||
break; | ||
default: | ||
continue; | ||
let buffer; | ||
if (typeof content === 'string') { | ||
try { | ||
if (content === localFile || fs.existsSync(content)) { | ||
buffer = fs.readFileSync(content); | ||
localFile = content; | ||
} | ||
else { | ||
buffer = Buffer.from(content); | ||
} | ||
} | ||
catch (err) { | ||
errorCallback?.(err); | ||
} | ||
} | ||
else if (Buffer.isBuffer(content)) { | ||
buffer = content; | ||
} | ||
if (buffer) { | ||
const output = filename + ext; | ||
key.push(ext === '.map' && localFile ? path.basename(localFile) : output); | ||
body.push(content); | ||
body.push(buffer); | ||
type.push(ext !== '.map' && mime.lookup(output) || mimeType || 'application/octet-stream'); | ||
} | ||
catch (err) { | ||
errorCallback?.(err); | ||
} | ||
} | ||
@@ -73,0 +71,0 @@ return [key, body, type]; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54369
952
80
+ Added@e-mc/core@0.8.4(transitive)
+ Added@e-mc/db@0.8.4(transitive)
+ Added@e-mc/module@0.8.4(transitive)
+ Added@e-mc/request@0.8.4(transitive)
+ Added@e-mc/types@0.8.4(transitive)
- Removed@e-mc/core@0.8.3(transitive)
- Removed@e-mc/db@0.8.3(transitive)
- Removed@e-mc/module@0.8.3(transitive)
- Removed@e-mc/request@0.8.3(transitive)
- Removed@e-mc/types@0.8.3(transitive)
Updated@e-mc/core@0.8.4
Updated@e-mc/db@0.8.4
Updated@e-mc/types@0.8.4