Exemplo n.º 1
0
        public static List <DocumentFile> GetFilesForTutorial(int tutorialId)
        {
            DocumentFileDSTableAdapters.FileInfoTableAdapter ta =
                new DocumentFileDSTableAdapters.FileInfoTableAdapter();

            DocumentFileDS.FileInfoDataTable table = ta.GetFilesByTutorial(tutorialId);

            List <DocumentFile> fileList = new List <DocumentFile>();

            if (table.Count > 0)
            {
                foreach (DocumentFileDS.FileInfoRow row in table)
                {
                    fileList.Add(DocumentFile.CreateNewTypedDocumentFileObject(
                                     row.fileID,
                                     row.uploadedOn,
                                     row.fileSize,
                                     row.fileName,
                                     row.extension,
                                     row.title,
                                     row.fileStoragePath));
                }
            }

            return(fileList);
        }
Exemplo n.º 2
0
        public List <DocumentFile> GetDocumentFilesByObject(int objectID, string objectTypeID)
        {
            if (objectID <= 0)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(objectTypeID))
            {
                return(null);
            }
            DocumentFileDSTableAdapters.FileInfoTableAdapter ta =
                new DocumentFileDSTableAdapters.FileInfoTableAdapter();

            DocumentFileDS.FileInfoDataTable table = ta.GetDocumentFilesByObject(objectID, objectTypeID);

            List <DocumentFile> fileList = new List <DocumentFile>();

            if (table.Count > 0)
            {
                foreach (DocumentFileDS.FileInfoRow row in table)
                {
                    fileList.Add(DocumentFile.CreateNewTypedDocumentFileObject(
                                     row.fileID,
                                     row.uploadedOn,
                                     row.fileSize,
                                     row.fileName,
                                     row.extension,
                                     row.title,
                                     row.fileStoragePath));
                }
            }

            return(fileList);
        }
