/// <summary> /// Updates an existing resource in the Localization table /// </summary> /// <param name="resourceId"></param> /// <param name="value"></param> /// <param name="culture"></param> /// <param name="resourceSet"></param> public static int UpdateResource(string resourceId, object value, CultureInfo culture, string resourceSet, bool valueIsFileName) { value = value ?? String.Empty; var localeId = LocaleFromCulture(culture); var result = ctx.Localization.FirstOrDefault( r => (r.ResourceSet == resourceSet && r.ResourceId == resourceId && r.LocaleId == localeId)); if (result != null) { if (result.GetType().Name.Equals("BinaryLocalization")) { string textFile = null; byte[] binFile = null; FileInfoFormat fi = FileObjectToBinary(value, out binFile, out textFile); ((ImageResource)result).FileName = fi.FileName; ((ImageResource)result).BinData = fi.BinContent; } else { ((StringResource)result).Value = value as string; } } else { ErrorMessage = "Error"; return(-1); } ResetCache(); ctx.SaveChanges(); return(1); }
/// <summary> /// Internal routine that looks at a file and based on its /// extension determines how that file needs to be stored in the /// database. Returns FileInfoFormat structure /// </summary> /// <param name="fileName"></param> /// <returns></returns> private static FileInfoFormat GetFileInfo(string fileName) { var details = new FileInfoFormat(); var fi = new FileInfo(fileName); if (!fi.Exists) { throw new InvalidOperationException("Invalid Filename"); } var extension = fi.Extension.ToLower().TrimStart('.'); details.FileName = fi.Name; switch (extension) { case "js": case "css": case "txt": details.FileFormatType = FileFormatTypes.Text; using (var sr = new StreamReader(fileName, Encoding.Default, true)) { details.TextContent = sr.ReadToEnd(); } details.ValueString = details.FileName + ";" + typeof(string).AssemblyQualifiedName + ";" + Encoding.Default.HeaderName; break; case "png": case "bmp": case "jpg": case "gif": details.FileFormatType = FileFormatTypes.Image; details.BinContent = File.ReadAllBytes(fileName); details.ValueString = details.FileName + ";" + typeof(Bitmap).AssemblyQualifiedName; break; default: details.BinContent = File.ReadAllBytes(fileName); details.ValueString = details.FileName + ";" + typeof(System.Byte[]).AssemblyQualifiedName; break; } return(details); }
/// <summary> /// Internal routine that looks at a file and based on its /// extension determines how that file needs to be stored in the /// database. Returns FileInfoFormat structure /// </summary> /// <param name="FileName"></param> /// <returns></returns> private FileInfoFormat GetFileInfo(string FileName) { FileInfoFormat Details = new FileInfoFormat(); FileInfo fi = new FileInfo(FileName); if (!fi.Exists) { throw new InvalidOperationException("Invalid Filename"); } string Extension = fi.Extension.ToLower().TrimStart('.'); Details.FileName = fi.Name; if (Extension == "txt" || Extension == "css" || Extension == "js") { Details.FileFormatType = FileFormatTypes.Text; using (StreamReader sr = new StreamReader(FileName, Encoding.Default, true)) { Details.TextContent = sr.ReadToEnd(); } Details.ValueString = Details.FileName + ";" + typeof(string).AssemblyQualifiedName + ";" + Encoding.Default.HeaderName; } else if (Extension == "gif" || Extension == "jpg" || Extension == "bmp" || Extension == "png") { Details.FileFormatType = FileFormatTypes.Image; Details.BinContent = File.ReadAllBytes(FileName); Details.ValueString = Details.FileName + ";" + typeof(Bitmap).AssemblyQualifiedName; } else { Details.BinContent = File.ReadAllBytes(FileName); Details.ValueString = Details.FileName + ";" + typeof(System.Byte[]).AssemblyQualifiedName; } return(Details); }
private static FileInfoFormat FileObjectToBinary(object value, out byte[] binFile, out string textFile) { FileInfoFormat fileData = null; binFile = null; textFile = null; try { fileData = GetFileInfo(value as string); } catch (Exception ex) { ErrorMessage = ex.Message; return(null); } if (fileData.FileFormatType == FileFormatTypes.Text) { textFile = fileData.TextContent; } else { binFile = fileData.BinContent; } return(fileData); }
/// <summary> /// Updates an existing resource in the Localization table /// </summary> /// <param name="ResourceId"></param> /// <param name="Value"></param> /// <param name="CultureName"></param> /// <param name="ResourceSet"></param> /// <param name="Type"></param> public int UpdateResource(string ResourceId, object Value, string CultureName, string ResourceSet, bool ValueIsFileName) { string Type = ""; if (CultureName == null) { CultureName = ""; } wwSqlDataAccess Data = new wwSqlDataAccess(wwDbResourceConfiguration.Current.ConnectionString); if (this.Transaction != null) { Data.Transaction = this.Transaction; } if (Value != null && !(Value is string)) { Type = Value.GetType().AssemblyQualifiedName; try { LosFormatter output = new LosFormatter(); StringWriter writer = new StringWriter(); output.Serialize(writer, Value); Value = writer.ToString(); } catch (Exception ex) { this.ErrorMessage = ex.Message; return(-1); } } else { Type = ""; if (Value == null) { Value = ""; } } byte[] BinFile = null; string TextFile = null; string FileName = ""; if (ValueIsFileName) { FileInfoFormat FileData = null; try { FileData = this.GetFileInfo(Value as string); } catch (Exception ex) { this.ErrorMessage = ex.Message; return(-1); } Type = "FileResource"; Value = FileData.ValueString; FileName = FileData.FileName; if (FileData.FileFormatType == FileFormatTypes.Text) { TextFile = FileData.TextContent; } else { BinFile = FileData.BinContent; } } if (Value == null) { Value = ""; } // *** Set up Binfile and TextFile parameters which are set only for // *** file values - otherwise they'll pass as Null values. SqlParameter BinFileParm = new SqlParameter("@BinFile", SqlDbType.Image); if (BinFile == null) { BinFileParm.Value = DBNull.Value; } else { BinFileParm.Value = BinFile; } SqlParameter TextFileParm = new SqlParameter("@TextFile", SqlDbType.NText); if (TextFile == null) { TextFileParm.Value = DBNull.Value; } else { TextFileParm.Value = TextFile; } int Result = 0; string Sql = "update " + wwDbResourceConfiguration.Current.ResourceTableName + " set Value=@Value, Type=@Type, BinFile=@BinFile,TextFile=@TextFile,FileName=@FileName where LocaleId=@LocaleId AND ResourceSet=@ResourceSet and ResourceId=@ResourceId"; Result = Data.ExecuteNonQuery(Sql, new SqlParameter("@ResourceId", ResourceId), new SqlParameter("@Value", Value), new SqlParameter("@Type", Type), new SqlParameter("@LocaleId", CultureName.ToLower()), new SqlParameter("@ResourceSet", ResourceSet), BinFileParm, TextFileParm, new SqlParameter("@FileName", FileName)); if (Result == -1) { this.ErrorMessage = Data.ErrorMessage; return(-1); } return(Result); }
/// <summary> /// Adds a resource to the Localization Table /// </summary> /// <param name="ResourceId"></param> /// <param name="Value"></param> /// <param name="CultureName"></param> /// <param name="ResourceSet"></param> /// <param name="Type"></param> /// <param name="Filename"></param> /// <param name="ValueIsFileName">if true the Value property is a filename to import</param> public int AddResource(string ResourceId, object Value, string CultureName, string ResourceSet, bool ValueIsFileName) { string Type = ""; if (CultureName == null) { CultureName = ""; } wwSqlDataAccess Data = new wwSqlDataAccess(wwDbResourceConfiguration.Current.ConnectionString); if (this.Transaction != null) { Data.Transaction = this.Transaction; } if (Value != null && !(Value is string)) { Type = Value.GetType().AssemblyQualifiedName; try { LosFormatter output = new LosFormatter(); StringWriter writer = new StringWriter(); output.Serialize(writer, Value); Value = writer.ToString(); } catch (Exception ex) { this.ErrorMessage = ex.Message; return(-1); } } else { Type = ""; } byte[] BinFile = null; string TextFile = null; string FileName = ""; if (ValueIsFileName) { FileInfoFormat FileData = null; try { FileData = this.GetFileInfo(Value as string); } catch (Exception ex) { this.ErrorMessage = ex.Message; return(-1); } Type = "FileResource"; Value = FileData.ValueString; FileName = FileData.FileName; if (FileData.FileFormatType == FileFormatTypes.Text) { TextFile = FileData.TextContent; } else { BinFile = FileData.BinContent; } } if (Value == null) { Value = ""; } SqlParameter BinFileParm = new SqlParameter("@BinFile", SqlDbType.Image); if (BinFile == null) { BinFileParm.Value = DBNull.Value; } else { BinFileParm.Value = BinFile; } SqlParameter TextFileParm = new SqlParameter("@TextFile", SqlDbType.NText); if (TextFile == null) { TextFileParm.Value = DBNull.Value; } else { TextFileParm.Value = TextFile; } string Sql = "insert into " + wwDbResourceConfiguration.Current.ResourceTableName + " (ResourceId,Value,LocaleId,Type,Resourceset,BinFile,TextFile,Filename) Values (@ResourceID,@Value,@LocaleId,@Type,@ResourceSet,@BinFile,@TextFile,@FileName)"; if (Data.ExecuteNonQuery(Sql, new SqlParameter("@ResourceId", ResourceId), new SqlParameter("@Value", Value), new SqlParameter("@LocaleId", CultureName.ToLower()), new SqlParameter("@Type", Type), new SqlParameter("@ResourceSet", ResourceSet.ToLower()), BinFileParm, TextFileParm, new SqlParameter("@FileName", FileName)) == -1) { this.ErrorMessage = Data.ErrorMessage; return(-1); } return(1); }