Ejemplo n.º 1
0
        public async Task <string> UploadImage(IFormFile formFile, string userId, string uploadedFileType)
        {
            string filePath = _appSettings.FolderPath.CompanyImagePath;
            var    path     = $"{Environment.CurrentDirectory}\\{_appSettings.FolderPath.RootFolder}\\{_appSettings.FolderPath.CompanyImagePath}";

            string fileName = string.Empty;

            if (uploadedFileType == Constants.UploadProfileImage)
            {
                fileName = $"companyLogo.{formFile.FileName.Split('.')[1]}";
            }
            else if (uploadedFileType == Constants.UploadHeaderLogoImage)
            {
                fileName = $"headerLogo.{formFile.FileName.Split('.')[1]}";
            }
            else
            {
                fileName = $"loginBackgroundImage.{formFile.FileName.Split('.')[1]}";
            }

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string[] files = Directory.GetFiles(path, uploadedFileType == Constants.UploadProfileImage ? $"companyLogo*" :
                                                uploadedFileType == Constants.UploadBackgroundImageForLogin ? $"loginBackgroundImage*" : $"headerLogo*");

            if (files.Length > 0)
            {
                if (File.Exists($"{files[0]}"))
                {
                    File.Delete($"{files[0]}");
                }
            }


            var fileData = Utility.Upload(formFile, path, fileName);
            UploadImageModel uploadImageModel = new UploadImageModel()
            {
                FileName = fileData.FilePath,
                UserId   = userId
            };

            if (fileData != null)
            {
                bool isSuccess = await _accountData.UploadPhoto(fileData.FilePath, uploadedFileType);

                if (isSuccess)
                {
                    return($"{_appSettings.FolderPath.CompanyImagePath}/{fileData.FilePath}");
                }
            }
            return(null);
        }