예제 #1
0
	public bool SetCover()
	{
		if (Photo != null && Photo.DetailImage != null && AlbumId > 0)
		{
			try
			{
				var album = BXIBlockSection.GetById(AlbumId);

				if (album == null)
					return false;

				if (album.Image != null)
					album.Image.Delete();

                BXFile newImage = new BXFile();
                newImage.OwnerModuleId = "iblock";
                newImage.FileNameOriginal = Photo.DetailImage.FileNameOriginal;
                newImage.SetFile(
                    BXImageUtility.GetResizedImageStream(Photo.DetailImage, CoverWidth, CoverHeight),
                    "iblock");
                newImage.Save();

				album.ImageId = newImage.Id;
				album.Save();
			}
			catch
			{
				return false;
			}

			return true;
		}
		return false;
	}
예제 #2
0
	protected BXFile SaveFile()
	{
		FileUpload fu = aifImage.Upload;

		if (!fu.HasFile)
			return null;

		HttpPostedFile postedFile = fu.PostedFile;
		BXFileValidationResult result = BXFile.ValidateAsImage(
			postedFile.FileName,
			postedFile.ContentType,
			postedFile.InputStream,
			BXConfigurationUtility.Options.User.AvatarMaxSizeKB * 1024,
			BXConfigurationUtility.Options.User.AvatarMaxWidth,
			BXConfigurationUtility.Options.User.AvatarMaxHeight
			);

		if (result == BXFileValidationResult.Valid)
		{
			BXFile f = new BXFile(fu.PostedFile, "user", "main", null);
            f.DemandFileUpload();
			f.Save();
			return f;
		}

		if ((result & BXFileValidationResult.InvalidContentType) == 0
			&& (result & BXFileValidationResult.InvalidExtension) == 0
			&& (result & BXFileValidationResult.InvalidImage) == 0
			&& ((result & BXFileValidationResult.InvalidWidth) != 0 || (result & BXFileValidationResult.InvalidHeight) != 0)
			)
		{
			using (MemoryStream destinationStream = new MemoryStream())
			{
				string postedFileName = postedFile.FileName,
					postedFileMimeType = postedFile.ContentType;

				using (System.Drawing.Image image = System.Drawing.Image.FromStream(postedFile.InputStream))
				using (System.Drawing.Image scaledImage = BXImageUtility.Resize(image, BXConfigurationUtility.Options.User.AvatarMaxWidth, BXConfigurationUtility.Options.User.AvatarMaxHeight))
				{
					ImageFormat format = image.RawFormat;
					BXImageFormatInfo fInfo = new BXImageFormatInfo(format);
					/*
					 * GIF в PNG всегда - из-за возможной потери качества при преобразовании в 256 цветов
					 * JPG в PNG если площадь изображения не превышает 128x128
					 */
					if (format.Guid == ImageFormat.Gif.Guid || (format.Guid == ImageFormat.Jpeg.Guid && scaledImage.Width * scaledImage.Height <= 16384))
						fInfo = new BXImageFormatInfo(ImageFormat.Png);
					postedFileName = fInfo.AppendFileExtension(postedFileName);
					postedFileMimeType = fInfo.GetMimeType();
					scaledImage.Save(destinationStream, fInfo.Format);
				}

				BXFile f = new BXFile(null, destinationStream, postedFileName, "user", "main", null, postedFileMimeType);
				if ((result & BXFileValidationResult.InvalidSize) != 0 && f.FileSize <= BXConfigurationUtility.Options.User.AvatarMaxSizeKB * 1024)
					result &= ~BXFileValidationResult.InvalidSize;
				if ((result & BXFileValidationResult.InvalidWidth) != 0 && f.Width <= BXConfigurationUtility.Options.User.AvatarMaxWidth)
					result &= ~BXFileValidationResult.InvalidWidth;
				if ((result & BXFileValidationResult.InvalidHeight) != 0 && f.Height <= BXConfigurationUtility.Options.User.AvatarMaxHeight)
					result &= ~BXFileValidationResult.InvalidHeight;

				if (result == BXFileValidationResult.Valid)
				{
                    f.DemandFileUpload();
					f.Save();
					return f;
				}
			}
		}

		StringBuilder fError = new StringBuilder();
		if ((result & BXFileValidationResult.InvalidContentType) == BXFileValidationResult.InvalidContentType)
			fError.Append(GetMessageRaw("InvalidType"));

		if ((result & BXFileValidationResult.InvalidExtension) == BXFileValidationResult.InvalidExtension)
		{
			if (fError.Length != 0)
				fError.Append(", ");
			fError.Append(GetMessageRaw("InvalidExtension"));
		}

		if ((result & BXFileValidationResult.InvalidImage) == BXFileValidationResult.InvalidImage)
		{
			if (fError.Length != 0)
				fError.Append(", ");
			fError.Append(GetMessageRaw("InvalidImage"));
		}

		if ((result & BXFileValidationResult.InvalidSize) != 0)
		{
			if (fError.Length != 0)
				fError.Append(", ");
			fError.AppendFormat(GetMessageRaw("InvalidSize"), BXStringUtility.BytesToString(BXConfigurationUtility.Options.User.AvatarMaxSizeKB * 1024));
		}

		if ((result & BXFileValidationResult.InvalidWidth) != 0)
		{
			if (fError.Length != 0)
				fError.Append(", ");
			fError.AppendFormat(GetMessageRaw("InvalidWidth"), BXConfigurationUtility.Options.User.AvatarMaxWidth);
		}

		if ((result & BXFileValidationResult.InvalidHeight) != 0)
		{
			if (fError.Length != 0)
				fError.Append(", ");
			fError.AppendFormat(GetMessageRaw("InvalidHeight"), BXConfigurationUtility.Options.User.AvatarMaxHeight);
		}

		throw new Exception(string.Format(GetMessageRaw("Error.InvalidImage"), fError.ToString()));
	}
	protected BXFile SaveFile(string type)
	{
		BXFile fImage = null;

		FileUpload fu;

		if ("detail".Equals(type, StringComparison.InvariantCultureIgnoreCase))
			fu = DetailImg.Upload;
		else
			fu = Img.Upload;

		if (fu.HasFile)
		{
			fImage = new BXFile(fu.PostedFile, "iblock", "iblock", null); 
			BXFileValidationResult fResult = fImage.ValidateAsImage(0, 0, 0);
			if (fResult != BXFileValidationResult.Valid)
			{
				string fError = "";
				if ((fResult & BXFileValidationResult.InvalidContentType) == BXFileValidationResult.InvalidContentType)
					fError += GetMessage("Error.InvalidType");
				if ((fResult & BXFileValidationResult.InvalidExtension) == BXFileValidationResult.InvalidExtension)
				{
					if (!String.IsNullOrEmpty(fError))
						fError += ", ";
					fError += GetMessage("Error.Invalid.Extension");
				}
				if ((fResult & BXFileValidationResult.InvalidImage) == BXFileValidationResult.InvalidImage)
				{
					if (!String.IsNullOrEmpty(fError))
						fError += ", ";
					fError += GetMessage("Error.InvalidImage");
				}
				throw new Exception(fError);
			}
			fImage.DemandFileUpload();
			fImage.Save();
		}

		return fImage;
	}
