Beispiel #1
0
        private SyncJob CreateJob(byte[] toArray)
        {
            try
            {
                Dictionary<string, byte[]> index = toArray.Deserialize<Dictionary<string, byte[]>>();

                SyncJob result = new SyncJob();
                result.filesToDelete = new List<string>();
                result.filesToDownload = new List<string>();

                foreach (var file in index.Keys)
                {
                    if (!fileIndex.ContainsKey(file)) result.filesToDelete.Add(file);
                }

                foreach (var file in fileIndex)
                {
                    if (!index.ContainsKey(file.Key) || !index[file.Key].SequenceEqual(file.Value))
                    {
                        var downloadUrl =
                            MmoAws.AmazonS3.GetPreSignedURL(new GetPreSignedUrlRequest()
                                                                {
                                                                    BucketName = Settings.Default.BucketName,
                                                                    Key = Settings.Default.FolderName + "/" + file.Key,
                                                                    Expires = DateTime.Now.AddHours(3)
                                                                });
                        result.filesToDownload.Add(file.Key + "|" + downloadUrl);
                    }
                }

                return result;
            }catch(Exception ex)
            {
                log.Error("Error creating sync job, "+ex.Message);
                return new SyncJob();
            }
        }
        /// <summary>
        /// Filename and Md5 index of the node libraries.
        /// </summary>
        /// <param name="inputIndex"></param>
        public SyncJob CreateSyncJob(Dictionary<string, byte[]> inputIndex)
        {
            //Search for files that do not exist
            var job = new SyncJob {filesToDelete = new List<string>(), filesToDownload = new List<string>()};

            foreach(var file in inputIndex.Keys)
            {
                if (!FileIndex.ContainsKey(file))
                {
                    job.filesToDelete.Add(file);
                }
            }

            foreach(var file in FileIndex.Keys)
            {
                if(!inputIndex.ContainsKey(file) || !inputIndex[file].SequenceEqual(FileIndex[file]))
                {
                    job.filesToDownload.Add(file);
                }
            }
            var operationCount = job.filesToDelete.Count + job.filesToDownload.Count;
            if(operationCount > 0) log.Debug("Generated SyncJob with "+(operationCount)+" operations.");
            return operationCount == 0 ? null : job;
        }
 /// <summary>
 /// Perform some sync job on the filesystem. Downloads files using the NodeLibDownload.
 /// </summary>
 /// <param name="syncJob"></param>
 public void PerformSyncJob(SyncJob syncJob)
 {
     if(syncJob.filesToDelete== null) syncJob.filesToDelete = new List<string>(0);
     if(syncJob.filesToDownload ==null) syncJob.filesToDownload = new List<string>(0);
     log.Info("Performing filesystem sync, total of "+(syncJob.filesToDelete.Count+syncJob.filesToDownload.Count)+" operations.");
     log.Debug("Performing "+syncJob.filesToDelete.Count+" deletions.");
     foreach(var fileToDelete in syncJob.filesToDelete)
     {
         if(File.Exists(libraryPath+"/"+fileToDelete)) File.Delete(libraryPath+"/"+fileToDelete);
     }
     log.Debug("Performing "+syncJob.filesToDownload.Count+" downloads.");
     foreach(var fileToDownload in syncJob.filesToDownload)
     {
         DownloadFile(fileToDownload, libraryPath);
     }
     log.Debug("Finished performing sync job.");
 }