/// <summary> /// Save File To Firebase Storage and return url to file /// </summary> /// <param name="localPath">url to local file</param> /// <returns>url to external file</returns> private async Task <string> SaveFileToStorage(string localPath) { try { var storage = FirebaseStorage.Instance; var storageRef = storage.GetReferenceFromUrl("gs://alicemessenger-5efbb.appspot.com"); var bytes = System.IO.File.ReadAllBytes(localPath); var metadata = new StorageMetadata.Builder() .SetContentType("image/jpeg") .Build(); var child = storageRef.Child("images/" + Path.GetFileName(localPath)); await child.PutBytes(bytes, metadata); localPath = Path.GetFileName(localPath); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("---> Error SaveFileToStorage " + ex.Message.ToString()); throw ex; } return(localPath); }
public Task <Attachment> UploadFile(FileData fileData) { TaskCompletionSource <Attachment> ResultCompletionSource = new TaskCompletionSource <Attachment>(); string fileExtension = GetExtension(fileData.FileName); string fileNameStr = fileData.FileName; string path = "userFile/" + Guid.NewGuid().ToString() + fileExtension; try { StorageReference storageReference = storage.GetReference(path); StorageMetadata storageMetadata = new StorageMetadata.Builder() .SetCustomMetadata("FileName", fileNameStr) .Build(); UploadTask uploadTask = storageReference.PutStream(fileData.GetStream(), storageMetadata); uploadTask.AddOnCompleteListener(new OnCompleteEventHandleListener((Android.Gms.Tasks.Task uploadFile) => { if (uploadFile.IsSuccessful) { var TaskResult = uploadFile.Result; var uri = ((UploadTask.TaskSnapshot)TaskResult).DownloadUrl.ToString(); Attachment attachment = new Attachment(fileNameStr, uri); ResultCompletionSource.SetResult(attachment); } })); } catch (Exception e) { ResultCompletionSource.TrySetException(e); } return(ResultCompletionSource.Task); }
public static StorageMetadata ToStorageMetadata(this MetadataChange self) { if (self == null) { return(null); } var builder = new StorageMetadata.Builder(); if (self.IsCacheControlChanged) { builder.SetCacheControl(self.CacheControl); } if (self.IsContentDispositionChanged) { builder.SetContentDisposition(self.ContentDisposition); } if (self.IsContentEncodingChanged) { builder.SetContentEncoding(self.ContentEncoding); } if (self.IsContentLanguageChanged) { builder.SetContentLanguage(self.ContentLanguage); } if (self.IsContentTypeChanged) { builder.SetContentType(self.ContentType); } if (self.CustomMetadata != null) { foreach (var(key, value) in self.CustomMetadata) { builder.SetCustomMetadata(key, value); } } return(builder.Build()); }
/// <summary> /// Save File To Firebase Storage and return url to file /// </summary> /// <param name="localPath">url to local file</param> /// <returns>url to external file</returns> private async Task <string> SaveFileToStorage(string localPath) { try { var bytes = File.ReadAllBytes(localPath); var metadata = new StorageMetadata.Builder() .SetContentType("image/jpeg") .Build(); var child = storageRef.Child("userimages/" + Path.GetFileName(localPath)); await child.PutBytes(bytes, metadata); localPath = Path.GetFileName(localPath); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("---> Error SaveFileToStorage " + ex.Message); } return(localPath); }