@@ -133,7 +133,7 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
133133 }
134134
135135 private suspend fun uploadAbsoluteFile (file : AbsoluteFile ): RapidUploadInfo {
136- val url = requireNotNull(file.getUrl()) { " 远程文件不存在 " }
136+ val url = requireNotNull(file.getUrl()) { " 远程文件 URL 获取失败 " }
137137 val rapid = with (file) {
138138 val content = file.md5.toUHexString(" " ).lowercase()
139139 val slice = if (size <= SLICE_SIZE ) {
@@ -151,27 +151,29 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
151151 }
152152
153153 // 用群号做根目录
154- createDir (path = " ${file.contact.id} " )
154+ mkdir (path = " ${file.contact.id} " )
155155 logger.info { " upload ${rapid.format()} " }
156156
157157 // 尝试秒传
158158 try {
159- rapidUploadFile(info = rapid)
159+ rapid(upload = rapid)
160160 return rapid
161161 } catch (throwable: IllegalArgumentException ) {
162162 logger.info { " 文件 ${file.name} 秒传失败, 进入文件上传, ${throwable.message} " }
163163 } catch (exception: Throwable ) {
164- logger.info ({ " 文件 ${file.name} 秒传失败, 进入文件上传" }, exception)
164+ logger.warning ({ " 文件 ${file.name} 秒传失败, 进入文件上传" }, exception)
165165 }
166166
167- val user = getUserInfo ()
167+ val user = rest.user ()
168168 check(file.size <= user.vip.updateLimit) { " ${file.contact} -${file.name} 超过了文件上传极限" }
169169 val limit = user.vip.superLimit.toLong()
170170
171171 if (file.size < limit) {
172172 try {
173- val bytes = download(urlString = url, 0 until file.size)
174- uploadSingleFile(path = rapid.path, bytes = bytes, size = file.size.toInt())
173+ val bytes = download(urlString = url, range = null )
174+ pcs.upload(path = rapid.path, ondup = OnDupType .NEW_COPY , size = bytes.size.toLong()) {
175+ writeFully(bytes)
176+ }
175177 return rapid
176178 } catch (throwable: ClientRequestException ) {
177179 logger.info { " 文件 ${file.name} 单文件上传失败, 进入文件上传, ${throwable.message} " }
@@ -181,34 +183,49 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
181183 }
182184
183185
184- val uploadId = with (
185- preCreate(
186- path = rapid.path,
187- size = file.size,
188- isDir = false ,
189- blocks = listOf (" 5910a591dd8fc18c32a8f3df4fdc1761" , " a5fc157d78e6ad1c7e114b056c92821e" ),
190- rename = RenameType .PATH
191- )
192- ) {
193- if (type == CreateReturnType .EXIST ) {
194- return rapid
195- } else {
196- check(uploadId.isNotEmpty()) { this }
197- uploadId
198- }
186+ val prepare = rest.prepare(upload = rapid, blocks = LAZY_BLOCKS , ondup = OnDupType .NEW_COPY )
187+ if (prepare.type == PrepareReturnType .EXIST ) {
188+ return rapid
199189 }
190+ val uploadId = requireNotNull(prepare.uploadId) { prepare }
200191
201- val blocks = blocks(urlString = url, limit = limit, path = rapid.path, uploadId = uploadId)
192+ val blocks = useHttpClient { client ->
193+ client.prepareGet(url) {
194+ url {
195+ if (NetdiskUploadConfig .https) {
196+ protocol = URLProtocol .HTTPS
197+ host = " gzc-download.ftn.qq.com"
198+ }
199+ }
200+ }.execute { response ->
201+ val channel = response.bodyAsChannel()
202+ val capacity = (file.size / limit + 1 ).toInt()
203+ List (capacity) { index ->
204+ val packet = channel.readRemaining(limit)
205+ supervisorScope {
206+ async {
207+ val size = packet.remaining.toInt()
208+ val temp = pcs.temp(path = rapid.path, id = uploadId, index = index, size = size) {
209+ writePacket(packet)
210+ }
211+ packet.close()
202212
203- createFile(
204- path = rapid.path,
205- size = file.size,
206- isDir = false ,
207- blocks = blocks,
213+ temp.md5
214+ }
215+ }
216+ }
217+ }.awaitAll()
218+ }
219+
220+ val merge = MergeFileInfo (
221+ blocks = blocks.toMutableList(),
208222 uploadId = uploadId,
209- rename = RenameType .PATH
223+ size = file.size,
224+ path = rapid.path
210225 )
211226
227+ rest.create(merge = merge, ondup = OnDupType .NEW_COPY )
228+
212229 return rapid
213230 }
214231
@@ -226,8 +243,8 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
226243 }
227244 }
228245
229- private suspend fun download (urlString : String , range : LongRange ): ByteArray {
230- val fragment = range.run { " bytes=${start} -${endInclusive} " }
246+ private suspend fun download (urlString : String , range : LongRange ? ): ByteArray {
247+ val fragment = range? .run { " bytes=${start} -${endInclusive} " }
231248 logger.verbose { " download $urlString #$fragment " }
232249 return useHttpClient { client ->
233250 client.prepareGet(urlString) {
@@ -241,35 +258,4 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
241258 }.body()
242259 }
243260 }
244-
245- private suspend fun blocks (urlString : String , limit : Long , path : String , uploadId : String ): List <String > {
246- return useHttpClient { client ->
247- client.prepareGet(urlString) {
248- url {
249- if (NetdiskUploadConfig .https) {
250- protocol = URLProtocol .HTTPS
251- host = " gzc-download.ftn.qq.com"
252- }
253- }
254- }.execute { response ->
255- val channel = response.bodyAsChannel()
256- val capacity = ((response.contentLength() ? : 0 ) / limit + 1 ).toInt()
257- List (capacity) { index ->
258- val packet = channel.readRemaining(limit)
259- val bytes = packet.readBytes()
260- supervisorScope {
261- async {
262- superFile(
263- path = path,
264- uploadId = uploadId,
265- index = index,
266- data = bytes,
267- size = bytes.size
268- ).md5
269- }
270- }
271- }
272- }.awaitAll()
273- }
274- }
275261}
0 commit comments