/// <summary> /// Determines if the image already exists. /// </summary> /// <param name="imageId">The image identifier.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool DoesImageFileExist(long imageId) { var imageFile = Entity.Get <ImageFileType>(imageId); if (imageFile == null || string.IsNullOrWhiteSpace(imageFile.FileDataHash)) { return(false); } return(FileRepositoryHelper.DoesFileExist(Factory.BinaryFileRepository, imageFile.FileDataHash)); }
public bool OnBeforeSave(IEnumerable <IEntity> entities, IDictionary <string, object> state) { foreach (FileType fileType in from entity in entities where entity.Is <FileType>() select entity.AsWritable <FileType>()) { string dataHash = fileType.FileDataHash; if (string.IsNullOrWhiteSpace(dataHash)) { continue; } bool isImage = fileType.Is <ImageFileType>(); bool isDocument = fileType.Is <Document>() || fileType.Is <DocumentRevision>(); // Check to see if we have a relationship to a document type and if so then perform the migration into the // indexed table otherwise insert the data into the non-indexed table. IFileRepository fileRepository = isDocument ? Factory.DocumentFileRepository : Factory.BinaryFileRepository; if (FileRepositoryHelper.DoesFileExist(fileRepository, dataHash)) { // File aleady exists continue; } if (!FileRepositoryHelper.DoesFileExist(Factory.TemporaryFileRepository, dataHash)) { // File was not found continue; } StreamProperties streamProperties; string token; try { using (var stream = Factory.TemporaryFileRepository.Get(dataHash)) { // Copy the file to its correct store streamProperties = GetPropertiesFromStream(stream, isImage); stream.Position = 0; token = fileRepository.Put(stream); } } catch (FileNotFoundException) { // File was not found continue; } Factory.TemporaryFileRepository.Delete(dataHash); if (isImage) { // Set image properties fileType.SetField(ImageFileType.ImageHeight_Field, streamProperties.ImageHeight); fileType.SetField(ImageFileType.ImageWidth_Field, streamProperties.ImageWidth); } else { if (isDocument) { Document document = fileType.As <Document>(); // Verify that we have the correct çurrent revision', as the current revision should have been // given the same temporary dataHash as the document. (This is done in DocumentEventTarget) if (document?.CurrentDocumentRevision?.FileDataHash == dataHash) { document.CurrentDocumentRevision.FileDataHash = token; } } } // Apply the new file hash. fileType.FileDataHash = token; fileType.Size = Convert.ToInt32(streamProperties.FileSize); } return(false); }