예제 #4
0
	protected BXFile SaveFile(bitrix_ui_AdminImageField adminImageField, int currentImageId)
	{
		if (adminImageField == null)
			return null;

		BXFile image = null;
		FileUpload fileUpload = adminImageField.Upload;

		if (adminImageField.Upload.HasFile)
		{
			image = new BXFile(fileUpload.PostedFile, "iblock", "iblock", adminImageField.Description);
			BXFileValidationResult result = image.ValidateAsImage(0, 0, 0);
			if (result != BXFileValidationResult.Valid)
			{
				string errorMessage = "";
				if ((result & BXFileValidationResult.InvalidContentType) == BXFileValidationResult.InvalidContentType)
					errorMessage += GetMessageRaw("InvalidType");
				if ((result & BXFileValidationResult.InvalidExtension) == BXFileValidationResult.InvalidExtension)
				{
					if (!String.IsNullOrEmpty(errorMessage))
						errorMessage += ", ";
					errorMessage += GetMessageRaw("InvalidExtension");
				}
				if ((result & BXFileValidationResult.InvalidImage) == BXFileValidationResult.InvalidImage)
				{
					if (!String.IsNullOrEmpty(errorMessage))
						errorMessage += ", ";
					errorMessage += GetMessageRaw("InvalidImage");
				}
				throw new Exception(String.Format(GetMessageRaw("Error.InvalidImage"), errorMessage));
			}
            image.DemandFileUpload();
			image.Save();
			return image;
		}
		else if (currentImageId > 0 && (image = BXFile.GetById(currentImageId)) != null)
		{
			image.Description = adminImageField.Description;
			image.Save();
		}

		return null;
	}
