Ejemplo n.º 1
0
        private IEnumerable <DownloadToken> GetTransfersForResourceImpl(string virtualFilePath)
        {
            var context = FileSystemTask.DownloadTransfersByResourceQuery;
            var eventId = AuditEvent.ResourceDownloadsQuery;

            var tokens = TransferStore.GetRunningTransfersForResource(virtualFilePath).Select(t => t.Token);

            string msg = "Queried transfers for resource [{0}] and returned [{1}] download tokens.";

            msg = String.Format(msg, virtualFilePath, tokens.Count());

            Auditor.Audit(AuditLevel.Info, context, eventId, msg);
            return(tokens);
        }
Ejemplo n.º 2
0
        private UploadToken GetTransferForResourceImpl(string virtualFilePath)
        {
            const FileSystemTask context = FileSystemTask.UploadTransferByResourceQuery;
            const AuditEvent     eventId = AuditEvent.ResourceUploadQuery;

            var token = TransferStore.GetRunningTransfersForResource(virtualFilePath)
                        .Select(t => t.Token)
                        .SingleOrDefault();

            string msg = "Queried upload transfer for resource [{0}]. Token found: [{1}].";

            msg = String.Format(msg, virtualFilePath, token != null);

            Auditor.Audit(AuditLevel.Info, context, eventId, msg);
            return(token);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Tries to get a given <see cref="TTransfer"/> from the internal
        /// cache.
        /// </summary>
        /// <param name="transferId">The identifier of the transfer, according to the corresponding
        /// <see cref="TransferToken.TransferId"/>.</param>
        /// <param name="throwExceptionIfNotFound">If true, a <see cref="UnknownTransferException"/>
        /// is thrown if no matching transfer was found in the cache.</param>
        /// <param name="context">The file system operation that is being performed during the invocation of
        /// this method. Used for internal auditing.</param>
        /// <returns>Either the matching transfer, or null, if no match was found *and*
        /// <paramref name="throwExceptionIfNotFound"/> is false.</returns>
        protected virtual TTransfer GetCachedTransfer(string transferId, bool throwExceptionIfNotFound, FileSystemTask context)
        {
            TTransfer transfer = TransferStore.TryGetTransfer(transferId);

            if (transfer == null && throwExceptionIfNotFound)
            {
                AuditHelper.AuditUnknownTransferRequest(Auditor, context, transferId);

                string msg = String.Format("Unknown transfer ID: {0}.", transferId);
                throw new UnknownTransferException(msg)
                      {
                          IsAudited = true, EventId = (int)AuditEvent.UnknownTransferRequest
                      };
            }

            return(transfer);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Frees resources associated with a now inactive (completed or canceled) transfer and
        /// finally submits the transfer to the <see cref="ITransferStore{TTransfer}.SetInactive"/>
        /// method of the underlying <see cref="TransferStore"/>.<br/>
        /// This method can be overridden in order to do some custom housekeeping
        /// at the end of every transfer, whether successfully completed or aborted.
        /// </summary>
        /// <param name="transfer">The transfer to be finalized.</param>
        protected virtual void FinalizeTransfer(TTransfer transfer)
        {
            //release locks, if any
            lock (transfer.SyncRoot)
            {
                if (transfer.ResourceLock != null)
                {
                    //release locked resource
                    transfer.ResourceLock.Dispose();
                    transfer.ResourceLock = null;
                }

                if (transfer.ExpirationNotificationJob != null)
                {
                    //cancel the expired job
                    transfer.ExpirationNotificationJob.Cancel();
                }

                //finally set the transfer as inactive
                TransferStore.SetInactive(transfer);
            }
        }
Ejemplo n.º 5
0
        private TTransfer InitTransferImpl(string submittedResourceFilePath, bool overwrite, long resourceLength, string contentType)
        {
            const FileSystemTask context = FileSystemTask.UploadTokenRequest;

            //validate maximum file size
            var maxFileSize = GetMaxFileUploadSize();

            if (maxFileSize.HasValue && maxFileSize < resourceLength)
            {
                string msg = "Upload for file [{0}] denied: Resource length of [{1}] is above the maximum upload limit of [{2}] bytes.";
                msg = String.Format(msg, submittedResourceFilePath, resourceLength, maxFileSize.Value);
                throw new ResourceAccessException(msg);
            }

            //of course, the length cannot be negative
            if (resourceLength < 0)
            {
                string msg = "Upload for file [{0}] denied: Resource length cannot be negative [{1}].";
                msg = String.Format(msg, submittedResourceFilePath, resourceLength);
                throw new ResourceAccessException(msg);
            }


            TFile fileItem = CreateFileItemImpl(submittedResourceFilePath, false, context);

            if (fileItem.Exists && !overwrite)
            {
                AuditHelper.AuditDeniedOperation(Auditor, context, AuditEvent.FileAlreadyExists, fileItem);

                string msg = String.Format("Cannot upload file [{0}] without overwriting existing data - a file already exists at this location.", submittedResourceFilePath);
                throw new ResourceOverwriteException(msg)
                      {
                          IsAudited = true
                      };
            }

            //get authorization
            TFolder parentFolder = GetParentFolder(fileItem, context);

            //validate file system specific restrictions
            VerifyCanUploadFileToFileSystemLocation(submittedResourceFilePath, parentFolder, fileItem);

            //get parent folder and check whether files can be added
            FolderClaims folderClaims = GetParentFolderClaims(fileItem, parentFolder);

            if (!folderClaims.AllowAddFiles)
            {
                //deny adding a file to that folder
                AuditHelper.AuditDeniedOperation(Auditor, context, AuditEvent.CreateFileDenied, fileItem);

                string msg = "Cannot create file at [{0}] - adding files to the folder is not permitted.";
                msg = String.Format(msg, submittedResourceFilePath);
                throw new ResourceAccessException(msg)
                      {
                          IsAudited = true
                      };
            }


            //only overwrite a file if explicitly requested
            FileClaims claims = GetFileClaims(fileItem);

            if (fileItem.Exists)
            {
                if (!claims.AllowOverwrite)
                {
                    AuditHelper.AuditDeniedOperation(Auditor, context, AuditEvent.FileDataOverwriteDenied, fileItem);

                    string msg = "Overwriting file [{0}] was denied due to missing permission.";
                    msg = String.Format(msg, submittedResourceFilePath);
                    throw new ResourceOverwriteException(msg)
                          {
                              IsAudited = true
                          };
                }
            }


            //try to get lock
            ResourceLockGuard writeLock = LockResourceForUpload(fileItem);

            if (writeLock != null && !writeLock.IsLockEnabled)
            {
                AuditHelper.AuditDeniedOperation(Auditor, context, AuditEvent.FileReadLockDenied, fileItem);

                string msg = "The file [{0}] is currently locked and cannot be accessed.";
                msg = String.Format(msg, submittedResourceFilePath);
                throw new ResourceLockedException(msg)
                      {
                          IsAudited = true
                      };
            }

            //create upload token
            UploadToken token = CreateUploadToken(submittedResourceFilePath, fileItem, resourceLength, contentType);

            //create expiration job if we have an expiration time
            Job <UploadToken> job = null;

            if (token.ExpirationTime.HasValue)
            {
                job = ScheduleExpiration(token);
            }

            //create and cache transfer instance
            TTransfer transfer = CreateTransfer(submittedResourceFilePath, fileItem, parentFolder, token, claims, writeLock, job);

            TransferStore.AddTransfer(transfer);

            AuditHelper.AuditResourceOperation(Auditor, context, AuditEvent.UploadTokenIssued, fileItem);
            return(transfer);
        }
Ejemplo n.º 6
0
        private TTransfer InitTransferImpl(string submittedResourceFilePath, int?clientMaxBlockSize, bool includeFileHash)
        {
            const FileSystemTask context = FileSystemTask.DownloadTokenRequest;
            TFile fileItem = CreateFileItemImpl(submittedResourceFilePath, false, context);

            if (!fileItem.Exists)
            {
                Auditor.AuditRequestedFileNotFound(fileItem, context);

                string msg = String.Format("Resource [{0}] not found.", submittedResourceFilePath);
                throw new VirtualResourceNotFoundException(msg)
                      {
                          IsAudited = true
                      };
            }

            //get authorization
            FileClaims claims = GetFileClaims(fileItem);

            if (!claims.AllowReadData)
            {
                Auditor.AuditDeniedOperation(context, AuditEvent.FileDataDownloadDenied, fileItem);

                string msg = "Read request for file [{0}] was denied - you are not authorized to read the resource.";
                msg = String.Format(msg, submittedResourceFilePath);
                throw new ResourceAccessException(msg)
                      {
                          IsAudited = true
                      };
            }

            //try to get lock
            ResourceLockGuard readLock = LockResourceForDownload(fileItem, claims);

            if (readLock != null && !readLock.IsLockEnabled)
            {
                Auditor.AuditDeniedOperation(context, AuditEvent.FileReadLockDenied, fileItem);

                string msg = "The requested file [{0}] is currently locked and thus cannot be accessed.";
                msg = String.Format(msg, submittedResourceFilePath);
                throw new ResourceLockedException(msg)
                      {
                          IsAudited = true
                      };
            }

            //create download token
            DownloadToken token = CreateDownloadToken(submittedResourceFilePath, fileItem, clientMaxBlockSize, includeFileHash);

            //create expiration job if we have an expiration time
            Job <DownloadToken> job = null;

            if (token.ExpirationTime.HasValue)
            {
                job = ScheduleExpiration(token);
            }

            //create transfer instance
            TTransfer transfer = CreateTransfer(submittedResourceFilePath, fileItem, token, claims, readLock, job);

            //cache transfer
            TransferStore.AddTransfer(transfer);

            //audit issued token
            Auditor.AuditResourceOperation(context, AuditEvent.DownloadTokenIssued, fileItem);

            return(transfer);
        }