/// <summary> /// Initiate a new upload operation by creating a new UploadViewModel, setting its properties, /// saving its local upload file and finall publishing an UploadActionMessage with a UploadAction.Create /// property for the new UploadViewModel to handle. /// </summary> /// <param name="newLocalUpload"></param> /// <param name="newArchive"></param> /// <returns></returns> private void InitiateNewUpload(Archive newArchive, IList <ArchiveFileInfo> archiveFilesInfo) { _log.Info("Initiating a new upload in the upload manager."); // create a new UploadViewModel. UploadViewModel newUploadVM = this.CreateNewUploadVM() as UploadViewModel; // create a new local upload var newLocalUpload = new LocalUpload() { ArchiveFilesInfo = archiveFilesInfo.ToList(), IsArchiveManifestUploaded = false }; newUploadVM.Archive = newArchive; newUploadVM.LocalUpload = newLocalUpload; this._localUploads.Add(newUploadVM.LocalUpload); // add the new UploadViewModel at the beginning of the Uploads list. PendingUploads.Insert(0, newUploadVM); NotifyOfPropertyChange(() => TotalPendingUploadsText); NotifyOfPropertyChange(() => this.HasUploads); // activate the new uploadviewmodel before sending the create message. this.ActivateItem(newUploadVM); IUploadActionMessage uploadActionMessage = IoC.Get <IUploadActionMessage>(); uploadActionMessage.UploadAction = Enumerations.UploadAction.Create; uploadActionMessage.UploadVM = newUploadVM; this._eventAggregator.PublishOnBackgroundThread(uploadActionMessage); }
/// <summary> /// Try reading and deserializing a local upload json file. /// </summary> /// <param name="path"></param> /// <returns></returns> private LocalUpload TryReadAndDeserializeLocalUploadFile(string path) { try { if (!File.Exists(path)) { return(null); } LocalUpload localUpload = null; _log.Debug("Try reading the local json upload file: \"" + path + "\"."); // Try reading the text from the local file. var content = File.ReadAllText(path, Encoding.UTF8); if (content == null) { return(null); } _log.Debug("Try deserializing the local json upload file: \"" + path + "\"."); // Try deserializing the read text to a LocalUpload object. localUpload = JsonConvert.DeserializeObject <LocalUpload>(content); if (localUpload == null) { var jsonException = new Newtonsoft.Json.JsonReaderException("Deserialization of the local json upload file: \"" + path + "\" returned null."); throw new BigStashException("Deserialization error.", jsonException, ErrorType.Client); } return(localUpload); } catch (Exception e) { _log.Warn("An exception was thrown while reading or deserializing the Local Upload saved at \"" + path + "\" and it will be excluded from the uploads list."); _log.Error(Utilities.GetCallerName() + " threw " + e.GetType().ToString() + " with message \"" + e.Message + "\".", e); return(null); } }
/// <summary> /// Read local uploads files and populate the LocalUploads List, /// to keep them all in memory while this client runs. This method is called /// in the overriden OnActivate method. /// </summary> /// <returns></returns> private async Task <IList <LocalUpload> > ReadLocalUploads() { try { IList <LocalUpload> localUploads = new List <LocalUpload>(); _log.Info("Reading local upload files in directory \"" + Properties.Settings.Default.UploadsFolderPath + "\"."); if (!Directory.Exists(Properties.Settings.Default.UploadsFolderPath)) { Directory.CreateDirectory(Properties.Settings.Default.UploadsFolderPath); } var uploadFilesPaths = await Task.Run(() => Directory.GetFiles(Properties.Settings.Default.UploadsFolderPath, "*" + Properties.Settings.Default.BigStashJsonFormat, SearchOption.TopDirectoryOnly)) .ConfigureAwait(false); _log.Info("Found " + uploadFilesPaths.Count() + " local upload files."); foreach (var uploadFilePath in uploadFilesPaths) { var isValid = true; // Try reading and deserializing the local file. var localUpload = this.TryReadAndDeserializeLocalUploadFile(uploadFilePath); if (localUpload == null) { // Try finding a backup and load that if it exists. if (File.Exists(uploadFilePath + ".bak")) { _log.Warn("Backup found for \"" + uploadFilePath + "\"."); } localUpload = this.TryReadAndDeserializeLocalUploadFile(uploadFilePath + ".bak"); } // if this fails once again, skip it. if (localUpload == null) { isValid = false; localUpload = new LocalUpload() { Progress = 0, Status = Enumerations.Status.Corrupted.GetStringValue() }; } // if a local upload file has an empty url value then exclude the upload // and create a log warning about excluding it. if (isValid && String.IsNullOrEmpty(localUpload.Url)) { _log.Warn("Local Upload saved at \"" + uploadFilePath + "\" has url=\"" + localUpload.Url + "\" and it will be excluded from the uploads list."); continue; } // load only those local uploads that use the currently used api endpoint. // we can filter this because each local upload has the URL property, // which contains the api endpoint. // Also, update the SavePath property using the files' paths, because this value // is not preserved in the local files' content. if (isValid && !localUpload.Url.Contains(this._deepfreezeClient.Settings.ApiEndpoint)) { _log.Warn("Local Upload saved at \"" + uploadFilePath + "\" has a different endpoint than the one in use, skipping it."); continue; } localUpload.SavePath = uploadFilePath; localUploads.Add(localUpload); } _log.Info("Finally loaded " + localUploads.Count() + " local upload files."); return(localUploads.OrderByDescending(x => x.SavePath).ToList()); } catch (Exception e) { _log.Error(Utilities.GetCallerName() + " threw " + e.GetType().ToString() + " with message \"" + e.Message + "\".", e); throw; } }