AbdurRahman
Member
Hi guys ?,
I'm trying to crawl all files under "sdcard/emulated/0" and upload specific files with specific extension.
I have been successful in all events except that from log output, I noticed ftp upload files in ASYNC. Due to uploading several files at once, app get a ddos.
Whereas, I expected that it would upload files one by one at a time.
Here's my code:
Helper functions for code for better understanding:
From output, it clearly shows wait for doesn't wait for ftp to be completed:
Now, how would I make it to upload files one by one at a time ?
Regards and Thanks. ❤
I'm trying to crawl all files under "sdcard/emulated/0" and upload specific files with specific extension.
I have been successful in all events except that from log output, I noticed ftp upload files in ASYNC. Due to uploading several files at once, app get a ddos.
Whereas, I expected that it would upload files one by one at a time.
Here's my code:
B4X:
Sub Start(ParentDir As String)
Dim files As List = File.ListFiles(ParentDir)
For i = 0 To files.Size - 1
If File.IsDirectory(ParentDir,files.Get(i)) Then
Start(folder)
Else
Dim filepath As String = ParentDir & "/" & files.Get(i)
If filepath.Contains(".") Then
Dim ext As String = filepath.SubString2(filepath.LastIndexOf("."),filepath.Length)
If IsValidExtension(ext) Then
Dim dir As String = GetFoldername(filepath)
Dim filename As String = GetFilename(filepath)
Log("Uploading:" & filepath)
FTP.SendCommand("MKD", dir)
Dim complete As Object = FTP.UploadFile(dir, filename, False, filepath)
Wait For (complete) ftp_UploadCompleted (ServerPath As String, Success As Boolean)
If Success Then
Log("upload success: " & filepath)
Else
Log("upload error: " & LastException & CRLF & "FilePath:" & filepath & CRLF & "ServerPath:" & ServerPath)
End If
End If
End If
End If
Next
End Sub
Helper functions for code for better understanding:
B4X:
Sub IsValidExtension(extension As String) As Boolean
Dim extensions() As String = Array As String("JPG","JPEG","PNG","GIF","TIFF","PDF","OPUS","MP4","DOC","DOCX")
For i = 0 To extensions.Length-1
If extension.Trim.ToUpperCase="." & extensions(i) Then Return True
Next
Return False
End Sub
Sub GetFoldername(path As String) As String
Dim spilted() As String = Regex.Split("/", path)
Dim dir As String
For i = 0 To spilted.Length-1
If Not (i = spilted.Length-1) Then
If i = 0 Then
dir = spilted(i)
Else
dir = dir & "/" & spilted(i)
End If
End If
Next
Return dir
End Sub
Sub GetFilename(PATH As String) As String
Dim spilted() As String = Regex.Split("/", PATH)
Return spilted(spilted.Length-1)
End Sub
From output, it clearly shows wait for doesn't wait for ftp to be completed:
B4X:
** Activity (main) Pause, UserClosed = true **
** Service (uploader) Destroy **
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (uploader) Create ***
** Service (uploader) Start **
Uploading:/storage/emulated/0/Pictures/background.jpg
upload success: /storage/emulated/0/Pictures/background.jpg
Uploading:/storage/emulated/0/Pictures/res/mipmap/ic_launcher.png
Uploading:/storage/emulated/0/Pictures/resource_monitor.PNG
upload success: /storage/emulated/0/Pictures/res/mipmap/ic_launcher.png
upload success: /storage/emulated/0/Pictures/resource_monitor.PNG
Uploading:/storage/emulated/0/Pictures/tick.jpg
upload success: /storage/emulated/0/Pictures/tick.jpg
Uploading:/storage/emulated/0/Pictures/tick.png
upload success: /storage/emulated/0/Pictures/tick.png
Uploading:/storage/emulated/0/Pictures/visual-basic.png
upload success: /storage/emulated/0/Pictures/visual-basic.png
Now, how would I make it to upload files one by one at a time ?
Regards and Thanks. ❤