Ejemplo n.º 1
0
        public async Task <ActionResult <bool> > CommitUploadTransaction(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory, Guid transactionId,
            CancellationToken cancellationToken)
        {
            using var driveScope = driveScopeFactory.CreateInstance();

            var drive = await m_globalScope.Drives.GetAsync(driveScope.DriveId, cancellationToken);

            var transaction = await m_transactions.GetAsync(transactionId, cancellationToken);

            var item = await driveScope.StorageItems.GetAsync(transaction.StorageItemId, cancellationToken);

            item.State = StorageItemState.Uploaded;
            await driveScope.StorageItems.UpdateAsync(item, null, cancellationToken);

            if (await m_uploadTransactionService.CommitTransaction(transactionId, item, cancellationToken))
            {
                drive.NumberOfFiles    += 1;
                drive.TotalStorageUsed += item.Size;
                await m_globalScope.Drives.UpdateAsync(drive, cancellationToken);

                return(Ok());
            }

            await driveScope.StorageItems.RemoveAsync(transaction.StorageItemId, cancellationToken);

            await m_transactions.RemoveAsync(transactionId, cancellationToken);

            return(BadRequest("File size is different than promised"));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <StorageItem> > Restore(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory,
            Guid itemId, CancellationToken cancellationToken)
        {
            using var driveScope = driveScopeFactory.CreateInstance();

            var item = await driveScope.StorageItems.GetAsync(itemId, cancellationToken);

            if (item == null)
            {
                return(NotFound());
            }

            if (!item.TrashedExplicitly)
            {
                return(BadRequest($"Item with the id '{item.Id}' is not trashed explicitly."));
            }

            var i = await driveScope.StorageItems.RestoreAsync(item, cancellationToken);

            return(Ok(new
            {
                i.Id, i.DriveId, i.ParentId, i.Name, i.TimeCreated, i.Size,
                i.State, i.Trashed, i.TrashedExplicitly, i.TrashedTime
            }));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <StorageItem> > Trash(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory,
            Guid itemId, CancellationToken cancellationToken)
        {
            using var driveScope = driveScopeFactory.CreateInstance();

            var item = await driveScope.StorageItems.GetAsync(itemId, cancellationToken);

            if (item == null)
            {
                return(NotFound());
            }
            if (item.Trashed)
            {
                return(BadRequest("File already trashed."));
            }
            if (item.State != StorageItemState.Uploaded)
            {
                return(BadRequest("Storage item is not uploaded."));
            }

            var i = await driveScope.StorageItems.TrashAsync(item, DateTime.UtcNow, cancellationToken);

            return(Ok(new
            {
                i.Id, i.DriveId, i.ParentId, i.Name, i.TimeCreated, i.Size,
                i.State, i.Trashed, i.TrashedExplicitly, i.TrashedTime
            }));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <dynamic> > GetFile(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory,
            [FromRoute] Guid id, CancellationToken cancellationToken)
        {
            using var driveScope = driveScopeFactory.CreateInstance();

            var file = await driveScope.StorageItems.GetAsync(id, cancellationToken);

            if (file == null)
            {
                return(NotFound());
            }

            return(Ok(file));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <UploadTransaction> > StartUploadTransaction(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory,
            StorageItem item, CancellationToken cancellationToken)
        {
            using var driveScope = driveScopeFactory.CreateInstance();
            var drive = await m_globalScope.Drives.GetAsync(driveScope.DriveId, cancellationToken);

            if (drive.Capacity - drive.TotalStorageUsed < item.Size)
            {
                return(BadRequest("Not enough space in Drive"));
            }

            if (item.ParentId != null)
            {
                var parentFolder = await driveScope.StorageItems.GetAsync((Guid)item.ParentId, cancellationToken);

                if (parentFolder == null)
                {
                    return(NotFound($"Folder {item.ParentId} does not exist."));
                }
                if (parentFolder.GetType() != typeof(Folder))
                {
                    return(BadRequest($"Item {item.ParentId} is not a folder."));
                }
            }

            var fileName = item.Name;

            if (string.IsNullOrWhiteSpace(fileName))
            {
                return(BadRequest("File name can not be empty or only white space."));
            }
            fileName = fileName.Trim();
            var file = new StorageItem
            {
                Id          = Guid.NewGuid(),
                DriveId     = driveScope.DriveId,
                Name        = fileName,
                Size        = item.Size,
                State       = StorageItemState.Uploading,
                TimeCreated = DateTime.UtcNow,
            };
            var transaction = await m_uploadTransactionService.StartTransaction(file, cancellationToken);

            await driveScope.StorageItems.AddAsync(file, cancellationToken);

            return(transaction);
        }
Ejemplo n.º 6
0
        public async Task <ActionResult <string> > GetDownloadUri(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory,
            Guid itemId, CancellationToken cancellationToken)
        {
            using var driveScope = driveScopeFactory.CreateInstance();
            var item = await driveScope.StorageItems.GetAsync(itemId, cancellationToken);

            if (item == null)
            {
                return(NotFound());
            }

            var uri = m_managementService.GetDownloadUri(item);

            return(Ok(uri));
        }
Ejemplo n.º 7
0
        public async Task <ActionResult <string> > CreateFolder(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory,
            StorageItem item, CancellationToken cancellationToken)
        {
            using var driveScope = driveScopeFactory.CreateInstance();

            if (item.ParentId != null)
            {
                var parentFolder = await driveScope.StorageItems.GetAsync((Guid)item.ParentId, cancellationToken);

                if (parentFolder == null)
                {
                    return(NotFound($"Folder {item.ParentId} does not exist."));
                }
                if (parentFolder.GetType() != typeof(Folder))
                {
                    return(BadRequest($"Item {item.ParentId} is not a folder."));
                }
            }

            var folderName = item.Name;

            if (string.IsNullOrWhiteSpace(folderName))
            {
                return(BadRequest("Folder name can not be empty or only white space."));
            }
            folderName = folderName.Trim();

            var folder = new Folder
            {
                Id          = Guid.NewGuid(),
                DriveId     = driveScope.DriveId,
                TimeCreated = DateTime.UtcNow,
                ParentId    = item.ParentId,
                Name        = folderName,
                State       = StorageItemState.Uploaded
            };

            await driveScope.StorageItems.AddAsync(folder, cancellationToken);

            return(Ok());
        }
Ejemplo n.º 8
0
        public async Task <ActionResult <StorageItem> > CreateFolder(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory,
            Guid itemId, Guid?newParentId, CancellationToken cancellationToken)
        {
            using var driveScope = driveScopeFactory.CreateInstance();

            if (newParentId != null)
            {
                var parentFolder = await driveScope.StorageItems.GetAsync((Guid)newParentId, cancellationToken);

                if (parentFolder == null)
                {
                    return(NotFound($"Folder {newParentId} does not exist."));
                }
                if (parentFolder is not Folder)
                {
                    return(BadRequest($"Item {newParentId} is not a folder."));
                }
            }

            var item = await driveScope.StorageItems.GetAsync(itemId, cancellationToken);

            if (item == null)
            {
                return(NotFound($"Item {itemId} does not exist."));
            }
            if (item.Trashed)
            {
                return(BadRequest("Can not move trashed items."));
            }

            item.ParentId = newParentId;

            var i = await driveScope.StorageItems.UpdateAsync(item, null, cancellationToken);

            return(Ok(new
            {
                i.Id, i.DriveId, i.ParentId, i.Name, i.TimeCreated, i.Size,
                i.State, i.Trashed, i.TrashedExplicitly, i.TrashedTime
            }));
        }
Ejemplo n.º 9
0
        public async Task <ActionResult> Delete(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory,
            Guid itemId, CancellationToken cancellationToken)
        {
            using var driveScope = driveScopeFactory.CreateInstance();

            var item = await driveScope.StorageItems.GetAsync(itemId, cancellationToken);


            if (item == null)
            {
                return(NotFound());
            }

            if (!item.TrashedExplicitly)
            {
                return(BadRequest("File is not trashed explicitly."));
            }

            await m_managementService.DeleteStorageItem(driveScope, item, cancellationToken);

            return(Ok());
        }
Ejemplo n.º 10
0
        public async Task <ActionResult <string> > Rename(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory,
            Guid itemId, [FromQuery, BindRequired] string newName, [FromQuery, BindRequired] byte[] rowVersion, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(newName))
            {
                return(BadRequest("Name is required"));
            }
            if (rowVersion == null)
            {
                return(BadRequest("RowVersion is required"));
            }

            newName = newName.Trim();

            using var driveScope = driveScopeFactory.CreateInstance();
            var item = await driveScope.StorageItems.GetAsync(itemId, cancellationToken);

            if (item == null)
            {
                return(NotFound());
            }

            item.Name = newName;

            try
            {
                await driveScope.StorageItems.UpdateAsync(item, rowVersion, cancellationToken);
            }
            catch (DbUpdateConcurrencyException)
            {
                return(BadRequest(
                           "The name has been changed by another user. Please refresh the page and try again."));
            }

            return(Ok(item));
        }
Ejemplo n.º 11
0
        public async Task <ActionResult <dynamic> > GetFiles(
            [FromRoute, ModelBinder] IDriveScopeFactory driveScopeFactory,
            [FromQuery] StorageItemQuery query,
            CancellationToken cancellationToken)
        {
            using var driveScope = driveScopeFactory.CreateInstance();

            IEnumerable <dynamic> parentsResult;

            if (query.ParentId == null)
            {
                parentsResult = Array.Empty <Folder>();
            }
            else
            {
                var directFolder = await driveScope.StorageItems.GetAsync((Guid)query.ParentId, cancellationToken);

                if (directFolder == null || directFolder.GetType() != typeof(Folder))
                {
                    return(BadRequest($"No folder found with id '{query.ParentId}'."));
                }

                var parents = (await driveScope.StorageItems.GetParentsAsync(directFolder, cancellationToken)).ToList();
                parents.Insert(0, (Folder)directFolder);
                parentsResult = parents.Select(p => new { p.Id, p.DriveId, p.Name, p.ParentId });
            }

            var items = await driveScope.StorageItems.GetWithQueryAsync(query, cancellationToken);

            var itemsResult = items.Select(i => new
            {
                i.Id, i.DriveId, i.ParentId, i.Name, i.TimeCreated, i.Size,
                i.State, i.Trashed, i.TrashedExplicitly, i.TrashedTime, Type = i.GetType().ToString(), i.RowVersion
            });

            return(Ok(new { Parents = parentsResult, Items = itemsResult }));
        }