/// <summary> /// Deletes a file after successful upload from the DB, and from disk if this was instructed by the caller. /// </summary> /// <param name="fileToDelete">The pending file to be deleted</param> private void DeletePendingFile(ScheduledUpload fileToDelete) { if (fileToDelete.DeleteFile) { if (File.Exists(fileToDelete.FilePath)) { try { File.Delete(fileToDelete.FilePath); } catch (IOException e) { Logger.Error("Cannot delete uploaded file {0}: {1}", fileToDelete.FilePath, e.Message); } } } }
/// <summary> /// Schedules a file to be uploaded to thebackend. /// </summary> /// <param name="filePath">Absolute path to the file that is going to be uploaded</param> /// <param name="callback">Optional callback which is sent back to the caller with information about the uploading outcome</param> /// <param name="deleteFile">Indicates if files need to be deleted after successful upload. Default is true</param> /// <returns>True if the file is scheduled for upload</returns> public bool UploadFileFile(string filePath, UploadCallback callback = null, bool deleteFile = true) { lock (enqueueLock) { if (this.isDisposed || this.isShuttingDown) { Logger.Error("This instance is disposed or shutting down."); return(false); } if (!this.isInitialized) { Logger.Error("This instance is not initialized. Call Initialize() first"); return(false); } var fileFullPath = Path.GetFullPath(filePath); Logger.Debug("Scheduling upload of file {0}", fileFullPath); // set up file makes all the initial validations var fileToUpload = new ScheduledUpload(fileFullPath) { DeleteFile = deleteFile, Callback = callback }; // now enqueue the file for upload if (this.pendingUploadCollection.TryAdd(fileToUpload)) { Logger.Info("The dialogue file {0} is scheduled for upload", fileFullPath); return(true); } else { // blocking collection is full Logger.Error("Cannot upload dialogue file {0} since upload queue is full", fileFullPath); return(false); } } }
/// <summary> /// Uploads a file to the backend. /// </summary> /// <param name="fileToUpload">A file to be uploaded to the backend.</param> /// <param name="putUrl">url where file is to be uploaded</param> /// <returns>A <see cref="UploadResult"/> indicating the outcome of the upload process</returns> internal UploadResult UploadFile(ScheduledUpload fileToUpload, string putUrl) { if (string.IsNullOrWhiteSpace(putUrl)) { Logger.Error("No resource Id for file {0}", fileToUpload.FilePath); return(new UploadResult(false, string.Format("No resource Id for file {0}", fileToUpload.FilePath))); } const string fileContentType = "application/x-www-form-urlencoded"; byte[] fileInBytes = File.ReadAllBytes(fileToUpload.FilePath); var restRequestFile = new RestRequest(putUrl, Method.PUT) { RequestFormat = DataFormat.Xml }; restRequestFile.AddHeader(AuthTokenKey, this.authToken); restRequestFile.AddHeader(ContentTypeKey, fileContentType); restRequestFile.AddParameter(fileContentType, fileInBytes, fileContentType, ParameterType.RequestBody); var restClient = new RestClient(new Uri(this.backendUrl)); var response = restClient.Put(restRequestFile); switch (response.StatusCode) { case System.Net.HttpStatusCode.Unauthorized: return(new UploadResult(false, string.Format("Unauthorized to upload {0}", fileToUpload.FilePath))); case System.Net.HttpStatusCode.OK: // file has been uploaded, thus we can remove it from pending files list Logger.Info("{0} was uploaded correctly: {1}", fileToUpload.FilePath, response.StatusDescription); return(new UploadResult(true, string.Empty)); default: Logger.Error("Could not upload {0}: {1} - {2}", fileToUpload.FilePath, Convert.ToInt32(response.StatusCode), response.Content); return(new UploadResult(false, string.Format("Could not upload {0}: {1} - {2}", fileToUpload.FilePath, Convert.ToInt32(response.StatusCode), response.StatusDescription))); } }