private bool IsImageAttachment(KidsNoteContent.Attachment attach) { return(attach.Type == AttachmentType.IMAGE || attach.Type == AttachmentType.IMAGE_MENU_AFTERNOON_SNACK || attach.Type == AttachmentType.IMAGE_MENU_DINNER || attach.Type == AttachmentType.IMAGE_MENU_LUNCH || attach.Type == AttachmentType.IMAGE_MENU_MORNING_SNACK); }
private string UploadPhotoAttachment(KidsNoteContent.Attachment attach, int index, string parentId, string ext, bool encrypt) { attach.Data.Seek(0, SeekOrigin.Begin); MemoryStream encryptedStream = null; if (encrypt) { EncryptorChaCha chacha = new EncryptorChaCha(true, EncryptorChaCha.DefaultChaChaEncKey, EncryptorChaCha.DefaultChaChaEncNonce); encryptedStream = new MemoryStream(); byte[] readBuffer = new byte[4096]; byte[] encBuffer = new byte[readBuffer.Length]; int nRead = attach.Data.Read(readBuffer, 0, readBuffer.Length); while (nRead > 0) { chacha.Process(readBuffer, 0, nRead, encBuffer, 0); encryptedStream.Write(encBuffer, 0, nRead); nRead = attach.Data.Read(readBuffer, 0, readBuffer.Length); } encryptedStream.Seek(0, SeekOrigin.Begin); } string mType = MimeType.get(ext); string attachName = string.Format("{0}_{1}", index.ToString("000"), attach.Name); string message = String.Format("Uploading attachment... {0}", attachName); UploadProgress(message); Stream toUpload = encrypt ? encryptedStream : attach.Data; string suffix = encrypt ? ".chacha" : ""; string id = ""; try { id = UploadFile(toUpload, parentId, attachName + suffix, attach.Name + suffix, mType); } catch (Exception) { id = ""; } return(id); }
private string UploadVideoAttachment(KidsNoteContent.Attachment attach, int index, string parentId, string ext, bool encrypt) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(attach.DownloadUrl); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string mType = MimeType.get(ext); string id = ""; Stream videoDownloadStream = null; FileStream fileStream = null; string savedFileName = ""; try { EncryptorChaCha chacha = null; if (encrypt) { chacha = new EncryptorChaCha(true, EncryptorChaCha.DefaultChaChaEncKey, EncryptorChaCha.DefaultChaChaEncNonce); } videoDownloadStream = response.GetResponseStream(); savedFileName = String.Format("video.{0}", ext); fileStream = File.Create(savedFileName); byte[] buffer = new byte[1024 * 16]; byte[] bufferEnc = new byte[buffer.Length]; int bytesRead = 0; int totalBytesRead = 0; do { bytesRead = videoDownloadStream.Read(buffer, 0, buffer.Length); if (bytesRead > 0) { if (chacha != null) { chacha.Process(buffer, 0, bytesRead, bufferEnc, 0); fileStream.Write(bufferEnc, 0, bytesRead); } else { fileStream.Write(buffer, 0, bytesRead); } totalBytesRead += bytesRead; } }while (bytesRead > 0); videoDownloadStream.Close(); fileStream.Seek(0, SeekOrigin.Begin); string attachName = string.Format("{0}_{1}", index.ToString("000"), attach.Name); string suffix = encrypt ? ".chacha" : ""; string message = String.Format("Uploading attachment... {0}", attachName); UploadProgress(message); id = UploadFile(fileStream, parentId, attachName + suffix, attach.Name + suffix, mType); fileStream.Close(); } catch (Exception e) { System.Diagnostics.Trace.WriteLine(e); id = ""; } if (videoDownloadStream != null) { videoDownloadStream.Close(); } if (response != null) { response.Close(); } if (fileStream != null) { fileStream.Close(); } if (System.IO.File.Exists(savedFileName)) { System.IO.File.Delete(savedFileName); } return(id); }