public static void SaveAssetBitmapFile(Asset asset, string assetBitmapReference, BinaryFile file) { if (asset.IsNew || asset.IsNull) { return; } if (file.IsEmpty) { return; } if (StringUtils.IsBlank(assetBitmapReference)) { return; } var abg = new AssetBitmapInfo(asset, assetBitmapReference); if (abg.FileExists) { File.Delete(abg.FilePath); } if (!Directory.Exists(abg.FolderPath)) { Directory.CreateDirectory(abg.FolderPath); m_Logger.DebugFormat("Created folder: {0}", abg.FolderPath); } string path = Path.Combine(abg.FolderPath, abg.Reference + "_" + abg.Asset.AssetId + "." + file.FileExtension); file.SaveAs(path); }
public static void SaveHomepageImage(Homepage homepage, int number, BinaryFile file, Size maxDimensions) { if (homepage.IsNew) { throw new SystemException("Homepage must be saved before image can be saved"); } if (!file.IsEmpty) { // The ID of the homepage being saved int homepageId = homepage.HomepageId.GetValueOrDefault(); // Delete the old files foreach (string f in GetHomepageImageFiles(homepageId, number)) { File.Delete(f); } // Construct path to the new homepage image string prefix = GetHomepageImagePrefix(homepageId, number); string filename = string.Format("{0}.{1}", prefix, file.FileExtension); string path = Path.Combine(HomepageImagesFolder, filename); // Save the file file.SaveAs(path); // Resize the file if required if (!maxDimensions.IsEmpty) { ImageUtils.ResizeImageDown(path, maxDimensions.Height, maxDimensions.Width); } } }
/// <summary> /// Saves the posted file to disk and adds its mimetype /// to the mimetype manager if it does not exist already /// </summary> private static void SavePostedFile(BinaryFile file, string path) { file.SaveAs(path); m_Logger.DebugFormat("File '{0}' saved to '{1}'", file.FileName, path); MimeTypeManager.AddMimeType(file.FileExtension, file.ContentType); }
public static void SaveAssetFile(Asset asset, BinaryFile file, bool notify, bool checkHash, bool doNotProcessForPreview) { // Quit if empty asset if (asset == null || asset.IsNull || asset.IsNew) { return; } // Quit if no file was uploaded if (file == null || file.IsEmpty) { return; } if (checkHash) { // Check the hash. Don't allow a processed asset to be re-uploaded AssetFinder finder = new AssetFinder { FileHash = file.FileHash, IsProcessed = true }; Asset assetFromHash = Asset.FindOne(finder); if (!assetFromHash.IsNull) { throw new InvalidAssetFileException(string.Format("{0} is already uploaded and has the Asset ID: {1}", file.FileName, assetFromHash.AssetId)); } } m_Logger.DebugFormat("SaveAssetFile - AssetId: {0}", asset.AssetId); // Ensure asset has path if (asset.AssetFilePath.IsNull) { asset.AssetFilePathId = AssetFilePathManager.GetDefault().AssetFilePathId.GetValueOrDefault(); } // Delete old files foreach (string path in Directory.GetFiles(asset.AssetFilePath.Path, asset.FileReference + ".*", SearchOption.AllDirectories)) { File.Delete(path); } // Delete any cached files string cacheFolder = Path.Combine(Settings.CachedAssetFilesFolder, asset.AssetId.ToString()); if (Directory.Exists(cacheFolder)) { Directory.Delete(cacheFolder, true); m_Logger.DebugFormat("Deleted cache folder: {0}", cacheFolder); } // Update the asset file path asset.AssetFilePathId = AssetFilePathManager.GetDefault().AssetFilePathId.GetValueOrDefault(); // Update asset file information asset.Filename = file.FileName; asset.FileSize = file.FileSize; asset.FileHash = file.FileHash; // The file extension comes from the view but set this for now // in case we need it elsewhere in the code before the Asset entity // is refreshed from the database. asset.FileExtension = file.FileExtension; // If not processing for Preview the asset.IsProcessed flag should be set // depending on the file extension. if (!doNotProcessForPreview) { asset.IsProcessed = (!APSGateway.Instance.CanProcess(file.FileExtension)); } // Save asset to database Asset.Update(asset); Asset.SaveAssetMetadata(asset); // Construct location to save asset file string assetFolder = GetFolder(asset.AssetFilePath, AssetFileType.AssetFile); string assetFilePath = Path.Combine(assetFolder, asset.FileReference + "." + file.FileExtension); // Save the file. Don't bother doing a mimetype check here, as the // files will be forced to open the download box by using 'application/octet-stream' // as the content type, so the server doesn't need to set this value. file.SaveAs(assetFilePath); m_Logger.DebugFormat("Saved file: {0}", assetFilePath); // Send this to the gateway for processing unless the do not process flag is set. // When the preview and thumbnail are ready, the APS will callback to the web app to save them. if (!doNotProcessForPreview && !asset.IsProcessed) { APSGateway.Instance.ProcessFile(asset, notify, FileOutputs.All); } // Regenerate bitmaps. This submits the assets to the APS and returns. // When the files are ready, the APS will callback to the web app to save them. AssetBitmapGroupManager.Generate(asset); // Check if file indexing is enabled, and if so, read the file contents into a byte // array and then store this in the database so that we can search it using full-text // indexing. As we're using an indexed view, this is done using a subquery. // See AssetFinder.cs for full details of how this is implemented. if (AssetFinder.FileIndexingEnabled && m_IndexableExtensions.Contains(file.FileExtension)) { ByteArray byteArray = ByteArray.New(file.InputStream); Asset.AddUpdateFileContents(asset.AssetId.GetValueOrDefault(), file.FileName, AssetFileType.AssetFile, byteArray, false); } // Now create the zip file if required if (ZipAssetFiles) { AssetFileZipper.CreateZip(asset); } }
public void Upload() { ValidateRequiredValues(); m_Logger.DebugFormat("Uploading file: {0}", BinaryFile.FileName); if (BinaryFile.FileExtension == "zip" && !PreserveZipFile) { m_Logger.Debug("File is zip file"); // If we're using a zip file, we need to go through the files inside // the zip and check them. Files that are not valid are not unzipped, // we just skip over them as there's no point in unzipping files we don't need. using (ZipFile zipFile = new ZipFile(BinaryFile.InputStream)) { int fileCount = 0; foreach (ZipEntry zipEntry in zipFile) { // Ignore non-files completely if (!zipEntry.IsFile) { m_Logger.DebugFormat(" - {0} skipped, not a file", zipEntry.Name); continue; } string filename = (Path.GetFileName(zipEntry.Name) ?? string.Empty).ToLower(); // Ignore blank filenames completely if (StringUtils.IsBlank(filename)) { m_Logger.Debug(" - skipped empty filename"); continue; } m_Logger.DebugFormat(" - {0} being processed", zipEntry.Name); fileCount++; // Initialise the uploaded asset result UploadedAssetResult uar = new UploadedAssetResult { Filename = filename, ZipFileIndex = zipEntry.ZipFileIndex }; Asset asset; if (IsDuplicateFile(zipFile, zipEntry, out asset)) { uar.FileStatus = FileStatus.DuplicateHash; uar.Asset = asset; if (UploadedBy.UserRole == UserRole.SuperAdministrator) { uar.SaveFile = true; m_Logger.Debug(" -- File is duplicate hash, but uploader is superadmin so save anyway"); } else { uar.SaveFile = false; m_Logger.Debug(" -- File is ignored, duplicate hash"); } } else if (IgnorePath(zipEntry.Name)) { uar.SaveFile = false; uar.FileStatus = FileStatus.IgnoredPath; m_Logger.Debug(" -- Path is ignored"); } else if (IgnoreFile(filename)) { uar.SaveFile = false; uar.FileStatus = FileStatus.IgnoredFile; m_Logger.Debug(" -- File is ignored"); } else { uar.SaveFile = true; uar.FileStatus = FileStatus.Processed; m_Logger.Debug(" -- File is valid"); } UploadedAssetResultList.Add(uar); } // Ensure that the zip file has at least one file // No point in continuing otherwise. if (fileCount == 0) { m_Logger.Debug("- Zip file does not contain any assets and cannot be processed"); throw new AssetUploadException("Zip file does not contain any assets"); } foreach (ZipEntry zipEntry in zipFile) { // Ignore non-files completely if (!zipEntry.IsFile) { continue; } // Get the filename string filename = (Path.GetFileName(zipEntry.Name) ?? string.Empty); // Ignore blank filenames completely if (StringUtils.IsBlank(filename)) { continue; } // Parsed filename string parsedFilename = (Path.GetFileName(zipEntry.Name) ?? string.Empty).ToLower(); // Only process the file if SaveFile is true (ie. it meets the save requirements) bool process = (UploadedAssetResultList.FindAll(o => o.Filename.ToLower() == parsedFilename && o.SaveFile).Count > 0); if (process) { OnProgress("Unzipping file: " + parsedFilename); // Construct the filepath where the file will be unzipped to string filePath = GetUniqueFilepath(Path.Combine(TempFolder, filename)); // Unzip the file using (Stream inputStream = zipFile.GetInputStream(zipEntry)) { using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) { CopyStream(inputStream, fileStream); fileStream.Close(); } } if (CreateCategorySubFolders) { // Store the temporary filename along with the original path m_TempFiles.Add(filePath, new OriginalLocation { OriginalPath = Path.GetDirectoryName(zipEntry.Name), ZipFileIndex = zipEntry.ZipFileIndex }); } else { m_TempFiles.Add(filePath, new OriginalLocation { OriginalPath = String.Empty, ZipFileIndex = zipEntry.ZipFileIndex }); } OnProgress("Unzipped file: " + parsedFilename); } } } } else { m_Logger.DebugFormat("Processing single file upload: {0}", BinaryFile.FileName); // Single file uploads. These are managed a bit differently from zip uploads. // They are validated and if anything fails, we throw an AssetUpload exception. // Ensure that the file isn't one that should be ignored if (IgnoreFile(BinaryFile.FileName)) { m_Logger.DebugFormat("File {0} is not a valid asset file", BinaryFile.FileName); throw new AssetUploadException(string.Format("The file '{0}' is not a valid asset file", BinaryFile.FileName)); } // Search for assets with the same file hash AssetFinder finder = new AssetFinder { FileHash = BinaryFile.FileHash, IsProcessed = true }; finder.SortExpressions.Add(new DescendingSort("AssetId")); Asset duplicateAsset = Asset.FindOne(finder); // If a duplicate asset was found and the uploading user is not a superadmin, halt the upload and don't save the file to disk // Include the duplicate asset ID in the error message for information purposes. // We allow duplicate uploads from super-admins as we assume they know wha they are doing. if (!duplicateAsset.IsNull) { // Log this duplicate m_Logger.DebugFormat("The file {0} has already been uploaded to the system and has the Asset ID: {1}", BinaryFile.FileName, duplicateAsset.AssetId); // Halt the upload if the user doing the upload is not a super-admin if (UploadedBy.UserRole != UserRole.SuperAdministrator) { string message = string.Format("This file has already been uploaded to the system and has the ID: {0}", duplicateAsset.AssetId); throw new DuplicateHashException(message, BinaryFile, duplicateAsset); } } // Get the filename where this file should be saved to on disk string filename = Path.Combine(TempFolder, BinaryFile.FileName); // Save the file OnProgress("Saving asset file to disk"); BinaryFile.SaveAs(filename); m_TempFiles.Add(filename, new OriginalLocation { OriginalPath = SourcePath, ZipFileIndex = -1 }); OnProgress("Saved asset file to disk"); // Set the filestatus based on whether the asset is a duplicate FileStatus fileStatus = (!duplicateAsset.IsNull) ? FileStatus.DuplicateHash : FileStatus.Processed; // Add it to the resut list UploadedAssetResult uar = new UploadedAssetResult { Filename = BinaryFile.FileName, FileStatus = fileStatus, SaveFile = true, ZipFileIndex = -1 }; UploadedAssetResultList.Add(uar); } // Get all of the uploaded files and upload these to the system // foreach (string path in Directory.GetFiles(TempFolder)) foreach (KeyValuePair <string, OriginalLocation> kvp in m_TempFiles) { // Save the asset file to the system OnProgress("Saving asset"); OriginalLocation origLocation = (OriginalLocation)kvp.Value; // Asset asset = SaveUploadedAsset(new BinaryFile(path, BinaryFile.SaveMode.Move)); Asset asset = SaveUploadedAsset(new BinaryFile(kvp.Key, BinaryFile.SaveMode.Move), origLocation.OriginalPath); OnProgress("Saved asset with ID: " + asset.AssetId); // Update audit logs LogUploadedAsset(asset, string.Format("Uploaded from: {0}", BinaryFile.FileName)); // Update the uploaded asset result list item with the asset record that was created UploadedAssetResult uar; if (origLocation.ZipFileIndex != -1) { // Original was from a zip file so use the zip file index uar = UploadedAssetResultList.Find(o => o.ZipFileIndex == origLocation.ZipFileIndex && o.SaveFile); } else { // Coming from a folder heirarchy so use filename. uar = UploadedAssetResultList.Find(o => o.Filename.ToLower() == asset.Filename.ToLower() && o.SaveFile); } uar.Asset = asset; } try { OnProgress("Deleting temp folder"); Directory.Delete(TempFolder, true); OnProgress("Deleted temp folder"); m_Logger.DebugFormat("- Deleted directory: {0}", TempFolder); } catch (Exception e) { m_Logger.Warn("Error deleting: " + TempFolder, e); } }