/// <summary>
        /// Get the unique name of the file (add -copy-(N) to the file name if there is already a file with that name in the directory)
        /// </summary>
        /// <param name="directoryPath">Path to the file directory</param>
        /// <param name="fileName">Original file name</param>
        /// <returns>Unique name of the file</returns>
        protected virtual string GetUniqueFileName(string directoryPath, string fileName)
        {
            var uniqueFileName = fileName;

            var i = 0;

            while (_fileProvider.FileExists(_fileProvider.Combine(directoryPath, uniqueFileName)))
            {
                uniqueFileName = $"{_fileProvider.GetFileNameWithoutExtension(fileName)}-Copy-{++i}{_fileProvider.GetFileExtension(fileName)}";
            }

            return(uniqueFileName);
        }
Пример #2
0
        /// <summary>
        /// Indicates whether assembly file is already loaded
        /// </summary>
        /// <param name="filePath">File path</param>
        /// <returns>True if assembly file is already loaded; otherwise false</returns>
        private static bool IsAlreadyLoaded(string filePath)
        {
            //search library file name in base directory to ignore already existing (loaded) libraries
            //(we do it because not all libraries are loaded immediately after application start)
            if (_baseAppLibraries.Any(sli => sli.Equals(_fileProvider.GetFileName(filePath), StringComparison.InvariantCultureIgnoreCase)))
            {
                return(true);
            }

            //compare full assembly name
            //var fileAssemblyName = AssemblyName.GetAssemblyName(filePath);
            //foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
            //{
            //    if (a.FullName.Equals(fileAssemblyName.FullName, StringComparison.InvariantCultureIgnoreCase))
            //        return true;
            //}
            //return false;

            //do not compare the full assembly name, just filename
            try
            {
                var fileNameWithoutExt = _fileProvider.GetFileNameWithoutExtension(filePath);
                if (string.IsNullOrEmpty(fileNameWithoutExt))
                {
                    throw new Exception($"Cannot get file extension for {_fileProvider.GetFileName(filePath)}");
                }

                foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
                {
                    var assemblyName = a.FullName.Split(',').FirstOrDefault();
                    if (fileNameWithoutExt.Equals(assemblyName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Cannot validate whether an assembly is already loaded. " + exc);
            }

            return(false);
        }
Пример #3
0
        public virtual IActionResult AsyncUpload()
        {
            var httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(Json(new
                {
                    success = false,
                    message = "No file uploaded"
                }));
            }

            var fileBinary = _downloadService.GetDownloadBits(httpPostedFile);

            var qqFileNameParameter = "qqfilename";
            var fileName            = httpPostedFile.FileName;

            if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
            {
                fileName = Request.Form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = _fileProvider.GetFileName(fileName);

            var contentType = httpPostedFile.ContentType;

            var fileExtension = _fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            var download = new Download
            {
                DownloadGuid   = Guid.NewGuid(),
                UseDownloadUrl = false,
                DownloadUrl    = string.Empty,
                DownloadBinary = fileBinary,
                ContentType    = contentType,
                //we store filename without extension for downloads
                Filename  = _fileProvider.GetFileNameWithoutExtension(fileName),
                Extension = fileExtension,
                IsNew     = true
            };

            try
            {
                _downloadService.InsertDownload(download);

                //when returning JSON the mime-type must be set to text/plain
                //otherwise some browsers will pop-up a "Save As" dialog.
                return(Json(new
                {
                    success = true,
                    downloadId = download.Id,
                    downloadUrl = Url.Action("DownloadFile", new { downloadGuid = download.DownloadGuid })
                }));
            }
            catch (Exception exc)
            {
                _logger.Error(exc.Message, exc, _workContext.CurrentCustomer);

                return(Json(new
                {
                    success = false,
                    message = "File cannot be saved"
                }));
            }
        }
Пример #4
0
        public virtual IActionResult UploadFileReturnRequest()
        {
            if (!_orderSettings.ReturnRequestsEnabled || !_orderSettings.ReturnRequestsAllowFiles)
            {
                return(Json(new
                {
                    success = false,
                    downloadGuid = Guid.Empty,
                }));
            }

            var httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(Json(new
                {
                    success = false,
                    message = "No file uploaded",
                    downloadGuid = Guid.Empty,
                }));
            }

            var fileBinary = httpPostedFile.GetDownloadBits();

            var qqFileNameParameter = "qqfilename";
            var fileName            = httpPostedFile.FileName;

            if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
            {
                fileName = Request.Form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = _fileProvider.GetFileName(fileName);

            var contentType = httpPostedFile.ContentType;

            var fileExtension = _fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            var validationFileMaximumSize = _orderSettings.ReturnRequestsFileMaximumSize;

            if (validationFileMaximumSize > 0)
            {
                //compare in bytes
                var maxFileSizeBytes = validationFileMaximumSize * 1024;
                if (fileBinary.Length > maxFileSizeBytes)
                {
                    return(Json(new
                    {
                        success = false,
                        message = string.Format(_localizationService.GetResource("ShoppingCart.MaximumUploadedFileSize"), validationFileMaximumSize),
                        downloadGuid = Guid.Empty,
                    }));
                }
            }

            var download = new Download
            {
                DownloadGuid   = Guid.NewGuid(),
                UseDownloadUrl = false,
                DownloadUrl    = "",
                DownloadBinary = fileBinary,
                ContentType    = contentType,
                //we store filename without extension for downloads
                Filename  = _fileProvider.GetFileNameWithoutExtension(fileName),
                Extension = fileExtension,
                IsNew     = true
            };

            _downloadService.InsertDownload(download);

            //when returning JSON the mime-type must be set to text/plain
            //otherwise some browsers will pop-up a "Save As" dialog.
            return(Json(new
            {
                success = true,
                message = _localizationService.GetResource("ShoppingCart.FileUploaded"),
                downloadUrl = Url.Action("GetFileUpload", "Download", new { downloadId = download.DownloadGuid }),
                downloadGuid = download.DownloadGuid,
            }));
        }
Пример #5
0
        public IActionResult UploadFileProductAttribute(int attributeId)
        {
            var attribute = _productAttributeService.GetProductAttributeMappingById(attributeId);

            if (attribute == null || attribute.AttributeControlType != AttributeControlType.FileUpload)
            {
                return(Json(new
                {
                    success = false,
                    downloadGuid = Guid.Empty
                }));
            }

            var httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(Json(new
                {
                    success = false,
                    message = "No file uploaded",
                    downloadGuid = Guid.Empty
                }));
            }

            var fileBinary = _downloadService.GetDownloadBits(httpPostedFile);

            var qqFileNameParameter = "qqfilename";
            var fileName            = httpPostedFile.FileName;

            if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
            {
                fileName = Request.Form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = _fileProvider.GetFileName(fileName);

            var contentType = httpPostedFile.ContentType;

            var fileExtension = _fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            if (attribute.ValidationFileMaximumSize.HasValue)
            {
                //compare in bytes
                var maxFileSizeBytes = attribute.ValidationFileMaximumSize.Value * 1024;
                if (fileBinary.Length > maxFileSizeBytes)
                {
                    //when returning JSON the mime-type must be set to text/plain
                    //otherwise some browsers will pop-up a "Save As" dialog.
                    return(Json(new
                    {
                        success = false,
                        message = string.Format(_localizationService.GetResource("ShoppingCart.MaximumUploadedFileSize"), attribute.ValidationFileMaximumSize.Value),
                        downloadGuid = Guid.Empty
                    }));
                }
            }

            var download = new Download
            {
                DownloadGuid   = Guid.NewGuid(),
                UseDownloadUrl = false,
                DownloadUrl    = "",
                DownloadBinary = fileBinary,
                ContentType    = contentType,
                //we store filename without extension for downloads
                Filename  = _fileProvider.GetFileNameWithoutExtension(fileName),
                Extension = fileExtension,
                IsNew     = true
            };

            _downloadService.InsertDownload(download);

            //generate new image merging the uploaded custom logo and product picture
            //newly added features for the override
            var logoPosition = _logoPositionService.GetByProductId(attribute.ProductId);

            (int, string)imagePath = _customLogoService.MergeProductPictureWithLogo(
                download.DownloadBinary,
                download.Id,
                attribute.ProductId,
                ProductPictureModifierUploadType.Custom,
                logoPosition.Size,
                logoPosition.Opacity,
                logoPosition.XCoordinate,
                logoPosition.YCoordinate,
                _mediaSettings.ProductDetailsPictureSize
                );
            //when returning JSON the mime-type must be set to text/plain
            //otherwise some browsers will pop-up a "Save As" dialog.
            return(Json(new
            {
                success = true,
                message = _localizationService.GetResource("ShoppingCart.FileUploaded"),
                downloadUrl = Url.Action("GetFileUpload", "Download", new { downloadId = download.DownloadGuid }),
                downloadGuid = download.DownloadGuid,
                imagePath = imagePath.Item2, //added property for the override
            }));
        }