public static int TryUploadOrganisationLogoFromTmp(DaoFactory factory)
        {
            var directoryPath = BuildFileDirectory();
            var dataStore     = Global.GetStore();

            if (!dataStore.IsDirectory(directoryPath))
            {
                return(0);
            }

            try
            {
                var photoPaths = Global.GetStore().ListFilesRelative("", directoryPath, OrganisationLogoImgName + "*", false);
                if (photoPaths.Length == 0)
                {
                    return(0);
                }

                byte[] bytes;
                using (var photoTmpStream = dataStore.GetReadStream(Path.Combine(directoryPath, photoPaths[0])))
                {
                    bytes = Global.ToByteArray(photoTmpStream);
                }

                var logoID = factory.InvoiceDao.SaveOrganisationLogo(bytes);
                dataStore.DeleteFiles(directoryPath, "*", false);
                return(logoID);
            }

            catch (Exception ex)
            {
                log4net.LogManager.GetLogger("ASC.CRM").ErrorFormat("TryUploadOrganisationLogoFromTmp failed with error: {0}", ex);
                return(0);
            }
        }
Exemplo n.º 2
0
        public static int TryUploadOrganisationLogoFromTmp()
        {
            var logo_id       = 0;
            var directoryPath = BuildFileDirectory();
            var dataStore     = Global.GetStore();

            if (!dataStore.IsDirectory(directoryPath))
            {
                return(0);
            }

            try
            {
                var photoPath = FromDataStore();
                photoPath = photoPath.Substring(photoPath.IndexOf(directoryPath));

                byte[] bytes;
                using (var photoTmpStream = dataStore.GetReadStream(photoPath))
                {
                    bytes = Global.ToByteArray(photoTmpStream);
                }

                logo_id = Global.DaoFactory.GetInvoiceDao().SaveOrganisationLogo(bytes);
                dataStore.DeleteFiles(directoryPath, "*", false);
                return(logo_id);
            }

            catch (Exception ex)
            {
                log4net.LogManager.GetLogger("ASC.CRM").ErrorFormat("TryUploadOrganisationLogoFromTmp failed with error: {0}", ex);
                return(0);
            }
        }
        public static String UploadLogo(Stream inputStream, bool isTmpDir)
        {
            var imageData = Global.ToByteArray(inputStream);

            var fileExtension = String.Concat("." + Global.GetImgFormatName(ImageFormat.Jpeg));
            var photoPath     = BuildFilePath(fileExtension);

            var result = ExecResizeImage(imageData, OrganisationLogoSize, Global.GetStore(), photoPath);

            return(result);
        }
        public FileUploadResult ProcessUpload(HttpContext context)
        {
            if (!CRMSecurity.IsAdmin)
            {
                throw CRMSecurity.CreateSecurityException();
            }

            var fileUploadResult = new FileUploadResult();

            if (!FileToUpload.HasFilesToUpload(context))
            {
                return(fileUploadResult);
            }

            var file = new FileToUpload(context);

            if (String.IsNullOrEmpty(file.FileName) || file.ContentLength == 0)
            {
                throw new InvalidOperationException(CRMErrorsResource.InvalidFile);
            }

            if (0 < SetupInfo.MaxImageUploadSize && SetupInfo.MaxImageUploadSize < file.ContentLength)
            {
                fileUploadResult.Success = false;
                fileUploadResult.Message = FileSizeComment.GetFileImageSizeNote(CRMCommonResource.ErrorMessage_UploadFileSize, false).HtmlEncode();
                return(fileUploadResult);
            }

            if (FileUtility.GetFileTypeByFileName(file.FileName) != FileType.Image)
            {
                fileUploadResult.Success = false;
                fileUploadResult.Message = CRMJSResource.ErrorMessage_NotImageSupportFormat.HtmlEncode();
                return(fileUploadResult);
            }

            try
            {
                var imageData   = Global.ToByteArray(file.InputStream);
                var imageFormat = ContactPhotoManager.CheckImgFormat(imageData);
                var photoUri    = OrganisationLogoManager.UploadLogo(imageData, imageFormat);

                fileUploadResult.Success = true;
                fileUploadResult.Data    = photoUri;
                return(fileUploadResult);
            }
            catch (Exception exception)
            {
                fileUploadResult.Success = false;
                fileUploadResult.Message = exception.Message.HtmlEncode();
                return(fileUploadResult);
            }
        }
        public static void TryUploadPhotoFromTmp(int contactID, bool isNewContact, string tmpDirName)
        {
            var directoryTmpPath = String.IsNullOrEmpty(tmpDirName) ? BuildFileTmpDirectory(contactID) : BuildFileTmpDirectory(tmpDirName);
            var directoryPath    = BuildFileDirectory(contactID);
            var dataStore        = Global.GetStore();

            if (!dataStore.IsDirectory(directoryTmpPath))
            {
                return;
            }

            try
            {
                if (dataStore.IsDirectory(directoryPath))
                {
                    DeletePhoto(contactID, false, null, false);
                }
                foreach (var photoSize in new[] { _bigSize, _mediumSize, _smallSize })
                {
                    var photoTmpPath = FromDataStoreRelative(isNewContact ? 0 : contactID, photoSize, true, tmpDirName);

                    var imageExtension = Path.GetExtension(photoTmpPath);

                    var photoPath = String.Concat(directoryPath, BuildFileName(contactID, photoSize), imageExtension).TrimStart('/');

                    byte[] data;
                    using (var photoTmpStream = dataStore.GetReadStream(photoTmpPath))
                    {
                        data = Global.ToByteArray(photoTmpStream);
                    }
                    using (var fileStream = new MemoryStream(data))
                    {
                        var photoUri = dataStore.Save(photoPath, fileStream).ToString();
                        photoUri = String.Format("{0}?cd={1}", photoUri, DateTime.UtcNow.Ticks);
                        ToCache(contactID, photoUri, photoSize);
                    }
                }
                DeletePhoto(contactID, true, tmpDirName, true);
            }
            catch (Exception ex)
            {
                log4net.LogManager.GetLogger("ASC.CRM").ErrorFormat("TryUploadPhotoFromTmp for contactID={0} failed witth error: {1}", contactID, ex);
                return;
            }
        }
        public static PhotoData UploadPhotoToTemp(Stream inputStream, String tmpDirName, bool checkFormat = true)
        {
            var imageData = Global.ToByteArray(inputStream);

            return(UploadPhotoToTemp(imageData, tmpDirName, checkFormat));
        }
        public static PhotoData UploadPhoto(Stream inputStream, int contactID, bool uploadOnly, bool checkFormat = true)
        {
            var imageData = Global.ToByteArray(inputStream);

            return(UploadPhoto(imageData, contactID, uploadOnly, checkFormat));
        }
        public static String UploadPhoto(Stream inputStream, String tmpDirName)
        {
            var imageData = Global.ToByteArray(inputStream);

            return(UploadPhoto(imageData, tmpDirName));
        }
        public static String UploadPhoto(Stream inputStream, int contactID, bool uploadOnly)
        {
            var imageData = Global.ToByteArray(inputStream);

            return(UploadPhoto(imageData, contactID, uploadOnly));
        }