public static IEnumerator LoadTestDescriptor(string fileName, EventHandler <EventArgs> OnFinished, EventHandler <MainMenuController.ExceptionEventArgs> OnError) { byte[] zipBytes = null; string filePath = Application.streamingAssetsPath + "/" + fileName; WWW www = new WWW(filePath); yield return(www); zipBytes = www.bytes; try { Dictionary <string, MemoryStream> descFiles = ZipUtility.ExtractZipFile(zipBytes); List <string> xmlNames = GetXmlNames(descFiles); Dictionary <string, MemoryStream> imgResources = GetImgs(descFiles); List <GameDescriptor> gDescriptors = ReadXmls(xmlNames, descFiles); new DescriptorProcessor().ProcessMultipleGameDescriptor(gDescriptors, imgResources); OnFinished(null, EventArgs.Empty); yield break; } catch (System.Exception e) { OnError(null, new MainMenuController.ExceptionEventArgs("Error occoured during descriptor loading!", e)); yield break; } }
public static void LoadDescriptor(string filePath, EventHandler <EventArgs> OnFinished, EventHandler <MainMenuController.ExceptionEventArgs> OnError) { byte[] zipBytes = null; zipBytes = File.ReadAllBytes(filePath); try { Dictionary <string, MemoryStream> descFiles = ZipUtility.ExtractZipFile(zipBytes); List <string> xmlNames = GetXmlNames(descFiles); Dictionary <string, MemoryStream> imgResources = GetImgs(descFiles); List <GameDescriptor> gDescriptors = ReadXmls(xmlNames, descFiles); new DescriptorProcessor().ProcessMultipleGameDescriptor(gDescriptors, imgResources); OnFinished(null, EventArgs.Empty); } catch (System.Exception e) { OnError(null, new MainMenuController.ExceptionEventArgs("Error occoured during descriptor loading!", e)); } }
/// <summary> /// Adds the uploaded files to the gallery. This method is called when the application is operating under full trust. In this case, /// the ComponentArt Upload control is used. The logic is nearly identical to that in AddUploadedFilesLessThanFullTrust - the only /// differences are syntax differences arising from the different file upload control. /// </summary> /// <param name="files">The files to add to the gallery.</param> private void AddUploadedFilesForFullTrust(UploadedFileInfoCollection files) { // Clear the list of hash keys so we're starting with a fresh load from the data store. try { MediaObjectHashKeys.Clear(); string albumPhysicalPath = this.GetAlbum().FullPhysicalPathOnDisk; HelperFunctions.BeginTransaction(); UploadedFileInfo[] fileInfos = new UploadedFileInfo[files.Count]; files.CopyTo(fileInfos, 0); Array.Reverse(fileInfos); foreach (UploadedFileInfo file in fileInfos) { if (String.IsNullOrEmpty(file.FileName)) { continue; } if ((System.IO.Path.GetExtension(file.FileName).Equals(".zip", StringComparison.OrdinalIgnoreCase)) && (!chkDoNotExtractZipFile.Checked)) { #region Extract the files from the zipped file. lock (file) { if (File.Exists(file.TempFileName)) { using (ZipUtility zip = new ZipUtility(Util.UserName, RoleController.GetGalleryServerRolesForUser())) { this._skippedFiles.AddRange(zip.ExtractZipFile(file.GetStream(), this.GetAlbum(), chkDiscardOriginalImage.Checked)); } } else { // When one of the files causes an OutOfMemoryException, this can cause the other files to disappear from the // temp upload directory. This seems to be an issue with the ComponentArt Upload control, since this does not // seem to happen with the ASP.NET FileUpload control. If the file doesn't exist, make a note of it and move on // to the next one. this._skippedFiles.Add(new KeyValuePair <string, string>(file.FileName, Resources.GalleryServerPro.Task_Add_Objects_Uploaded_File_Does_Not_Exist_Msg)); continue; // Skip to the next file. } } #endregion } else { #region Add the file string filename = HelperFunctions.ValidateFileName(albumPhysicalPath, file.FileName); string filepath = Path.Combine(albumPhysicalPath, filename); #if DEBUG TechInfoSystems.TracingTools.Tools.MarkSpot(string.Format(CultureInfo.CurrentCulture, "Attempting to move file {0} to {1}...", file.FileName, filepath)); #endif lock (file) { if (File.Exists(file.TempFileName)) { file.SaveAs(filepath); } else { // When one of the files causes an OutOfMemoryException, this can cause the other files to disappear from the // temp upload directory. This seems to be an issue with the ComponentArt Upload control, since this does not // seem to happen with the ASP.NET FileUpload control. If the file doesn't exist, make a note of it and move on // to the next one. this._skippedFiles.Add(new KeyValuePair <string, string>(file.FileName, Resources.GalleryServerPro.Task_Add_Objects_Uploaded_File_Does_Not_Exist_Msg)); continue; // Skip to the next file. } } try { IGalleryObject go = Factory.CreateMediaObjectInstance(filepath, this.GetAlbum()); GalleryObjectController.SaveGalleryObject(go); if ((chkDiscardOriginalImage.Checked) && (go is Business.Image)) { ((Business.Image)go).DeleteHiResImage(); GalleryObjectController.SaveGalleryObject(go); } } catch (UnsupportedMediaObjectTypeException ex) { try { File.Delete(filepath); } catch (UnauthorizedAccessException) { } // Ignore an error; the file will end up getting deleted during cleanup maintenance this._skippedFiles.Add(new KeyValuePair <string, string>(filename, ex.Message)); } #endregion } } HelperFunctions.CommitTransaction(); } catch { HelperFunctions.RollbackTransaction(); throw; } finally { // Delete the uploaded temporary files, as by this time they have been saved to the destination directory. foreach (UploadedFileInfo file in files) { try { System.IO.File.Delete(file.TempFileName); } catch (UnauthorizedAccessException) { } // Ignore an error; the file will end up getting deleted during cleanup maintenance } // Clear the list of hash keys to free up memory. MediaObjectHashKeys.Clear(); HelperFunctions.PurgeCache(); } }
/// <summary> /// Adds the uploaded files to the gallery. This method is called when the application is operating at lesss than full trust. In this case, /// the ASP.NET FileUpload control is used. The logic is nearly identical to that in AddUploadedFilesForFullTrust - the only /// differences are syntax differences arising from the different file upload control. /// </summary> private void AddUploadedFilesLessThanFullTrust() { // Clear the list of hash keys so we're starting with a fresh load from the data store. try { MediaObjectHashKeys.Clear(); string albumPhysicalPath = this.GetAlbum().FullPhysicalPathOnDisk; HelperFunctions.BeginTransaction(); for (int i = 0; i < 5; i++) { FileUpload file = (FileUpload)phUpload.FindControl("fuUpload" + i); if (!file.HasFile) { continue; } if ((System.IO.Path.GetExtension(file.FileName).Equals(".zip", StringComparison.OrdinalIgnoreCase)) && (!chkDoNotExtractZipFile.Checked)) { #region Extract the files from the zipped file. // Extract the files from the zipped file. using (ZipUtility zip = new ZipUtility(Util.UserName, RoleController.GetGalleryServerRolesForUser())) { this._skippedFiles.AddRange(zip.ExtractZipFile(file.FileContent, this.GetAlbum(), chkDiscardOriginalImage.Checked)); } #endregion } else { #region Add the file string filename = HelperFunctions.ValidateFileName(albumPhysicalPath, file.FileName); string filepath = Path.Combine(albumPhysicalPath, filename); #if DEBUG TechInfoSystems.TracingTools.Tools.MarkSpot(string.Format(CultureInfo.CurrentCulture, "Attempting to move file {0} to {1}...", file.FileName, filepath)); #endif file.SaveAs(filepath); try { IGalleryObject go = Factory.CreateMediaObjectInstance(filepath, this.GetAlbum()); GalleryObjectController.SaveGalleryObject(go); if ((chkDiscardOriginalImage.Checked) && (go is Business.Image)) { ((Business.Image)go).DeleteHiResImage(); GalleryObjectController.SaveGalleryObject(go); } } catch (UnsupportedMediaObjectTypeException ex) { try { File.Delete(filepath); } catch (UnauthorizedAccessException) { } // Ignore an error; the file will end up getting deleted during cleanup maintenance this._skippedFiles.Add(new KeyValuePair <string, string>(filename, ex.Message)); } #endregion } } HelperFunctions.CommitTransaction(); } catch { HelperFunctions.RollbackTransaction(); throw; } finally { // Clear the list of hash keys to free up memory. MediaObjectHashKeys.Clear(); HelperFunctions.PurgeCache(); } }
private static List <ActionResult> CreateMediaObjectFromFile(AddMediaObjectSettings options) { string sourceFilePath = Path.Combine(AppSetting.Instance.PhysicalApplicationPath, GlobalConstants.TempUploadDirectory, options.FileNameOnServer); try { IAlbum album = AlbumController.LoadAlbumInstance(options.AlbumId, true); if ((Path.GetExtension(options.FileName).Equals(".zip", StringComparison.OrdinalIgnoreCase)) && (options.ExtractZipFile)) { // Extract the files from the zipped file. using (ZipUtility zip = new ZipUtility(Utils.UserName, RoleController.GetGalleryServerRolesForUser())) { using (FileStream fs = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read)) { return(zip.ExtractZipFile(fs, album, options.DiscardOriginalFile)); } } } else { string albumPhysicalPath = album.FullPhysicalPathOnDisk; string filename = HelperFunctions.ValidateFileName(albumPhysicalPath, options.FileName); string filepath = Path.Combine(albumPhysicalPath, filename); MoveFile(filepath, sourceFilePath); ActionResult result = CreateMediaObject(filepath, album, options); return(new List <ActionResult>() { result }); } } catch (Exception ex) { AppErrorController.LogError(ex); return(new List <ActionResult>() { new ActionResult() { Title = options.FileName, Status = ActionResultStatus.Error, Message = "The event log may have additional details." } }); } finally { try { // If the file still exists in the temp directory, delete it. Typically this happens when we've // extracted the contents of a zip file (since other files will have already been moved to the dest album.) if (File.Exists(sourceFilePath)) { File.Delete(sourceFilePath); } } catch (IOException) { } // Ignore an error; not a big deal if it continues to exist in the temp directory catch (UnauthorizedAccessException) { } // Ignore an error; not a big deal if it continues to exist in the temp directory } }
/// <summary> /// Adds the uploaded files to the gallery. This method is called when the application is operating under full trust. In this case, /// the ComponentArt Upload control is used. The logic is nearly identical to that in AddUploadedFilesLessThanFullTrust - the only /// differences are syntax differences arising from the different file upload control. /// </summary> /// <param name="files">The files to add to the gallery.</param> private void AddUploadedFilesForFullTrust(UploadedFileInfoCollection files) { // Clear the list of hash keys so we're starting with a fresh load from the data store. try { MediaObjectHashKeys.Clear(); string albumPhysicalPath = this.GetAlbum().FullPhysicalPathOnDisk; HelperFunctions.BeginTransaction(); UploadedFileInfo[] fileInfos = new UploadedFileInfo[files.Count]; files.CopyTo(fileInfos, 0); Array.Reverse(fileInfos); foreach (UploadedFileInfo file in fileInfos) { if (String.IsNullOrEmpty(file.FileName)) { continue; } if ((System.IO.Path.GetExtension(file.FileName).Equals(".zip", StringComparison.OrdinalIgnoreCase)) && (!chkDoNotExtractZipFile.Checked)) { #region Extract the files from the zipped file. lock (file) { if (File.Exists(file.TempFileName)) { using (ZipUtility zip = new ZipUtility(Util.UserName, GetGalleryServerRolesForUser())) { this._skippedFiles.AddRange(zip.ExtractZipFile(file.GetStream(), this.GetAlbum(), chkDiscardOriginalImage.Checked)); } } else { // When one of the files causes an OutOfMemoryException, this can cause the other files to disappear from the // temp upload directory. This seems to be an issue with the ComponentArt Upload control, since this does not // seem to happen with the ASP.NET FileUpload control. If the file doesn't exist, make a note of it and move on // to the next one. this._skippedFiles.Add(new KeyValuePair <string, string>(file.FileName, Resources.GalleryServerPro.Task_Add_Objects_Uploaded_File_Does_Not_Exist_Msg)); continue; // Skip to the next file. } } // Sueetie Modified - Add contents of ZIP file - All List <SueetieMediaObject> sueetieMediaObjects = SueetieMedia.GetSueetieMediaUpdateList(this.GetAlbumId()); int i = 0; foreach (SueetieMediaObject _sueetieMediaObject in sueetieMediaObjects) { int _moid = _sueetieMediaObject.MediaObjectID; IGalleryObject mo = Factory.LoadMediaObjectInstance(_moid); SueetieContent sueetieContent = new SueetieContent { SourceID = _sueetieMediaObject.MediaObjectID, ContentTypeID = MediaHelper.ConvertContentType(mo.MimeType.TypeCategory), ApplicationID = (int)SueetieApplications.Current.ApplicationID, UserID = _sueetieMediaObject.SueetieUserID, DateTimeCreated = mo.DateAdded, IsRestricted = mo.IsPrivate, Permalink = MediaHelper.SueetieMediaObjectUrl(_moid, this.GalleryId) }; // Add Sueetie-specific data to Sueetie_gs_MediaObject _sueetieMediaObject.ContentTypeID = MediaHelper.ConvertContentType(mo.MimeType.TypeCategory); _sueetieMediaObject.AlbumID = this.GetAlbum().Id; _sueetieMediaObject.InDownloadReport = false; SueetieMedia.CreateSueetieMediaObject(_sueetieMediaObject); SueetieCommon.AddSueetieContent(sueetieContent); i++; } #endregion } else { #region Add the file string filename = HelperFunctions.ValidateFileName(albumPhysicalPath, file.FileName); string filepath = Path.Combine(albumPhysicalPath, filename); lock (file) { if (File.Exists(file.TempFileName)) { file.SaveAs(filepath); } else { // When one of the files causes an OutOfMemoryException, this can cause the other files to disappear from the // temp upload directory. This seems to be an issue with the ComponentArt Upload control, since this does not // seem to happen with the ASP.NET FileUpload control. If the file doesn't exist, make a note of it and move on // to the next one. this._skippedFiles.Add(new KeyValuePair <string, string>(file.FileName, Resources.GalleryServerPro.Task_Add_Objects_Uploaded_File_Does_Not_Exist_Msg)); continue; // Skip to the next file. } } try { IGalleryObject go = Factory.CreateMediaObjectInstance(filepath, this.GetAlbum()); GalleryObjectController.SaveGalleryObject(go); if ((chkDiscardOriginalImage.Checked) && (go is Business.Image)) { ((Business.Image)go).DeleteHiResImage(); GalleryObjectController.SaveGalleryObject(go); } // Sueetie Modified - Add mediaobject to Sueetie_Content - Single File SueetieContent sueetieContent = new SueetieContent { SourceID = go.Id, ContentTypeID = MediaHelper.ConvertContentType(go.MimeType.TypeCategory), ApplicationID = (int)SueetieApplications.Current.ApplicationID, UserID = CurrentSueetieUserID, IsRestricted = this.GetAlbum().IsPrivate, Permalink = MediaHelper.SueetieMediaObjectUrl(go.Id, this.GalleryId) }; SueetieCommon.AddSueetieContent(sueetieContent); // Add Sueetie-specific data to Sueetie_gs_MediaObject SueetieMediaObject _sueetieMediaObject = new SueetieMediaObject(); _sueetieMediaObject.MediaObjectID = go.Id; _sueetieMediaObject.ContentTypeID = MediaHelper.ConvertContentType(go.MimeType.TypeCategory); _sueetieMediaObject.AlbumID = this.GetAlbum().Id; _sueetieMediaObject.InDownloadReport = false; SueetieMedia.CreateSueetieMediaObject(_sueetieMediaObject); SueetieMediaAlbum sueetieAlbum = SueetieMedia.GetSueetieMediaAlbum(this.GetAlbum().Id); if (CurrentSueetieGallery.IsLogged) { SueetieLogs.LogUserEntry((UserLogCategoryType)sueetieAlbum.AlbumMediaCategoryID, sueetieAlbum.ContentID, CurrentSueetieUserID); } } catch (UnsupportedMediaObjectTypeException ex) { try { File.Delete(filepath); } catch (UnauthorizedAccessException) { } // Ignore an error; the file will end up getting deleted during cleanup maintenance this._skippedFiles.Add(new KeyValuePair <string, string>(filename, ex.Message)); } #endregion } } SueetieMedia.ClearMediaPhotoListCache(0); SueetieMedia.ClearSueetieMediaObjectListCache(this.CurrentSueetieGalleryID); HelperFunctions.CommitTransaction(); } catch { HelperFunctions.RollbackTransaction(); throw; } finally { // Delete the uploaded temporary files, as by this time they have been saved to the destination directory. foreach (UploadedFileInfo file in files) { try { System.IO.File.Delete(file.TempFileName); } catch (UnauthorizedAccessException) { } // Ignore an error; the file will end up getting deleted during cleanup maintenance } // Clear the list of hash keys to free up memory. MediaObjectHashKeys.Clear(); HelperFunctions.PurgeCache(); } }
/// <summary> /// Adds the uploaded files to the gallery. This method is called when the application is operating at lesss than full trust. In this case, /// the ASP.NET FileUpload control is used. The logic is nearly identical to that in AddUploadedFilesForFullTrust - the only /// differences are syntax differences arising from the different file upload control. /// </summary> private void AddUploadedFilesLessThanFullTrust() { // Clear the list of hash keys so we're starting with a fresh load from the data store. try { MediaObjectHashKeys.Clear(); string albumPhysicalPath = this.GetAlbum().FullPhysicalPathOnDisk; HelperFunctions.BeginTransaction(); for (int i = 0; i < 5; i++) { FileUpload file = (FileUpload)phUpload.FindControl("fuUpload" + i); if (!file.HasFile) { continue; } if ((System.IO.Path.GetExtension(file.FileName).Equals(".zip", StringComparison.OrdinalIgnoreCase)) && (!chkDoNotExtractZipFile.Checked)) { #region Extract the files from the zipped file. // Extract the files from the zipped file. using (ZipUtility zip = new ZipUtility(Util.UserName, GetGalleryServerRolesForUser())) { this._skippedFiles.AddRange(zip.ExtractZipFile(file.FileContent, this.GetAlbum(), chkDiscardOriginalImage.Checked)); } #endregion } else { #region Add the file string filename = HelperFunctions.ValidateFileName(albumPhysicalPath, file.FileName); string filepath = Path.Combine(albumPhysicalPath, filename); file.SaveAs(filepath); try { IGalleryObject go = Factory.CreateMediaObjectInstance(filepath, this.GetAlbum()); // Sueetie Modified - Fix Blank Title on individual uploads if (go.Title.Trim().Length == 0) { go.Title = go.Original.FileName; } GalleryObjectController.SaveGalleryObject(go); if ((chkDiscardOriginalImage.Checked) && (go is Business.Image)) { ((Business.Image)go).DeleteHiResImage(); GalleryObjectController.SaveGalleryObject(go); } // Sueetie Modified - Add mediaobject to Sueetie_Content - Single File SueetieContent sueetieContent = new SueetieContent { SourceID = go.Id, ContentTypeID = MediaHelper.ConvertContentType(go.MimeType.TypeCategory), ApplicationID = (int)SueetieApplications.Current.ApplicationID, UserID = CurrentSueetieUserID, IsRestricted = this.GetAlbum().IsPrivate, Permalink = MediaHelper.SueetieMediaObjectUrl(go.Id, this.GalleryId) }; SueetieCommon.AddSueetieContent(sueetieContent); // Add Sueetie-specific data to Sueetie_gs_MediaObject SueetieMediaObject _sueetieMediaObject = new SueetieMediaObject(); _sueetieMediaObject.MediaObjectID = go.Id; _sueetieMediaObject.ContentTypeID = MediaHelper.ConvertContentType(go.MimeType.TypeCategory); _sueetieMediaObject.AlbumID = this.GetAlbum().Id; _sueetieMediaObject.InDownloadReport = false; SueetieMedia.CreateSueetieMediaObject(_sueetieMediaObject); SueetieMediaAlbum sueetieAlbum = SueetieMedia.GetSueetieMediaAlbum(this.GetAlbum().Id); if (CurrentSueetieGallery.IsLogged) { SueetieLogs.LogUserEntry((UserLogCategoryType)sueetieAlbum.AlbumMediaCategoryID, sueetieAlbum.ContentID, CurrentSueetieUserID); } SueetieMedia.ClearMediaPhotoListCache(0); SueetieMedia.ClearSueetieMediaObjectListCache(this.CurrentSueetieGalleryID); } catch (UnsupportedMediaObjectTypeException ex) { try { File.Delete(filepath); } catch (UnauthorizedAccessException) { } // Ignore an error; the file will end up getting deleted during cleanup maintenance this._skippedFiles.Add(new KeyValuePair <string, string>(filename, ex.Message)); } #endregion } } HelperFunctions.CommitTransaction(); } catch { HelperFunctions.RollbackTransaction(); throw; } finally { // Clear the list of hash keys to free up memory. MediaObjectHashKeys.Clear(); HelperFunctions.PurgeCache(); } }
/// <summary> /// Creates the media object from the file specified in <paramref name="options" />. /// </summary> /// <param name="options">The options.</param> /// <returns>List{ActionResult}.</returns> /// <exception cref="Events.CustomExceptions.GallerySecurityException">Thrown when user is not authorized to add a media object to the album.</exception> /// <remarks>This function can be invoked from a thread that does not have access to the current HTTP context (for example, when /// uploading ZIP files). Therefore, be sure nothing in this body (or the functions it calls) uses HttpContext.Current, or at /// least check it for null first.</remarks> private static List <ActionResult> CreateMediaObjectFromFile(AddMediaObjectSettings options) { string sourceFilePath = Path.Combine(AppSetting.Instance.PhysicalApplicationPath, GlobalConstants.TempUploadDirectory, options.FileNameOnServer); try { IAlbum album = AlbumController.LoadAlbumInstance(options.AlbumId, true, true); if (HttpContext.Current != null) { SecurityManager.ThrowIfUserNotAuthorized(SecurityActions.AddMediaObject, RoleController.GetGalleryServerRolesForUser(), album.Id, album.GalleryId, Utils.IsAuthenticated, album.IsPrivate, album.IsVirtualAlbum); } else { // We are extracting files from a zip archive (we know this because this is the only scenario that happens on a background // thread where HttpContext.Current is null). Tweak the security check slightly to ensure the HTTP context isn't used. // The changes are still secure because options.CurrentUserName is assigned in the server's API method. SecurityManager.ThrowIfUserNotAuthorized(SecurityActions.AddMediaObject, RoleController.GetGalleryServerRolesForUser(options.CurrentUserName), album.Id, album.GalleryId, !String.IsNullOrWhiteSpace(options.CurrentUserName), album.IsPrivate, album.IsVirtualAlbum); } var extension = Path.GetExtension(options.FileName); if (extension != null && ((extension.Equals(".zip", StringComparison.OrdinalIgnoreCase)) && (options.ExtractZipFile))) { List <ActionResult> result; // Extract the files from the zipped file. using (var zip = new ZipUtility(options.CurrentUserName, RoleController.GetGalleryServerRolesForUser(options.CurrentUserName))) { using (var fs = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read)) { result = zip.ExtractZipFile(fs, album, options.DiscardOriginalFile); } } album.SortAsync(true, options.CurrentUserName, true); return(result); } else { string albumPhysicalPath = album.FullPhysicalPathOnDisk; string filename = HelperFunctions.ValidateFileName(albumPhysicalPath, options.FileName); string filepath = Path.Combine(albumPhysicalPath, filename); MoveFile(filepath, sourceFilePath); ActionResult result = CreateMediaObject(filepath, album, options); album.Sort(true, options.CurrentUserName); return(new List <ActionResult> { result }); } } catch (Exception ex) { AppEventController.LogError(ex); return(new List <ActionResult> { new ActionResult { Title = options.FileName, Status = ActionResultStatus.Error.ToString(), Message = "The event log may have additional details." } }); } finally { try { // If the file still exists in the temp directory, delete it. Typically this happens when we've // extracted the contents of a zip file (since other files will have already been moved to the dest album.) if (File.Exists(sourceFilePath)) { File.Delete(sourceFilePath); } } catch (IOException) { } // Ignore an error; not a big deal if it continues to exist in the temp directory catch (UnauthorizedAccessException) { } // Ignore an error; not a big deal if it continues to exist in the temp directory } }