コード例 #1
0
        /// <summary>
        /// Delete asset and update audit log
        /// </summary>
        /// <param name="user">The user deleting the asset</param>
        /// <param name="assetId">The ID of the asset to be deleted</param>
        public static void DeleteAsset(User user, int assetId)
        {
            m_Logger.DebugFormat("DeleteAsset: {0}", assetId);

            Asset asset = Asset.Get(assetId);

            if (asset.IsNull)
            {
                return;
            }

            asset.IsDeleted = true;
            Asset.Update(asset);
            m_Logger.Debug(" -Flagged asset as deleted");

            // Update the audit log
            AuditLogManager.LogAssetAction(asset, user, AuditAssetAction.DeleteAsset);
            AuditLogManager.LogUserAction(user, AuditUserAction.DeleteAsset, string.Format("Deleted Asset with AssetId: {0}", asset.AssetId));
            m_Logger.Debug(" -Updated audit log");

            // Delete asset files
            DeleteAssetFiles(asset, asset.AssetFilePath.Path);

            // Delete asset bitmaps folder
            AssetBitmapGroupManager.DeleteAssetBitmapsFolder(asset);

            // Delete all asset categories
            AssetCategory.DeleteAllByAssetId(assetId);
        }
コード例 #2
0
        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);
            }
        }