public void StageData(ArrayList files, FolderInfo folderInfo)
 {
     FileController fc = new FileController();
     if (folderInfo != null && files != null)
     {
         foreach (DotNetNuke.Services.FileSystem.FileInfo fileInfo in files)
         {
             if (File.Exists(fileInfo.PhysicalPath))
             {
                 File.Move(fileInfo.PhysicalPath, folderInfo.PhysicalPath + fileInfo.FileName);
                 fc.UpdateFile(fileInfo.FileId, fileInfo.FileName, fileInfo.Extension.Substring(1), fileInfo.Size, Null.NullInteger, Null.NullInteger, fileInfo.ContentType, folderInfo.PhysicalPath, folderInfo.FolderID);
             }
         }
     }
 }
		protected void Save_OnClick(object sender, EventArgs e)
		{
			try
			{
				if (FolderList.Items.Count == 0)
				{
					return;
				}

				DotNetNuke.Entities.Portals.PortalSettings portalSettings = DotNetNuke.Entities.Portals.PortalSettings.Current;

				string fileContents = htmlText2.Text.Trim();
				string newFileName = FileName.Text;
				if (! (newFileName.EndsWith(".html")))
				{
					newFileName = newFileName + ".html";
				}

				string rootFolder = portalSettings.HomeDirectoryMapPath;
				string dbFolderPath = FolderList.SelectedValue;
				string virtualFolder = (string)(string)FileSystemValidation.ToVirtualPath(dbFolderPath);
				rootFolder = rootFolder + FolderList.SelectedValue;
				rootFolder = rootFolder.Replace("/", "\\");

				string errorMessage = string.Empty;
				FolderController folderCtrl = new FolderController();
				FolderInfo folder = folderCtrl.GetFolder(portalSettings.PortalId, dbFolderPath, false);

				if ((folder == null))
				{
					ShowSaveTemplateMessage(GetString("msgFolderDoesNotExist.Text"));
					return;
				}

				// Check file name is valid
				FileSystemValidation dnnValidator = new FileSystemValidation();
				errorMessage = dnnValidator.OnCreateFile(virtualFolder + newFileName, fileContents.Length);
				if (! (string.IsNullOrEmpty(errorMessage)))
				{
					ShowSaveTemplateMessage(errorMessage);
					return;
				}

				FileController fileCtrl = new FileController();
				DotNetNuke.Services.FileSystem.FileInfo existingFile = fileCtrl.GetFile(newFileName, portalSettings.PortalId, folder.FolderID);

				// error if file exists
				if (! Overwrite.Checked && existingFile != null)
				{
					ShowSaveTemplateMessage(GetString("msgFileExists.Text"));
					return;
				}

				FileInfo newFile = existingFile;
				if ((newFile == null))
				{
					newFile = new FileInfo();
				}

				newFile.FileName = newFileName;
				newFile.ContentType = "text/plain";
				newFile.Extension = "html";
				newFile.Size = fileContents.Length;
				newFile.FolderId = folder.FolderID;

				errorMessage = FileSystemUtils.CreateFileFromString(rootFolder, newFile.FileName, fileContents, newFile.ContentType, string.Empty, false);

				if (! (string.IsNullOrEmpty(errorMessage)))
				{
					ShowSaveTemplateMessage(errorMessage);
					return;
				}

				existingFile = fileCtrl.GetFile(newFileName, portalSettings.PortalId, folder.FolderID);
				if (newFile.FileId != existingFile.FileId)
				{
					newFile.FileId = existingFile.FileId;
				}

				if (newFile.FileId != Null.NullInteger)
				{
					fileCtrl.UpdateFile(newFile.FileId, newFile.FileName, newFile.Extension, newFile.Size, newFile.Width, newFile.Height, newFile.ContentType, folder.FolderPath, folder.FolderID);
				}
				else
				{
					fileCtrl.AddFile(portalSettings.PortalId, newFile.FileName, newFile.Extension, newFile.Size, newFile.Width, newFile.Height, newFile.ContentType, folder.FolderPath, folder.FolderID, true);
				}

				ShowSaveTemplateMessage(string.Empty);
			}
			catch (Exception ex)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
				throw ex;
			}
		}
        /// <summary>
        /// Updates a File
        /// </summary>
        /// <param name="strSourceFile">The original File Name</param>
        /// <param name="strDestFile">The new File Name</param>
        /// <param name="PortalId"></param>
        /// <param name="isCopy">Flag determines whether file is to be be moved or copied</param>
        /// <param name="ClearCache"></param>
        private static string UpdateFile( string strSourceFile, string strDestFile, int PortalId, bool isCopy, bool isNew, bool ClearCache )
        {
            string retValue = "";
            retValue += CheckValidFileName( strSourceFile ) + " ";
            retValue += CheckValidFileName( strDestFile );
            if( retValue.Length > 1 )
            {
                return retValue;
            }
            retValue = "";

            try
            {
                FolderController objFolderController = new FolderController();
                FileController objFileController = new FileController();
                string sourceFolderName = Globals.GetSubFolderPath( strSourceFile );
                string sourceFileName = GetFileName( strSourceFile );
                FolderInfo sourceFolder = objFolderController.GetFolder( PortalId, sourceFolderName );

                string destFileName = GetFileName( strDestFile );
                string destFolderName = Globals.GetSubFolderPath( strDestFile );

                if( sourceFolder != null )
                {
                    Services.FileSystem.FileInfo file = objFileController.GetFile( sourceFileName, PortalId, sourceFolder.FolderID );
                    if( file != null )
                    {
                        //Get the source Content from wherever it is
                        Stream sourceStream = GetFileStream( file );

                        if( isCopy )
                        {
                            //Add the new File
                            AddFile( PortalId, sourceStream, strDestFile, "", file.Size, destFolderName, true, ClearCache );
                        }
                        else
                        {
                            //Move/Update existing file
                            FolderInfo destinationFolder = objFolderController.GetFolder( PortalId, destFolderName );

                            //Now move the file
                            if( destinationFolder != null )
                            {
                                objFileController.UpdateFile( file.FileId, destFileName, file.Extension, file.Size, file.Width, file.Height, file.ContentType, destFolderName, destinationFolder.FolderID );

                                //Write the content to the Destination
                                WriteStream( file.FileId, sourceStream, strDestFile, destinationFolder.StorageLocation, true );

                                //Now we need to clean up the original files
                                if( sourceFolder.StorageLocation == (int)FolderController.StorageLocationTypes.InsecureFileSystem )
                                {
                                    //try and delete the Insecure file
                                    AttemptFileDeletion( strSourceFile );
                                }
                                if( sourceFolder.StorageLocation == (int)FolderController.StorageLocationTypes.SecureFileSystem )
                                {
                                    //try and delete the Secure file
                                    AttemptFileDeletion( strSourceFile + Globals.glbProtectedExtension );
                                }
                            }
                        }
                    }
                }
            }
            catch( Exception ex )
            {
                retValue = ex.Message;
            }

            return retValue;
        }