Exemplo n.º 3
0
        public static List <DocumentFile> GetFilesByStorageFolder(string folderName)
        {
            if (string.IsNullOrEmpty(folderName))
            {
                return(null);
            }

            folderName = folderName + "%";

            DocumentFileDSTableAdapters.FileInfoTableAdapter ta =
                new DocumentFileDSTableAdapters.FileInfoTableAdapter();

            DocumentFileDS.FileInfoDataTable table = ta.GetFilesByStoragePathName(folderName);

            List <DocumentFile> fileList = new List <DocumentFile>();

            if (table.Count > 0)
            {
                foreach (DocumentFileDS.FileInfoRow row in table)
                {
                    fileList.Add(DocumentFile.CreateNewTypedDocumentFileObject(
                                     row.fileID,
                                     row.uploadedOn,
                                     row.fileSize,
                                     row.fileName,
                                     row.extension,
                                     row.title,
                                     row.fileStoragePath));
                }
            }

            return(fileList);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get a specific document file form the database
        /// </summary>
        /// <param name="documentID">The ID of the document file</param>
        /// <returns>A data file object with the file data</returns>
        public static DocumentFile GetDocumentFile(int documentFileID)
        {
            if (documentFileID <= 0)
            {
                throw new ArgumentException("Invalid document file ID. Must be > 0");
            }

            DocumentFileDSTableAdapters.FileInfoTableAdapter theAdapter =
                new DocumentFileDSTableAdapters.FileInfoTableAdapter();

            DocumentFileDS.FileInfoDataTable theTable =
                theAdapter.GetBasicFileData(documentFileID);

            DocumentFile theResult = null;

            if (theTable.Count > 0)
            {
                theResult = DocumentFile.CreateNewTypedDocumentFileObject(
                    theTable[0].fileID,
                    theTable[0].uploadedOn,
                    theTable[0].fileSize,
                    theTable[0].fileName,
                    theTable[0].extension,
                    theTable[0].title,
                    theTable[0].fileStoragePath
                    );
            }
            else
            {
                log.Error("SQL Query resulted in zero rows for the file");
                return(null);
            }

            return(theResult);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Delete a file
        /// </summary>
        /// <param name="documentFileID">The file to delete</param>
        public static void DeleteDocumentFile(int documentFileID)
        {
            if (documentFileID <= 0)
            {
                throw new ArgumentException("Invalid file ID.  Should be > 0.  Got " + documentFileID);
            }

            DocumentFileDSTableAdapters.FileInfoTableAdapter ta = new DocumentFileDSTableAdapters.FileInfoTableAdapter();
            ta.DeleteFile(documentFileID);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Update the document text for a document file in the database
        /// </summary>
        /// <param name="documentFileID">The document file ID</param>
        /// <param name="newText">The new text</param>
        public static void UpdateDocumentFileText(int documentFileID, string title, string newText)
        {
            if (documentFileID <= 0)
            {
                throw new ArgumentException("Invalid document file ID. Must be > 0");
            }

            if (newText == null)
            {
                throw new ArgumentException("Invalid null text for document file " + documentFileID.ToString());
            }

            DocumentFileDSTableAdapters.FileInfoTableAdapter ta =
                new DocumentFileDSTableAdapters.FileInfoTableAdapter();

            ta.UpdateFile(documentFileID, title, newText);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Update the storage path oh the file in the database
        /// </summary>
        /// <param name="documentFileID">The document file ID</param>
        /// <param name="newText">The new storage path</param>
        public static bool UpdateDocumentFileStoragePath(int documentFileID, string storagePath)
        {
            if (documentFileID <= 0)
            {
                throw new ArgumentException("Invalid document file ID. Must be > 0");
            }

            if (storagePath == null)
            {
                throw new ArgumentException("Invalid null storage path for document file " + documentFileID.ToString());
            }

            DocumentFileDSTableAdapters.FileInfoTableAdapter ta =
                new DocumentFileDSTableAdapters.FileInfoTableAdapter();

            ta.UpdatePathStorageFile(documentFileID, storagePath);

            return(true);
        }
Exemplo n.º 8
0
        public static bool UpdateDocumentFileTitle(int documentFileID, string title)
        {
            if (documentFileID <= 0)
            {
                throw new ArgumentException("Invalid document file ID. Must be > 0");
            }

            DocumentFileDSTableAdapters.FileInfoTableAdapter ta =
                new DocumentFileDSTableAdapters.FileInfoTableAdapter();

            try
            {
                ta.UpdateFileTitle(documentFileID, title);
            }
            catch (Exception q)
            {
                log.Error("Failed to update file title", q);
                return(false);
            }

            return(true);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Create a new file
        /// </summary>
        /// <param name="theDocumentFile">The file data</param>
        /// <param name="fileStoragePath">Returns the path where the file was actually stored</param>
        /// <param name="isDirty">Specifies if the file should be marked as Dirty in the database</param>
        /// <returns>Returns the resulting ID for the file</returns>
        /// <exception cref="ArgumentException">If the arguments are invalid</exception>
        public static int CreateDocumentFile(DocumentFile theDocumentFile, ref string fileStoragePath, bool isDirty)
        {
            // We create the file in two steps:  Step one is to save the document to the databse and step two is
            // to save the actual binary data to the disk.
            // We do this because we avoid loading the database with gigabytes of data, that we can dumpt to disk.  This
            // is necessary because there are cases where hosting providers will charge much more for database space than
            // disk space and because there are free version of SQL Server that have limitations as to the amount of data
            // that can be stored.  Hence, we avoid dumping into the databse content that is not going to be used
            // in actual queries.  here is a downside to this.  Because the database records are not in the same space
            // than the files themselves, it's possible that files may be deleted and create inconsistencies with the database.

            string destinationDirectory = Artexacta.App.Configuration.Configuration.GetDocumentStorageDirectory();

            if (String.IsNullOrEmpty(destinationDirectory))
            {
                log.Error("Undefined Document Storage Directory in WebConfig.");
                throw new Exception("Undefined Document Storage Directory in WebConfig.");
            }

            // Make sure that the storage directory exists and that we can operate there
            DirectoryInfo dirInfo = new DirectoryInfo(destinationDirectory);

            if (!dirInfo.Exists)
            {
                log.Error("Document Storage Directory defined does not exist: " + destinationDirectory);
                throw new Exception("Document Storage Directory defined does not exist.");
            }



            ///*********************************************************************
            //////*********************************************************************
            ///*********************************************************************
            //////*********************************************************************
            ///*********************************************************************
            ///
            //create subfolders system to storage files
            ///*********************************************************************
            //////*********************************************************************
            ///*********************************************************************
            //////*********************************************************************
            ///*********************************************************************

            //Files in the system are stored in only one folder. This should be changed to the following:
            //every file should be stored in a folder named YYYY/MM/DD/HH
            //(i.e. the date + the hour.  ST does not allow slashes, but this is a hierarchical system.
            //There is a folder YYYY and a subfolder MM and a subfolder DD and a subfolder HH).
            //The strategy is as follows: when a new file has to be stored, it will check if the corresponding folder exists,
            //if it doesnt it creates the folder and stores the file there. This is to avoid having millions of files in ONE folder (which is painful in windows system files).



            // Make sure that the year storage directory exists and that we can operate there
            string year = DateTime.Now.Year.ToString();

            DirectoryInfo dirInfoYear = new DirectoryInfo(destinationDirectory + "//" + year);

            if (!dirInfoYear.Exists)
            {
                log.Debug("Document Storage Directory for year " + year + " does not exist. It will be created.");

                try
                {
                    dirInfoYear.Create();
                }
                catch (Exception q)
                {
                    log.Error("Couldn't create the directory for the year " + year + ". ", q);
                    throw new Exception("Couldn't create the directory for the year " + year + ". Error: " + q.Message);
                }
            }

            // Make sure that the month storage directory exists and that we can operate there
            string month = DateTime.Now.Month.ToString();

            DirectoryInfo dirInfoMonth = new DirectoryInfo(destinationDirectory + "//" + year + "//" + month);

            if (!dirInfoMonth.Exists)
            {
                log.Debug("Document Storage Directory for year " + year + " and month " + month + " does not exist. It will be created.");

                try
                {
                    dirInfoMonth.Create();
                }
                catch (Exception q)
                {
                    log.Error("Couldn't create the directory for the year " + year + " and month " + month + ". ", q);
                    throw new Exception("Couldn't create the directory for the year " + year + " and month " + month + ". Error: " + q.Message);
                }
            }

            // Make sure that the day storage directory exists and that we can operate there
            string day = DateTime.Now.Day.ToString();

            DirectoryInfo dirInfoDay = new DirectoryInfo(destinationDirectory + "//" + year + "//" + month + "//" + day);

            if (!dirInfoDay.Exists)
            {
                log.Debug("Document Storage Directory for year " + year + ", month " + month + " and day " + day + " does not exist. It will be created.");

                try
                {
                    dirInfoDay.Create();
                }
                catch (Exception q)
                {
                    log.Error("Couldn't create the directory for the year " + year + ", month " + month + " and day " + day + ". ", q);
                    throw new Exception("Couldn't create the directory for the year " + year + ", month " + month + " and day " + day + ". Error: " + q.Message);
                }
            }

            // Make sure that the hour storage directory exists and that we can operate there
            string hour = DateTime.Now.Hour.ToString();

            DirectoryInfo dirInfoHour = new DirectoryInfo(destinationDirectory + "//" + year + "//" + month + "//" + day + "//" + hour);

            if (!dirInfoHour.Exists)
            {
                log.Debug("Document Storage Directory for year " + year + ", month " + month + ", day " + day + " and hour " + hour + " does not exist. It will be created.");

                try
                {
                    dirInfoHour.Create();
                }
                catch (Exception q)
                {
                    log.Error("Couldn't create the directory for the year " + year + ", month " + month + ", day " + day + " and hour " + hour + ". ", q);
                    throw new Exception("Couldn't create the directory for the year " + year + ", month " + month + ", day " + day + " and hour " + hour + ". Error: " + q.Message);
                }
            }

            //Final directory
            dirInfo = dirInfoHour;

            // Try to get a random file name and save the file to disk. We will try three times.
            string fileName = "";
            bool   success  = false;

            for (int i = 0; i < 3; i++)
            {
                BinaryWriter writer = null;
                try
                {
                    fileName = dirInfo.FullName + "\\" + Path.GetRandomFileName();

                    FileInfo newFileInfo = new FileInfo(fileName);
                    if (newFileInfo.Exists)
                    {
                        continue;
                    }

                    writer = new BinaryWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write));

                    writer.Write(theDocumentFile.Bytes);
                    writer.Close();
                    success = true;
                    break;
                }
                catch (Exception q)
                {
                    log.Error("Failed to save file to disk", q);
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
            }

            if (!success)
            {
                throw new Exception("Error creating actual physical file. Tried three times and failed");
            }

            int?fileID = 0;

            try
            {
                //int? documentID = null;
                //int? documentVersion = null;

                //if (theDocumentFile.DocumentID > 0 && theDocumentFile.Version > 0)
                //{
                //    documentID = theDocumentFile.DocumentID;
                //    documentVersion = theDocumentFile.Version;
                //}

                DocumentFileDSTableAdapters.FileInfoTableAdapter ta = new DocumentFileDSTableAdapters.FileInfoTableAdapter();
                if (isDirty)
                {
                    ta.InsertDirtyFile(
                        theDocumentFile.Title,
                        theDocumentFile.DateUploaded,
                        theDocumentFile.Size,
                        theDocumentFile.Name,
                        theDocumentFile.Extension,
                        fileName,
                        string.IsNullOrEmpty(theDocumentFile.Text) ? "" : theDocumentFile.Text,
                        ref fileID
                        );
                }
                else
                {
                    ta.InsertNonDirtyFile(
                        theDocumentFile.Title,
                        theDocumentFile.DateUploaded,
                        theDocumentFile.Size,
                        theDocumentFile.Name,
                        theDocumentFile.Extension,
                        fileName,
                        string.IsNullOrEmpty(theDocumentFile.Text) ? "" : theDocumentFile.Text,
                        ref fileID
                        );
                }
            }
            catch (Exception q)
            {
                log.Error("Failed to save a file to the database", q);

                // We failed to save to the the database. We should get rid of the file.
                try
                {
                    FileInfo newFileInfo = new FileInfo(fileName);
                    newFileInfo.Delete();
                }
                catch (Exception qr)
                {
                    // Tried to delete it but I can't.  Nothing we cn do.
                    log.Error("Failed to delete a file past a faield database insert.  Delete file by hand? : " + fileName, qr);
                }
            }

            fileStoragePath = fileName;
            return(fileID.Value);
        }