Exemple #1
0
        protected OperationResponse <AppUserGetByIdCommandOutputDTO> GetCurrentUser()
        {
            var result = new OperationResponse <AppUserGetByIdCommandOutputDTO>();

            using (var scope = IoCGlobal.NewScope())
            {
                using (var dbContextScope = this.DbContextScopeFactory.Create())
                {
                    var getByIdResult = this.Repository.GetById(this.CurrentUserService.CurrentUserId);
                    result.AddResponse(getByIdResult);
                    if (result.IsSucceed)
                    {
                        result.Bag = new AppUserGetByIdCommandOutputDTO
                        {
                            Id         = getByIdResult.Bag.Id,
                            Email      = getByIdResult.Bag.Email,
                            UserName   = getByIdResult.Bag.UserName,
                            FirstName  = getByIdResult.Bag.FirstName,
                            LastName   = getByIdResult.Bag.LastName,
                            PictureUrl = getByIdResult.Bag.PictureUrl,
                        };
                    }
                }
            }

            return(result);
        }
 public void SendMessageDataChanged(SignalREventArgs @event)
 {
     using (var scope = IoCGlobal.NewScope())
     {
         this.Clients.All.DataChanged(@event);
     }
 }
        private async Task DeleteFile(HttpContext context)
        {
            var fileRepositoryIdStr = context.Request.Query["id"];

            if (int.TryParse(fileRepositoryIdStr, out int fileRepositoryId))
            {
                //Scope name: AutofacWebRequest is mandatory to match the WebApi scope created.
                //If the scope is changed some injections stop working with the message that "AutofacWebRequest" scope was not found.
                using (var scope = IoCGlobal.NewScope("AutofacWebRequest"))
                {
                    try
                    {
                        var getFileDataRetriever = IoCGlobal.Resolve <IFileRetriever>(null, scope);

                        var fileInfoResult   = getFileDataRetriever.GetFileData(fileRepositoryId);
                        var fileInfo         = fileInfoResult.Bag;
                        var fileDeleteResult = getFileDataRetriever.DeleteFile(fileRepositoryId);
                        if (!fileInfoResult.IsSucceed)
                        {
                            return;
                        }

                        //string mimeType = null;
                        //var fileStorageService = IoCGlobal.Resolve<IFileStorageService>(fileInfo.FileSystemTypeId, scope);
                        //byte[] result = null;

                        //result = await fileStorageService.RetrieveFile(fileInfo.RootPath, fileInfo.AccessPath, fileInfo.RelativePath, fileInfo.FileName);
                        //mimeType = FileHelpers.GetMimeTypeByExtension(fileInfo.FileName);


                        //context.Response.OnStarting(state =>
                        //{
                        //    var httpContext = (HttpContext)state;
                        //    httpContext.Response.ContentType = mimeType;
                        //    return Task.FromResult(0);
                        //}, context);

                        //var memStream = new MemoryStream(result);
                        //memStream.CopyTo(context.Response.Body);
                        //context.Response.ContentType = mimeType;
                        context.Response.Body.Flush();
                    }
                    catch (Exception ex)
                    {
                        Logger.Error($"File Delete - {fileRepositoryId}", ex);
                        throw;
                    }
                    return;
                }
            }
        }
        public OperationResponse <FileInsertCommandOutputDTO> Execute <T, A>(T input) where T : FileArgs <A> where A : UploadedFile
        {
            //using (var transaction = new TransactionScope())
            //{
            DomainModel.File.File file = null;
            var defaultFileStorageId   = (string)typeof(FileSourceEnum).GetStaticPropertyValue(AppConfig.Instance.FileStorageSettings.DefaultFileStorageDestination);
            var fileStorageTypeId      = input.FileSource ?? defaultFileStorageId;
            var result = new OperationResponse <FileInsertCommandOutputDTO>();

            using (var dbContextScope = this.DbContextScopeFactory.Create())
            {
                file = new DomainModel.File.File
                {
                    RootPath          = FileInsertCommand.FirstStepDefaultFileName,
                    AccessPath        = FileInsertCommand.FirstStepDefaultAccessPath,
                    RelativePath      = FileInsertCommand.FirstStepDefaultFolderPath,
                    FileName          = FileInsertCommand.FirstStepDefaultFileName,
                    FileSize          = input.UploadedFile.ContentLength,
                    ThumbnailFileSize = input.UploadedFile.ThumbnailContent.Length
                };

                OperationResponse <DomainModel.File.File> fileInsertResult = null;


                // First step. Save file without paths.
                fileInsertResult = this.Repository.Insert(file);
                result.AddResponse(fileInsertResult);

                // Second step. Store file and grab its path details.
                input.FilePrefix = file.Id.ToString();
                FileStorageResultDTO fileStorageResult;
                using (var scope = IoCGlobal.NewScope("FileStorageScope"))
                {
                    var fileStorage = IoCGlobal.Resolve <IFileStorageService>(fileStorageTypeId, scope);
                    fileStorageResult = fileStorage.Save(input);
                }

                file.RootPath              = fileStorageResult.RootPath;
                file.AccessPath            = fileStorageResult.AccessPath;
                file.RelativePath          = fileStorageResult.FolderPath;
                file.FileName              = fileStorageResult.FileName;
                file.FullFilePath          = fileStorageResult.FullFilePath;
                file.ThumbnailFileName     = fileStorageResult.ThumbnailFileName;
                file.ThumbnailFullFilePath = fileStorageResult.ThumbnailFullFilePath;
                file.FileSystemTypeId      = fileStorageResult.FileSourceId;
                dbContextScope.SaveChanges();

                var getById = this.Repository.GetById(file.Id);
                result.AddResponse(getById);
                if (result.IsSucceed)
                {
                    result.Bag = new FileInsertCommandOutputDTO
                    {
                        Id = getById.Bag.Id
                    };
                }
                else
                {
                    this.Logger.Error("Error retrieving File", new OperationResponseException(result));
                }

                return(result);
            }
        }
        private async Task RetrieveFile(HttpContext context)
        {
            var  fileRepositoryIdStr = context.Request.Query["id"];
            bool returnThumbNail     = true;

            {
                var returnThumbnailStr = context.Request.Query["thumbnail"];
                bool.TryParse(returnThumbnailStr, out returnThumbNail);
            }

            if (int.TryParse(fileRepositoryIdStr, out int fileRepositoryId))
            {
                //Scope name: AutofacWebRequest is mandatory to match the WebApi scope created.
                //If the scope is changed some injections stop working with the message that "AutofacWebRequest" scope was not found.
                using (var scope = IoCGlobal.NewScope("AutofacWebRequest"))
                {
                    try
                    {
                        var getFileDataRetriever = IoCGlobal.Resolve <IFileRetriever>(null, scope);

                        var fileInfoResult = getFileDataRetriever.GetFileData(fileRepositoryId);
                        if (!fileInfoResult.IsSucceed || fileInfoResult.Bag == null)
                        {
                            throw new Exception();
                        }

                        var fileInfo = fileInfoResult.Bag;

                        string mimeType           = null;
                        var    fileStorageService = IoCGlobal.Resolve <IFileStorageService>(fileInfo.FileSystemTypeId, scope);
                        byte[] result             = null;
                        if (returnThumbNail)
                        {
                            var thumbnailFileName = fileInfo.ThumbnailFileName ?? fileInfo.FileName;
                            result = await fileStorageService.RetrieveFile(fileInfo.RootPath, fileInfo.AccessPath, fileInfo.RelativePath, thumbnailFileName);

                            mimeType = FileHelpers.GetMimeTypeByExtension(thumbnailFileName);
                        }
                        else
                        {
                            result = await fileStorageService.RetrieveFile(fileInfo.RootPath, fileInfo.AccessPath, fileInfo.RelativePath, fileInfo.FileName);

                            mimeType = FileHelpers.GetMimeTypeByExtension(fileInfo.FileName);
                        }

                        if (result == null || result.Length == 0)
                        {
                            throw new FileNotFoundException();
                        }

                        context.Response.OnStarting(state =>
                        {
                            var httpContext = (HttpContext)state;
                            httpContext.Response.ContentType = mimeType;
                            return(Task.FromResult(0));
                        }, context);



                        var memStream = new MemoryStream(result);
                        memStream.CopyTo(context.Response.Body);
                        //context.Response.ContentType = mimeType;
                        context.Response.Body.Flush();
                    }
                    catch (Exception ex)
                    {
                        Logger.Error($"File Retrieve - {fileRepositoryId}", ex);
                        throw;
                    }
                    return;
                }
            }
            else
            {
                throw new Exception();
            }
        }