'Upload a file to Google Drive
'Name - The Name of the filename (Name at Google Drive)
'UploadDir - The (local) folder of the upload file.
'UploadFile - Filename (local) of the upload file
'FileID - The file ID on Google Drive. If not known, a new file will created.
'
'Events : FileUploadDone(FileID as string)
'Update einbauen/Testen
'Parrent testen
Sub UploadFile(Name As String, LocalDir As String, LocalFilename As String, Parrent As String, FileId As String)
Starter.cl.lg("-------------------")
Starter.cl.lg("GoogleDrive, UploadFile")
Dim data() As Byte
If Name = "" Then Name = LocalFilename
Starter.cl.lg("LocalDir:" & LocalDir)
Starter.cl.lg("LocalFilename:" & LocalFilename)
Starter.cl.lg("Parent:" & Parrent)
Starter.cl.lg("FileID:" & FileId)
Try
Dim h_ul As HttpJob
h_ul.Initialize("",Me)
If FileId = "" Then
Starter.cl.lg("Upload w/o File ID (create new File)")
'Create the stream
Dim boundary As String = "foo_bar_baz"
Dim ContentType As String = "application/octet-stream"
Dim EOL As String= Chr(13) & Chr(10)
Dim In As InputStream = File.OpenInput(LocalDir, LocalFilename)
Dim out2 As OutputStream
out2.InitializeToBytesArray(1000)
Dim m As Map
m.Initialize
m.Put("name", Name)
If Parrent <>"" Then m.Put("parents",Array As String(Parrent))
Dim Jg As JSONGenerator
Jg.Initialize(m)
'The first half of the POST contains the JSON data AND the Content Type from above.
Dim RR As String = "--" & boundary & EOL & _
"Content-Type: application/json; charset=UTF-8" & EOL & EOL & _
Jg.ToString & EOL & EOL & _
"--" & boundary & EOL & _
"Content-Type: " & ContentType & EOL & EOL
data = (RR).GetBytes("UTF-8")
out2.WriteBytes(data, 0, data.Length) 'Write it To the Stream.
File.Copy2(In, out2) 'Add the File itself To the Stream.
data = (EOL & EOL & "--" & boundary & "--").GetBytes("UTF-8") 'Add the final boundary to the POST
out2.WriteBytes(data, 0, data.Length) 'Write it To the Stream Then make the Bytes the complete Stream.
data = out2.ToBytesArray
h_ul.PostBytes("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", data)
h_ul.GetRequest.SetContentType("multipart/related; boundary=" & boundary)
h_ul.GetRequest.SetHeader("Authorization", "Bearer " & myAccessToken)
Else
Starter.cl.lg("Upload mit File ID (File Content Update)")
'Create a new stream
Dim OutUpdate As OutputStream
Dim InUpate As InputStream = File.OpenInput(LocalDir, LocalFilename)
OutUpdate.InitializeToBytesArray(1000) 'Initialize the stream
File.Copy2(InUpate, OutUpdate) 'Copy the filestream in the output stream
data = OutUpdate.ToBytesArray 'Fill export data array
h_ul.PatchBytes("https://www.googleapis.com/upload/drive/v3/files" & "/" & FileId & "?uploadType=media", data)
h_ul.getrequest.SetHeader("Authorization", "Bearer " & myAccessToken)
End If
wait For (h_ul) JobDone(h_ul As HttpJob)
If h_ul.Success Then
Starter.cl.lg("Upload File Success")
'starter.cl.lg(h_ul.GetString)
Dim j As JSONParser
Dim Map1 As Map
J.Initialize(h_ul.GetString)
Map1 = J.NextObject
FileId=Map1.Get("id")
else
End If
Catch
Starter.cl.lg("GoogleUpload File Error")
Starter.cl.lg(LastException.Message)
Dim writer As TextWriter
writer.Initialize(File.OpenOutput(Starter.DirRootExternal,"Log_Backup_Err.txt",True))
writer.WriteLine(LocalFilename & ", " & LastException.Message)
writer.Close
End Try
CallSubDelayed2(evModule, evName & "_FileUploaded", FileId)
End Sub