private void Addobject(string filePath, string bucketKey) { try { var attribute = File.GetAttributes(filePath); string relativePath = filePath.Replace(Utilities.Path + "\\", "").Replace("\\", "/"); if (attribute != FileAttributes.Directory && attribute != (FileAttributes.Archive | FileAttributes.Hidden)) { if (File.Exists(filePath) && !Utilities.IsFileUsedbyAnotherProcess(filePath)) { bool IsItSharedFile = false; string Username = string.Empty; if (relativePath.ToLower().IndexOf("shared") == 0) { IsItSharedFile = true; var folders = relativePath.Split('/'); if (folders.Length > 1) { Username = folders[1]; relativePath = ""; for (int i = 2; i < folders.Length; i++) relativePath += folders[i] + "/"; } else { relativePath = ""; for (int i = 1; i < folders.Length; i++) relativePath += folders[i] + "/"; } relativePath = relativePath.Substring(0, relativePath.Length - 1); } var appUpdateInfo = new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(filePath).LastWriteTime, Status = UpdateStatus.Update }; _processingFiles.Add(filePath); try { var uploadResponses = new List<UploadPartResponse>(); byte[] bytes; long contentLength; using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { contentLength = fileStream.Length; bytes = new byte[contentLength]; fileStream.Read(bytes, 0, Convert.ToInt32(contentLength)); } if (contentLength > 0) { var lastUploadedPartdetail = GetLastUploadedPartResponse(relativePath, bucketKey, uploadResponses); int alreadyUploadedParts = lastUploadedPartdetail.LastPartNumber; string uploadId; if (string.IsNullOrEmpty(lastUploadedPartdetail.UploadId)) { InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest().WithBucketName(bucketKey).WithKey( relativePath); InitiateMultipartUploadResponse initResponse = _amazons3.InitiateMultipartUpload(initiateRequest); uploadId = initResponse.UploadId; } else uploadId = lastUploadedPartdetail.UploadId; try { long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB long filePosition = partSize * alreadyUploadedParts; for (int i = alreadyUploadedParts + 1; filePosition < contentLength; i++) { // Before upload the next set of upload part, need to check the last modified because user might modify it in the mean time if (File.Exists(filePath) && appUpdateInfo.LastModifiedTime == new FileInfo(filePath).LastWriteTime) { byte[] bytesToStream; if (filePosition + partSize < contentLength) { bytesToStream = new byte[partSize]; Array.Copy(bytes, filePosition, bytesToStream, 0, partSize); } else { bytesToStream = new byte[contentLength - filePosition]; Array.Copy(bytes, filePosition, bytesToStream, 0, contentLength - filePosition); } Stream stream = new MemoryStream(bytesToStream); UploadPartRequest uploadRequest = new UploadPartRequest() .WithBucketName(bucketKey) .WithKey(relativePath) .WithUploadId(uploadId) .WithPartNumber(i) .WithPartSize(partSize) .WithFilePosition(filePosition) .WithTimeout(1000000000); uploadRequest.WithInputStream(stream); // Upload part and add response to our list. var response = _amazons3.UploadPart(uploadRequest); WriteResponseToFile(relativePath, bucketKey, uploadId, appUpdateInfo.LastModifiedTime, response); uploadResponses.Add(response); filePosition += partSize; ModifySyncStatus("Uploaded", contentLength <= filePosition ? contentLength : filePosition, contentLength, relativePath); } else { // need to abort the upload process _processingFiles.Remove(filePath); RemoveConfig(relativePath, lastUploadedPartdetail.BucketKey); _amazons3.AbortMultipartUpload(new AbortMultipartUploadRequest() .WithBucketName(bucketKey) .WithKey(relativePath) .WithUploadId(uploadId)); return; } } CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest() .WithBucketName(bucketKey) .WithKey(relativePath) .WithUploadId(uploadId) .WithPartETags(uploadResponses); CompleteMultipartUploadResponse completeUploadResponse = _amazons3.CompleteMultipartUpload(completeRequest); RemoveConfig(relativePath, completeUploadResponse.BucketName); //_service.AddObject(filePath, Utilities.MyConfig.BucketKey, relativePath); SetAcltoObject(relativePath); if (IsItSharedFile) { ProcessApplicationUpdates(appUpdateInfo, true, Username); } else ProcessApplicationUpdates(appUpdateInfo, false, string.Empty); _processingFiles.Remove(filePath); UploadContentForSearch(filePath, relativePath); } catch (Exception) { _processingFiles.Remove(filePath); _amazons3.AbortMultipartUpload(new AbortMultipartUploadRequest() .WithBucketName(bucketKey) .WithKey(relativePath) .WithUploadId(uploadId)); RemoveConfig(relativePath, Utilities.MyConfig.BucketKey); if (!_fileQueue.ContainsKey(filePath)) _fileQueue.Add(filePath, new FileQueue { Type = WatcherChangeTypes.Created, Name = Path.GetFileName(filePath) }); else _fileQueue.Add(filePath, new FileQueue { Type = WatcherChangeTypes.Created, Name = Path.GetFileName(filePath) }); } } } catch (Exception) { return; } } } } catch (Exception) { return; } }
private void ProcessApplicationUpdates(AppUpdateInfo appUpdateInfo, bool IsShared, string Username) { try { //if (_applicationUpates.Count > 0) //{ //var appUpdateInfo = (AppUpdateInfo)_applicationUpates.Dequeue(); string url = Utilities.DevelopmentMode ? "http://localhost:3000" : "http://versavault.com"; if (!IsShared) { switch (appUpdateInfo.Status) { case UpdateStatus.Add: { url += "/api/add_files?bucket_key=" + Utilities.MyConfig.BucketKey + "&key=" + appUpdateInfo.Key + "&last_modified=" + appUpdateInfo.LastModifiedTime.ToUniversalTime().ToString( "yyyy-MM-dd HH:mm:ss tt") + "&machine_key=" + Utilities.MyConfig.MachineKey; break; } case UpdateStatus.Update: { url += "/api/update_files?bucket_key=" + Utilities.MyConfig.BucketKey + "&key=" + appUpdateInfo.Key + "&last_modified=" + appUpdateInfo.LastModifiedTime.ToUniversalTime().ToString( "yyyy-MM-dd HH:mm:ss tt") + "&machine_key=" + Utilities.MyConfig.MachineKey; break; } case UpdateStatus.Delete: { url += "/api/delete_files?bucket_key=" + Utilities.MyConfig.BucketKey + "&key=" + appUpdateInfo.Key + "&machine_key=" + Utilities.MyConfig.MachineKey; break; } } } else { switch (appUpdateInfo.Status) { case UpdateStatus.Add: { url += "/api/add_files?shared=" + IsShared + "&key=" + appUpdateInfo.Key + "&last_modified=" + appUpdateInfo.LastModifiedTime.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss tt") + "&username="******"/api/update_files?shared=" + IsShared + "&key=" + appUpdateInfo.Key + "&last_modified=" + appUpdateInfo.LastModifiedTime.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss tt") + "&username="******"/api/delete_files?shared=" + IsShared + "&key=" + appUpdateInfo.Key + "&last_modified=" + appUpdateInfo.LastModifiedTime.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss tt") + "&username=" + Username; break; } } } GetResponse(url); //} } catch (Exception) { // Todo // Need to fix some how if any error happens here return; } }