Skip to content

Commit 7e46c3e

Browse files
committed
removed multi
1 parent 3a80d36 commit 7e46c3e

File tree

5 files changed

+33
-77
lines changed

5 files changed

+33
-77
lines changed

src/Lighthouse/upload/files/browser.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@ import {
55
UploadFileReturnType,
66
DealParameters,
77
} from '../../../types'
8-
import { checkDuplicateFileNames, retryFetch } from '../../utils/util'
8+
import file from '../../uploadEncrypted/encrypt/file'
9+
import { fetchWithTimeout } from '../../utils/util'
910

1011
// eslint-disable-next-line @typescript-eslint/no-empty-function
1112
export default async <T extends boolean>(
1213
files: any,
1314
accessToken: string,
14-
multi: boolean,
1515
dealParameters: DealParameters | undefined,
1616
uploadProgressCallback?: (data: IUploadProgressCallback) => void
1717
): Promise<{ data: UploadFileReturnType<T> }> => {
1818
try {
19-
const endpoint =
20-
lighthouseConfig.lighthouseNode +
21-
`/api/v0/add?wrap-with-directory=${multi}`
22-
checkDuplicateFileNames(files)
19+
const isDirectory = files.length === 1 && files[0].webkitRelativePath
20+
let endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=false`
21+
22+
if(!isDirectory && files.length > 1) {
23+
endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=true`
24+
}
2325

2426
const formData = new FormData()
2527
for (let i = 0; i < files.length; i++) {
@@ -36,7 +38,7 @@ export default async <T extends boolean>(
3638
})
3739

3840
const response = uploadProgressCallback
39-
? await retryFetch(endpoint, {
41+
? await fetchWithTimeout(endpoint, {
4042
method: 'POST',
4143
body: formData,
4244
headers: headers,
@@ -47,7 +49,7 @@ export default async <T extends boolean>(
4749
})
4850
},
4951
})
50-
: await retryFetch(endpoint, {
52+
: await fetchWithTimeout(endpoint, {
5153
method: 'POST',
5254
body: formData,
5355
headers: headers,
@@ -59,20 +61,10 @@ export default async <T extends boolean>(
5961
}
6062

6163
const responseText = await response.text()
64+
console.log(responseText)
65+
console.log(typeof(responseText))
6266

63-
let data
64-
if (typeof responseText === 'string') {
65-
if (multi) {
66-
data = JSON.parse(
67-
`[${responseText.slice(0, -1)}]`.split('\n').join(',')
68-
)
69-
} else {
70-
const temp = responseText.split('\n')
71-
data = JSON.parse(temp[temp.length - 2])
72-
}
73-
}
74-
75-
return { data }
67+
return { data: JSON.parse(responseText) }
7668
} catch (error: any) {
7769
throw new Error(error?.message)
7870
}

src/Lighthouse/upload/files/index.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,34 @@ import {
99
async function uploadFiles(
1010
sourcePath: string | any,
1111
apiKey: string,
12-
multi?: false,
1312
dealParameters?: DealParameters,
1413
uploadProgressCallback?: (data: IUploadProgressCallback) => void
1514
): Promise<{ data: IFileUploadedResponse }>
1615

1716
async function uploadFiles(
1817
sourcePath: string | any,
1918
apiKey: string,
20-
multi?: true,
2119
dealParameters?: DealParameters,
2220
uploadProgressCallback?: (data: IUploadProgressCallback) => void
2321
): Promise<{ data: IFileUploadedResponse[] }>
2422

2523
async function uploadFiles(
2624
path: string | any,
2725
apiKey: string,
28-
multi?: boolean,
2926
dealParameters?: DealParameters,
3027
uploadProgressCallback?: (data: IUploadProgressCallback) => void
3128
) {
3229
// Upload File to IPFS
33-
34-
if (multi) {
35-
//@ts-ignore
36-
if (typeof window === 'undefined') {
37-
return await uploadFile(path, apiKey, true, dealParameters)
38-
} else {
39-
return await uploadFileBrowser(
40-
path,
41-
apiKey,
42-
true,
43-
dealParameters,
44-
uploadProgressCallback
45-
)
46-
}
30+
//@ts-ignore
31+
if (typeof window === 'undefined') {
32+
return await uploadFile(path, apiKey, dealParameters)
4733
} else {
48-
//@ts-ignore
49-
if (typeof window === 'undefined') {
50-
return await uploadFile(path, apiKey, false, dealParameters)
51-
} else {
52-
return await uploadFileBrowser(
53-
path,
54-
apiKey,
55-
false,
56-
dealParameters,
57-
uploadProgressCallback
58-
)
59-
}
34+
return await uploadFileBrowser(
35+
path,
36+
apiKey,
37+
dealParameters,
38+
uploadProgressCallback
39+
)
6040
}
6141
}
6242

src/Lighthouse/upload/files/node.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import basePathConvert from '../../utils/basePathConvert'
22
import { lighthouseConfig } from '../../../lighthouse.config'
33
import { UploadFileReturnType, DealParameters } from '../../../types'
4-
import { retryFetch } from '../../utils/util'
4+
import { fetchWithTimeout } from '../../utils/util'
55

66
export async function walk(dir: string) {
77
const { readdir, stat } = eval(`require`)('fs-extra')
@@ -25,7 +25,6 @@ export async function walk(dir: string) {
2525
export default async <T extends boolean>(
2626
sourcePath: string,
2727
apiKey: string,
28-
multi: boolean,
2928
dealParameters: DealParameters | undefined
3029
): Promise<{ data: UploadFileReturnType<T> }> => {
3130
const { createReadStream, lstatSync } = eval(`require`)('fs-extra')
@@ -36,7 +35,7 @@ export default async <T extends boolean>(
3635
try {
3736
const endpoint =
3837
lighthouseConfig.lighthouseNode +
39-
`/api/v0/add?wrap-with-directory=${multi}`
38+
`/api/v0/add?wrap-with-directory=false`
4039
if (stats.isFile()) {
4140
const data = new FormData()
4241
const stream = createReadStream(sourcePath)
@@ -48,7 +47,7 @@ export default async <T extends boolean>(
4847

4948
data.append('file', blob, path.basename(sourcePath))
5049

51-
const response = await retryFetch(endpoint, {
50+
const response = await fetchWithTimeout(endpoint, {
5251
method: 'POST',
5352
body: data,
5453
credentials: 'include',
@@ -67,12 +66,7 @@ export default async <T extends boolean>(
6766
}
6867

6968
let responseData = (await response.text()) as any
70-
if (multi) {
71-
const temp = responseData.split('\n')
72-
responseData = JSON.parse(temp[temp.length - 2])
73-
} else {
74-
responseData = JSON.parse(responseData)
75-
}
69+
responseData = JSON.parse(responseData)
7670

7771
return { data: responseData }
7872
} else {
@@ -90,11 +84,11 @@ export default async <T extends boolean>(
9084
data.append(
9185
'file',
9286
blob,
93-
multi ? path.basename(file) : basePathConvert(sourcePath, file)
87+
basePathConvert(sourcePath, file)
9488
)
9589
}
9690

97-
const response = await retryFetch(endpoint, {
91+
const response = await fetchWithTimeout(endpoint, {
9892
method: 'POST',
9993
body: data,
10094
credentials: 'include',
@@ -113,17 +107,7 @@ export default async <T extends boolean>(
113107
}
114108

115109
let responseData = (await response.text()) as any
116-
117-
if (typeof responseData === 'string') {
118-
if (multi) {
119-
responseData = JSON.parse(
120-
`[${responseData.slice(0, -1)}]`.split('\n').join(',')
121-
)
122-
} else {
123-
const temp = responseData.split('\n')
124-
responseData = JSON.parse(temp[temp.length - 2])
125-
}
126-
}
110+
responseData = JSON.parse(responseData)
127111

128112
return { data: responseData }
129113
}

src/Lighthouse/upload/text/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { lighthouseConfig } from '../../../lighthouse.config'
2-
import { retryFetch } from '../../utils/util'
2+
import { fetchWithTimeout } from '../../utils/util'
33

44
export default async (text: string, apiKey: string, name: string) => {
55
try {
@@ -11,7 +11,7 @@ export default async (text: string, apiKey: string, name: string) => {
1111
const blob = new Blob([text], { type: 'text/plain' })
1212
formData.append('file', blob, name)
1313

14-
const response = await retryFetch(endpoint, {
14+
const response = await fetchWithTimeout(endpoint, {
1515
method: 'POST',
1616
body: formData,
1717
timeout: 7200000,

src/Lighthouse/upload/text/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { lighthouseConfig } from '../../../lighthouse.config'
2-
import { retryFetch } from '../../utils/util'
2+
import { fetchWithTimeout } from '../../utils/util'
33

44
export default async (text: string, apiKey: string, name: string) => {
55
try {
@@ -12,7 +12,7 @@ export default async (text: string, apiKey: string, name: string) => {
1212

1313
formData.append('file', blob, name)
1414

15-
const response = await retryFetch(endpoint, {
15+
const response = await fetchWithTimeout(endpoint, {
1616
method: 'POST',
1717
body: formData,
1818
credentials: 'include',

0 commit comments

Comments
 (0)