public void DeleteFolder(long folderId)
        {
            var userData       = _readOnlyRepository.First <Account>(a => a.EMail == User.Identity.Name);
            var folderToDelete = userData.Files.FirstOrDefault(f => f.Id == folderId);

            //if (!folderToDelete.IsDirectory)
            //    {
            //        var deleteRequest = new DeleteObjectRequest
            //        {
            //            BucketName = userData.BucketName,
            //            Key = fileToDelete.Url + fileToDelete.Name
            //        };
            //        AWSClient.DeleteObject(deleteRequest);
            //        fileToDelete.IsArchived = true;
            //    }
            //    else
            //    {
            var userFiles = userData.Files.Where(t => t.Url.Contains(folderToDelete.Name));

            foreach (var file in userFiles)
            {
                if (file == null)
                {
                    continue;
                }

                if (file.IsDirectory)
                {
                    DeleteFolder(file.Id);
                }

                var fileFolderArray = file.Url.Split('/');
                var fileFolder      = fileFolderArray.Length > 1 ? fileFolderArray[fileFolderArray.Length - 2] : fileFolderArray.FirstOrDefault();

                if (!file.IsArchived && fileFolder.Equals(folderToDelete.Name) &&
                    !string.Equals(file.Name, folderToDelete.Name))
                {
                    var deleteRequest = new DeleteObjectRequest
                    {
                        BucketName = userData.BucketName,
                        Key        = file.Url + file.Name
                    };
                    AWSClient.DeleteObject(deleteRequest);
                    AddActivity("El usuario ha borrado el folder " + folderToDelete.Name);
                    file.IsArchived = true;
                    _writeOnlyRepository.Update(userData);
                }
            }

            folderToDelete.IsArchived = true;
            var deleteRequest2 = new DeleteObjectRequest
            {
                BucketName = userData.BucketName,
                Key        = folderToDelete.Url + folderToDelete.Name + "/"
            };

            AWSClient.DeleteObject(deleteRequest2);
            _writeOnlyRepository.Update(userData);
            //}
        }
        public ActionResult RenameObject(long objectId, string newObjectName, string clientDateTime2)
        {
            var userData   = _readOnlyRepository.First <Account>(a => a.EMail == User.Identity.Name);
            var fileData   = _readOnlyRepository.GetById <File>(objectId);
            var clientDate = Convert.ToDateTime(clientDateTime2);

            if (!fileData.IsDirectory)
            {
                //Copy the object
                var copyRequest = new CopyObjectRequest
                {
                    SourceBucket      = userData.BucketName,
                    SourceKey         = fileData.Url + fileData.Name,
                    DestinationBucket = userData.BucketName,
                    DestinationKey    = fileData.Url + newObjectName + "." + (fileData.Name.Split('.').LastOrDefault()),
                    CannedACL         = S3CannedACL.PublicRead
                };

                AWSClient.CopyObject(copyRequest);

                //Delete the original
                var deleteRequest = new DeleteObjectRequest
                {
                    BucketName = userData.BucketName,
                    Key        = fileData.Url + fileData.Name
                };
                AWSClient.DeleteObject(deleteRequest);

                fileData.ModifiedDate = clientDate;
                fileData.Name         = newObjectName + "." + (fileData.Name.Split('.').LastOrDefault());
                _writeOnlyRepository.Update(fileData);
            }
            else
            {
                RenameFolder(objectId, fileData.Name, newObjectName, clientDateTime2);
                fileData.ModifiedDate = clientDate;
                fileData.Name         = newObjectName;
                _writeOnlyRepository.Update(fileData);
            }

            return(RedirectToAction("ListAllContent"));
        }
        public void RenameFolder(long objectId, string oldObjectName, string newObjectName, string clientDateTime2)
        {
            var userData = _readOnlyRepository.First <Account>(a => a.EMail == User.Identity.Name);
            var fileData = userData.Files.FirstOrDefault(f => f.Id == objectId);

            var userFiles = userData.Files.Where(t => t.Url.Contains(fileData.Name));

            var clientDate = Convert.ToDateTime(clientDateTime2);
            var newFoldUrl = string.IsNullOrEmpty(fileData.Url) || string.IsNullOrWhiteSpace(fileData.Url)
                ? newObjectName + "/"
                : fileData.Url.Replace(oldObjectName, newObjectName) + fileData.Name + "/";

            var putFolder = new PutObjectRequest {
                BucketName = userData.BucketName, Key = newFoldUrl, ContentBody = string.Empty
            };

            AWSClient.PutObject(putFolder);

            foreach (var file in userFiles)
            {
                if (file == null)
                {
                    continue;
                }

                if (file.IsDirectory)
                {
                    RenameFolder(file.Id, oldObjectName, newObjectName, clientDateTime2);
                }
                else
                {
                    //Copy the object
                    var newUrl = file.Url.Replace(oldObjectName, newObjectName) + file.Name;

                    var copyRequest = new CopyObjectRequest
                    {
                        SourceBucket      = userData.BucketName,
                        SourceKey         = file.Url + file.Name,
                        DestinationBucket = userData.BucketName,
                        DestinationKey    = newUrl,
                        CannedACL         = S3CannedACL.PublicRead
                    };

                    AWSClient.CopyObject(copyRequest);

                    //Delete the original
                    var deleteRequest = new DeleteObjectRequest
                    {
                        BucketName = userData.BucketName,
                        Key        = file.Url + file.Name
                    };
                    AWSClient.DeleteObject(deleteRequest);

                    file.ModifiedDate = clientDate;
                    file.Url          = file.Url.Replace(oldObjectName, newObjectName);
                    _writeOnlyRepository.Update(file);
                }
            }//fin foreach

            var deleteFolderRequest = new DeleteObjectRequest
            {
                BucketName = userData.BucketName,
                Key        = fileData.Url + fileData.Name + "/"
            };

            AWSClient.DeleteObject(deleteFolderRequest);
            var newFolderUrl = fileData.Url.Replace(oldObjectName, newObjectName);

            fileData.Url = newFolderUrl;

            _writeOnlyRepository.Update(fileData);
        }
        public ActionResult DeleteFile(int fileId)
        {
            var userData     = _readOnlyRepository.First <Account>(a => a.EMail == User.Identity.Name);
            var fileToDelete = userData.Files.FirstOrDefault(f => f.Id == fileId);

            if (fileToDelete != null)
            {
                if (!fileToDelete.IsDirectory)
                {
                    var deleteRequest = new DeleteObjectRequest
                    {
                        BucketName = userData.BucketName,
                        Key        = fileToDelete.Url + fileToDelete.Name
                    };
                    AWSClient.DeleteObject(deleteRequest);
                    fileToDelete.IsArchived = true;
                }
                else//Borrar carpeta con todos sus archivos
                {
                    DeleteFolder(fileToDelete.Id);
                    //var filesList = new List<KeyVersion>();
                    //var userFiles = userData.Files;

                    //foreach (var file in userFiles)
                    //{
                    //    if (file == null)
                    //        continue;

                    //    var fileFolderArray = file.Url.Split('/');
                    //    var fileFolder = fileFolderArray.Length > 1 ? fileFolderArray[fileFolderArray.Length - 2] : fileFolderArray.FirstOrDefault();

                    //    if (!file.IsArchived && fileFolder.Equals(fileToDelete.Name) &&
                    //        !string.Equals(file.Name, fileToDelete.Name))
                    //    {
                    //        filesList.Add(!file.IsDirectory ? new KeyVersion(file.Url + file.Name) : new KeyVersion(file.Url+file.Name + "/"));
                    //        file.IsArchived = true;
                    //    }

                    fileToDelete.IsArchived = true;
                    //}

                    //filesList.Add(new KeyVersion(fileToDelete.Name+"/"));

                    //var deleteRequest = new DeleteObjectsRequest
                    //{
                    //    BucketName = userData.BucketName,
                    //    Keys = filesList
                    //};

                    //AWSClient.DeleteObjects(deleteRequest);

                    var deleteRequest = new DeleteObjectRequest
                    {
                        BucketName = userData.BucketName,
                        Key        = fileToDelete.Url + fileToDelete.Name + "/"
                    };
                    AWSClient.DeleteObject(deleteRequest);
                    AddActivity("El usuario ha borrado el siguiente archivo " + fileToDelete.Name);
                }
                _writeOnlyRepository.Update(userData);
            }

            return(RedirectToAction("ListAllContent"));
        }