Example #4
0
        private string ExportModule( int ModuleID, string FileName )
        {
            string strMessage = "";

            ModuleController objModules = new ModuleController();
            ModuleInfo objModule = objModules.GetModule( ModuleID, TabId, false );
            if (objModule != null)
            {
                if (objModule.BusinessControllerClass != "" & objModule.IsPortable)
                {
                    try
                    {
                        object objObject = Reflection.CreateObject(objModule.BusinessControllerClass, objModule.BusinessControllerClass);

                        //Double-check
                        if (objObject is IPortable)
                        {

                            string Content = Convert.ToString(((IPortable)objObject).ExportModule(ModuleID));

                            if (Content != "")
                            {
                                // add attributes to XML document
                                Content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<content type=\"" + CleanName(objModule.FriendlyName) + "\" version=\"" + objModule.Version + "\">" + Content + "</content>";

                                //First check the Portal limits will not be exceeded (this is approximate)
                                PortalController objPortalController = new PortalController();
                                string strFile = PortalSettings.HomeDirectoryMapPath + FileName;
                                if (objPortalController.HasSpaceAvailable(PortalId, Content.Length))
                                {
                                    // save the file
                                    StreamWriter objStream = File.CreateText(strFile);
                                    objStream.WriteLine(Content);
                                    objStream.Close();

                                    // add file to Files table
                                    FileController objFiles = new FileController();
                                    FileInfo finfo = new FileInfo(strFile);
                                    FolderController objFolders = new FolderController();
                                    FolderInfo objFolder = objFolders.GetFolder(PortalId, "");
                                    Services.FileSystem.FileInfo objFile = objFiles.GetFile(lblFile.Text, PortalId, objFolder.FolderID);
                                    if (objFile == null)
                                    {
                                        objFiles.AddFile(PortalId, lblFile.Text, "xml", finfo.Length, 0, 0, "application/octet-stream", "", objFolder.FolderID, true);
                                    }
                                    else
                                    {
                                        objFiles.UpdateFile(objFile.FileId, objFile.FileName, "xml", finfo.Length, 0, 0, "application/octet-stream", "", objFolder.FolderID);
                                    }
                                }
                                else
                                {
                                    strMessage += "<br>" + string.Format(Localization.GetString("DiskSpaceExceeded"), strFile);
                                }
                            }
                            else
                            {
                                strMessage = Localization.GetString("NoContent", this.LocalResourceFile);
                            }
                        }
                        else
                        {
                            strMessage = Localization.GetString("ExportNotSupported", this.LocalResourceFile);
                        }
                    }
                    catch
                    {
                        strMessage = Localization.GetString("Error", this.LocalResourceFile);
                    }
                }
                else
                {
                    strMessage = Localization.GetString("ExportNotSupported", this.LocalResourceFile);
                }
            }

            return strMessage;
        }
        private static string UpdateFileData( int fileID, int folderID, int PortalId, string fileName, string extension, string contentType, long length, string folderName )
        {
            string retvalue = "";
            try
            {
                FileController objFileController = new FileController();
                Image imgImage = null;
                int imageWidth = 0;
                int imageHeight = 0;
                
                // HACK : VB version of this line used InStr() and converted the outputed
                // int to a boolean to check if the file extention was an image type,
                // which does not work in C# hence the line as it is now.
                if((Globals.glbImageFileTypes.IndexOf(extension.ToLower())+1) > 0)
                {
                    try
                    {
                        Services.FileSystem.FileInfo objFile = objFileController.GetFileById( fileID, PortalId );
                        Stream imageStream = GetFileStream( objFile );
                        imgImage = Image.FromStream( imageStream );
                        imageHeight = imgImage.Height;
                        imageWidth = imgImage.Width;
                        imgImage.Dispose();
                        imageStream.Close();
                    }
                    catch
                    {
                        // error loading image file
                        contentType = "application/octet-stream";
                    }
                    finally
                    {
                        //Update the File info
                        objFileController.UpdateFile( fileID, fileName, extension, length, imageWidth, imageHeight, contentType, folderName, folderID );
                    }
                }
            }
            catch( Exception ex )
            {
                retvalue = ex.Message;
            }

            return retvalue;
        }