public void AddContent(IContentTransformer transformer, IContentSource source, byte[] content) { ITransformerStoreModel currentTransformer = GetTransformer(transformer, source); string contentFileName = $"{Guid.NewGuid():N}.bin"; string contentFilePath = Path.Combine(_storeContentDirectoryName, currentTransformer.Id.ToString(), contentFileName); int contentHash = BitConverter.ToInt32(new MD5CryptoServiceProvider().ComputeHash(content), 0); try { lock (_dbConnection) { if (ExistContent(currentTransformer.Id, contentHash)) { return; } _dbConnection.Execute($"INSERT INTO Contents (TransformerId, Created, ContentHash, ContentFileName) VALUES ({currentTransformer.Id}, '{DateTime.Now:yyyy-MM-dd hh:mm:ss}', '{contentHash}', '{contentFileName}')"); File.WriteAllBytes(contentFilePath, content); } } catch { if (File.Exists(contentFilePath)) { File.Delete(contentFilePath); } } }
public void Init() { TransformerCatalog transformerCatalog = TransformerCatalog.TryLoad(); foreach (TransformerConfig transformerConfig in transformerCatalog.Transformers) { Type transformerType = Type.GetType(transformerConfig.TypeName); if (transformerType == null) { throw new Exception($"The type '{transformerConfig.TypeName}' was not found."); } if (!typeof(IContentTransformer).IsAssignableFrom(transformerType)) { throw new Exception($"The type '{transformerType.FullName}' is not assignable to type '{typeof(IContentTransformer).FullName}'."); } IContentTransformer transformer = (IContentTransformer)_container.Resolve(transformerType); if (transformerConfig.ContentSource == null || string.IsNullOrEmpty(transformerConfig.ContentSource.Name)) { throw new Exception($"The transformer '{transformerType.Name}' does not have any content source."); } IContentSource source = _contentSourceService.Build(transformerConfig.ContentSource.Name); source.Init(transformerConfig.ContentSource.Config); ITransformerStoreModel transformerStoreModel = _contentTransformerStorage.AddOrGetTransformer(transformer, source); _transformerProcessors.Add(transformerStoreModel.Id, new TransformerProcessor(_contentTransformerStorage, transformer, source)); } }
public ITransformerStoreModel AddOrGetTransformer(IContentTransformer transformer, IContentSource source) { ITransformerStoreModel currentTransformer = _transformers.Find(model => model.TransformerType == transformer.GetType().FullName&& model.SourceIdentity == source.Identity); if (currentTransformer != null) { return(currentTransformer); } _dbConnection.Execute($"INSERT INTO Transformers (Created, Name, TransformerType, SourceIdentity) VALUES ('{DateTime.Now:yyyy-MM-dd hh:mm:ss}', '{transformer.GetType().Assembly.GetName().Name}', '{transformer.GetType().FullName}', '{source.Identity}')"); TransformerStoreModel transformerStoreModel = (TransformerStoreModel)GetTransformer(transformer, source); Directory.CreateDirectory(Path.Combine(_storeContentDirectoryName, transformerStoreModel.Id.ToString())); _transformers.Add(transformerStoreModel); return(transformerStoreModel); }