// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data; using System.Data.Common; namespace Spludlow.MetSat { /// /// Database inserts and store with thumbnails /// public class DataItems { private Spludlow.Data.IDAL DAL; private Spludlow.Data.DAL.CommandInfo Insert_DataItems; private Spludlow.Data.DAL.CommandInfo Insert_DataItemFiles; private string FileStoreRoot; private Spludlow.Drawing.ThumbNails ThumbNails; public DataItems() { this.SetUp("@MetSat", Spludlow.Config.Get("MetSat.Store"), "MetSat.Thumbs"); } private void SetUp(string connectionString, string fileStoreRoot, string thumbnailsConfigKey) { this.DAL = Spludlow.Data.DAL.Create(connectionString); this.Insert_DataItems = this.DAL.MakeInserter("DataItems"); this.Insert_DataItemFiles = this.DAL.MakeInserter("DataItemFiles"); this.FileStoreRoot = fileStoreRoot; this.ThumbNails = new Drawing.ThumbNails(thumbnailsConfigKey); } public void SaveDataItem( Schema.DataItemFilesDataTable filesTable, string satIntDesId, string dataProductId, DateTime captureTime, string dataItemName) { Schema.DataItemsRow item = (new Schema.DataItemsDataTable()).NewDataItemsRow(); item.ImportSourceId = ""; item.SatIntdesId = satIntDesId; item.DataProductId = dataProductId; item.CaptureTime = captureTime; item.Name = dataItemName; this.DAL.Begin(); try { item.DataItemId = (int)this.DAL.Insert(this.Insert_DataItems, item); foreach (Schema.DataItemFilesRow fileRow in filesTable.Rows) { string[] tempStoreName = ((string)fileRow["Filename"]).Split(new char[] { '@' }); string tempStoreFilename = tempStoreName[0]; fileRow["Filename"] = tempStoreName[1]; fileRow.DataItemId = item.DataItemId; fileRow.DataItemFileId = (int)this.DAL.Insert(this.Insert_DataItemFiles, fileRow); string storeFilename = Spludlow.Io.FileStore.FilePath(fileRow.DataItemFileId, this.FileStoreRoot, fileRow.StoreExtention, true); File.Move(tempStoreFilename, storeFilename); } this.DAL.Commit(); } catch { this.DAL.Rollback(); throw; } foreach (Schema.DataItemFilesRow fileRow in filesTable.Rows) { string storeFilename = Spludlow.Io.FileStore.FilePath(fileRow.DataItemFileId, this.FileStoreRoot, fileRow.StoreExtention, false); this.ThumbNails.WebAndFilePath(storeFilename, true); if (fileRow.StoreExtention == ".7z") Spludlow.Compress.ReCompress(storeFilename); } } public Schema.DataItemFilesDataTable MakeFilesTable() { Schema.DataItemFilesDataTable table = new Schema.DataItemFilesDataTable(); table.DataItemFileIdColumn.ReadOnly = false; table.DataItemIdColumn.AllowDBNull = true; return table; } public Schema.DataItemFilesRow MakeFileRow(Schema.DataItemFilesDataTable filesTable, string dataItemFileTypeId, string name, string channel) { Schema.DataItemFilesRow row = filesTable.NewDataItemFilesRow(); name = Path.GetFileName(name); string extension = Path.GetExtension(name); string mimeFormat = "application/octet-stream"; if (extension == ".png") mimeFormat = "image/png"; string tempStoreFilename = Spludlow.Io.FileStore.TempName(this.FileStoreRoot, extension); row.DataItemFileTypeId = dataItemFileTypeId; row.Filename = tempStoreFilename + "@" + name; row.CheckSum = ""; row.Size = 0; row.Width = 0; row.Height = 0; row.Pixels = 0; row.Channel = channel; row.Format = mimeFormat; row.StoreExtention = extension; filesTable.Rows.Add(row); return row; } } }