예제 #5
0
	//protected string SavedName;
	//protected string DisplaySize;
	//protected string ContentType;

	
	private void ProcessUploadMode()
	{
		HasError = true;
		UploadMode = true;
		JSCodeMode = false;

		if (string.IsNullOrEmpty(Request.QueryString["id"]))
		{
			ErrorMessage += JSEncode(HtmlEncode(GetMessageRaw("Error.NoId")) + "<br/>");
			return;
		}

		OwnerId = Request.QueryString["id"];
		CachedId = Request.QueryString["old"];

		HttpPostedFile file = Request.Files[OwnerId + "_ValueUpload"];
		if (file == null || file.ContentLength == 0)
		{
			ErrorMessage += JSEncode(HtmlEncode(GetMessageRaw("Error.NoFile")) + "<br/>");
			return;
		}

		try
		{
			string cacheid = Guid.NewGuid().ToString("N");			
			bool success = false;
			try
			{
				Uploaded = new BXFile(BXTextEncoder.EmptyTextEncoder, file, "uf", "main", "");
				Uploaded.TempGuid = cacheid;
				Uploaded.DemandFileUpload();
				Uploaded.Save();
				success = true;				
			}
			catch(BXSecurityException)
			{
				ErrorMessage += JSEncode(HtmlEncode(GetMessageRaw("Error.InsufficientRights")) + "<br/>");
			}
			catch
			{
			}

			if (!success)
				return;
			
			//Delete Old File
			BXFile old;
			if (!string.IsNullOrEmpty(CachedId) && (old = BXFile.GetByTempGuid(CachedId, BXTextEncoder.EmptyTextEncoder)) != null)
			{
				try
				{
					old.Delete();
				}
				catch
				{
				}
			}

			//Set New File Params
			CachedId = cacheid;						
				
			HasError = false;
		}
		catch
		{
		}
	}
예제 #6
0
    protected BXFile SaveFile(HttpPostedFile postedFile)
    {
        BXFile image = new BXFile(postedFile, "iblock", "iblock", null);
		BXFileValidationResult fResult = image.ValidateAsImage(0, 0, 0);
        if (fResult != BXFileValidationResult.Valid)
        {
            string fError = "";
            if ((fResult & BXFileValidationResult.InvalidContentType) == BXFileValidationResult.InvalidContentType)
                fError += GetMessageRaw("Error.InvalidType");
            if ((fResult & BXFileValidationResult.InvalidExtension) == BXFileValidationResult.InvalidExtension)
            {
                if (!String.IsNullOrEmpty(fError))
                    fError += ", ";
                fError += GetMessageRaw("Error.Inavlid.extension");
            }
            if ((fResult & BXFileValidationResult.InvalidImage) == BXFileValidationResult.InvalidImage)
            {
                if (!String.IsNullOrEmpty(fError))
                    fError += ", ";
                fError += GetMessageRaw("Error.InvalidImage");
            }
            throw new FormatException(fError);
        }
        image.DemandFileUpload();
        image.Save();
        return image;
    }
예제 #7
0
	protected BXFile SaveFile()
	{
		BXFile fImage = null;

		if (Img.Upload.HasFile)
		{
			fImage = new BXFile(Img.Upload.PostedFile, "iblock", "iblock", null);
			BXFileValidationResult fResult = fImage.ValidateAsImage(0, 0, 0);
			if (fResult != BXFileValidationResult.Valid)
			{
				string fError = "";
				if ((fResult & BXFileValidationResult.InvalidContentType) == BXFileValidationResult.InvalidContentType)
					fError += GetMessage("Error.InvalidType");
				if ((fResult & BXFileValidationResult.InvalidExtension) == BXFileValidationResult.InvalidExtension)
				{
					if (!String.IsNullOrEmpty(fError))
						fError += ", ";
					fError += GetMessage("Error.InvalidExtension");
				}
				if ((fResult & BXFileValidationResult.InvalidImage) == BXFileValidationResult.InvalidImage)
				{
					if (!String.IsNullOrEmpty(fError))
						fError += ", ";
					fError += GetMessage("Error.InvalidImage");
				}
				throw new Exception(fError);
			}
            fImage.DemandFileUpload();
			fImage.Save();
		}

		return fImage;
	}