Example #1
0
        public ActionResult Edit(S3Object s3object, string prefix = "", int maxKeys = 100)
        {
            ViewBag.prefix = prefix;
              ViewBag.maxKeys = maxKeys;

              if (ModelState.IsValid && Request.Files.Count > 0)
              {
            var put = new Amazon.S3.Model.PutObjectRequest();
            put.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]);
            put.WithKey(s3object.Key);

            var file = Request.Files[0];
            put.WithContentType(file.ContentType);
            put.WithInputStream(file.InputStream);
            put.WithCannedACL(Amazon.S3.Model.S3CannedACL.PublicRead);
            s3.PutObject(put);

            return RedirectToAction("Index", new { prefix = prefix, maxKeys = maxKeys });
              }

              return View(s3object);
        }
 private void DownloadFolder(S3Object s3Obj, string folderPath)
 {
     string fullPath = s3Obj.Shared ? folderPath : Path.Combine(folderPath, s3Obj.Key.Replace("/", "\\"));
     try
     {
         // need to import complete directory
         _downloadObjects.Add(fullPath);
         if (!Directory.Exists(fullPath))
             Directory.CreateDirectory(fullPath);
         // get the folder structure from application database
         string url = Utilities.DevelopmentMode ? "http://localhost:3000" : "http://versavault.com";
         url += "/api/child_files?bucket_key=" + Utilities.MyConfig.BucketKey + "&parent_uid=" + s3Obj.Uid + "&machine_key=" + Utilities.MyConfig.MachineKey + "&shared=" + s3Obj.Shared;
         string result = GetResponse(url);
         if (!string.IsNullOrEmpty(result))
         {
             var res = JsonConvert.DeserializeObject<s3_object>(result);
             foreach (var s3ObjSub in res.S3Object)
             {
                 if (s3ObjSub != null)
                 {
                     string fullPathSub = Path.Combine(s3ObjSub.Shared ? Utilities.SharedFolderPath + "\\" + s3ObjSub.Username : Utilities.Path, s3ObjSub.Key.Replace("/", "\\"));
                     if (s3ObjSub.Shared)
                     {
                         if (!Directory.Exists(Utilities.SharedFolderPath + "\\" + s3ObjSub.Username))
                             Directory.CreateDirectory(Utilities.SharedFolderPath + "\\" + s3ObjSub.Username);
                     }
                     if (s3ObjSub.Folder)
                     {
                         if (!Directory.Exists(fullPathSub))
                         {
                             DownloadFolder(s3ObjSub, fullPathSub);
                         }
                         else
                         {
                             try
                             {
                                 TimeSpan timeSpan = new DirectoryInfo(fullPathSub).LastWriteTime.ToUniversalTime().Subtract(s3ObjSub.LastModified);
                                 var seconds = Math.Floor(timeSpan.TotalSeconds);
                                 if (seconds != 0)
                                 {
                                     if (seconds < 0)
                                     {
                                         _downloadObjects.Add(fullPathSub);
                                         DownloadFolder(s3ObjSub, fullPathSub);
                                         string relativePath = fullPathSub.Replace(Utilities.Path + "\\", "").Replace("\\", "/");
                                         if (s3ObjSub.Shared)
                                         {
                                             var folders = relativePath.Split('\\');
                                             ProcessApplicationUpdates(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPathSub).LastWriteTime, Status = UpdateStatus.Add }, s3ObjSub.Shared, folders.Length > 1 ? folders[1] : folders[0]);
                                         }
                                         else
                                             ProcessApplicationUpdates(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPathSub).LastWriteTime, Status = UpdateStatus.Add }, false, string.Empty);
                                         //_applicationUpates.Enqueue(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPathSub).LastWriteTime, Status = UpdateStatus.Update });
                                     }
                                     //else
                                     //   Uploadfiles(fullPathSub); // need to figure it out how to do this beacause we cannot upload the entire folder here
                                 }
                             }
                             catch (Exception)
                             {
                                 // Todo
                                 // need to fix this when an error occured
                                 continue;
                             }
                         }
                     }
                     else
                     {
                         if (!File.Exists(fullPathSub))
                         {
                             _downloadObjects.Add(fullPathSub);
                             _service.GetObject(s3ObjSub.Shared ? s3ObjSub.BucketKey : Utilities.MyConfig.BucketKey, s3ObjSub.Key, fullPathSub);
                             string relativePath = fullPathSub.Replace(Utilities.Path + "\\", "").Replace("\\", "/");
                             if (s3ObjSub.Shared)
                             {
                                 var folders = relativePath.Split('\\');
                                 ProcessApplicationUpdates(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPathSub).LastWriteTime, Status = UpdateStatus.Add }, s3ObjSub.Shared, folders.Length > 1 ? folders[1] : folders[0]);
                             }
                             else
                                 ProcessApplicationUpdates(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPathSub).LastWriteTime, Status = UpdateStatus.Add }, false, string.Empty);
                             //_applicationUpates.Enqueue(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPathSub).LastWriteTime, Status = UpdateStatus.Update });
                         }
                         else
                         {
                             try
                             {
                                 if (!Utilities.IsFileUsedbyAnotherProcess(fullPathSub))
                                 {
                                     TimeSpan timeSpan = new FileInfo(fullPathSub).LastWriteTime.ToUniversalTime().Subtract(s3ObjSub.LastModified);
                                     var seconds = Math.Floor(timeSpan.TotalSeconds);
                                     if (seconds != 0)
                                     {
                                         if (seconds < 0)
                                         {
                                             _downloadObjects.Add(fullPathSub);
                                             _service.GetObject(s3ObjSub.Shared ? s3ObjSub.BucketKey : Utilities.MyConfig.BucketKey, s3ObjSub.Key, fullPathSub);
                                             string relativePath = fullPathSub.Replace(Utilities.Path + "\\", "").Replace("\\", "/");
                                             if (s3ObjSub.Shared)
                                             {
                                                 var folders = relativePath.Split('\\');
                                                 ProcessApplicationUpdates(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPathSub).LastWriteTime, Status = UpdateStatus.Add }, s3ObjSub.Shared, folders.Length > 1 ? folders[1] : folders[0]);
                                             }
                                             else
                                                 ProcessApplicationUpdates(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPathSub).LastWriteTime, Status = UpdateStatus.Add }, false, string.Empty);
                                             //_applicationUpates.Enqueue(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPathSub).LastWriteTime, Status = UpdateStatus.Update });
                                         }
                                         else
                                             Addobject(fullPathSub, s3ObjSub.Shared ? s3ObjSub.BucketKey : Utilities.MyConfig.BucketKey);
                                     }
                                 }
                             }
                             catch (Exception)
                             {
                                 // Todo
                                 // need to fix this when an error occured
                                 continue;
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (Exception)
     {
         // Todo
         // need to figure it out why it is happening
     }
     finally
     {
         string relativePath = fullPath.Replace(Utilities.Path + "\\", "").Replace("\\", "/");
         if (relativePath.ToLower().IndexOf("shared") == 0)
         {
             var folders = relativePath.Split('\\');
             ProcessApplicationUpdates(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPath).LastWriteTime, Status = UpdateStatus.Add }, true, folders.Length > 1 ? folders[1] : folders[0]);
         }
         else
             ProcessApplicationUpdates(new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(fullPath).LastWriteTime, Status = UpdateStatus.Add }, false, string.Empty);
         //_applicationUpates.Enqueue(new AppUpdateInfo { Key = s3Obj.Key, LastModifiedTime = new DirectoryInfo(fullPath).LastWriteTime, Status = UpdateStatus.Update });
     }
 }
Example #3
0
        //
        // GET: /S3Object/Edit/5
        public ActionResult Edit(string id = null, string prefix = "", int maxKeys = 100)
        {
            ViewBag.prefix = prefix;
              ViewBag.maxKeys = maxKeys;

              var list = new Amazon.S3.Model.ListObjectsRequest();
              list.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]);
              list.WithPrefix(id);

              var s3Objects = s3.ListObjects(list).S3Objects;
              if (s3Objects.Count == 0)
              {
            return HttpNotFound();
              }

              var get = new Amazon.S3.Model.GetObjectRequest();
              get.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]);
              get.WithKey(s3Objects[0].Key);

              var response = s3.GetObject(get);

              S3Object modelObject = new S3Object
              {
            Key = s3Objects[0].Key,
            Size = s3Objects[0].Size,
            LastModified = s3Objects[0].LastModified,
            ContentType = response.ContentType
              };

              return View(modelObject);
        }