Skip to content

Commit

Permalink
fix: [SMB] 0 byte uploaded file
Browse files Browse the repository at this point in the history
* On certain devices, smbj uploads file without any exception but the remote file is 0 byte.

Change-Id: Iaf593d7dd0ffb61b0216a72833d4a4dfb49dbccb
  • Loading branch information
XayahSuSuSu committed Jul 24, 2024
1 parent f0d4fea commit a49031d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class FTPClientImpl(private val entity: CloudEntity, private val extra: FTPExtra
client.storeFile(dstPath, countingStream)
srcInputStream.close()
countingStream.close()
if (countingStream.byteCount == 0L) throw IOException("Failed to write remote file: 0 byte.")
onUploading(countingStream.byteCount, countingStream.byteCount)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import net.schmizz.sshj.userauth.password.PasswordFinder
import net.schmizz.sshj.userauth.password.Resource
import java.io.File
import java.io.FileOutputStream
import java.io.IOException


class SFTPClientImpl(private val entity: CloudEntity, private val extra: SFTPExtra) : CloudClient {
Expand Down Expand Up @@ -121,6 +122,7 @@ class SFTPClientImpl(private val entity: CloudEntity, private val extra: SFTPExt
srcInputStream.close()
countingStream.close()
dstFile.close()
if (countingStream.byteCount == 0L) throw IOException("Failed to write remote file: 0 byte.")
onUploading(countingStream.byteCount, countingStream.byteCount)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.hierynomus.smbj.SMBClient
import com.hierynomus.smbj.SmbConfig
import com.hierynomus.smbj.auth.AuthenticationContext
import com.hierynomus.smbj.common.SMBRuntimeException
import com.hierynomus.smbj.io.InputStreamByteChunkProvider
import com.hierynomus.smbj.session.Session
import com.hierynomus.smbj.share.Directory
import com.hierynomus.smbj.share.DiskShare
Expand All @@ -24,7 +23,6 @@ import com.xayah.core.model.SmbVersion
import com.xayah.core.model.database.CloudEntity
import com.xayah.core.model.database.SMBExtra
import com.xayah.core.network.R
import com.xayah.core.network.io.CountingInputStreamImpl
import com.xayah.core.network.io.CountingOutputStreamImpl
import com.xayah.core.network.util.getExtraEntity
import com.xayah.core.rootservice.parcelables.PathParcelable
Expand All @@ -40,7 +38,6 @@ import com.xayah.libpickyou.parcelables.FileParcelable
import com.xayah.libpickyou.ui.PickYouLauncher
import com.xayah.libpickyou.ui.model.PickerType
import java.io.File
import java.io.FileInputStream
import java.io.IOException


Expand Down Expand Up @@ -203,14 +200,16 @@ class SMBClientImpl(private val entity: CloudEntity, private val extra: SMBExtra
val dstPath = "$dst/$name"
log { "upload: $src to $dstPath" }
val dstFile = openFile(dstPath)
val dstStream = dstFile.outputStream
val srcFile = File(src)
val srcFileSize = srcFile.length()
val srcInputStream = FileInputStream(srcFile)
val countingStream = CountingInputStreamImpl(srcInputStream, srcFileSize) { read, total -> onUploading(read, total) }
dstFile.write(InputStreamByteChunkProvider(countingStream))
val srcInputStream = srcFile.inputStream()
val countingStream = CountingOutputStreamImpl(dstStream, srcFileSize, onUploading)
srcInputStream.copyTo(countingStream)
srcInputStream.close()
countingStream.close()
dstFile.close()
if (countingStream.byteCount == 0L) throw IOException("Failed to write remote file: 0 byte.")
onUploading(countingStream.byteCount, countingStream.byteCount)
}

Expand Down

0 comments on commit a49031d

Please sign in to comment.