Beispiel #1
0
        /// <summary>
        /// Upload documents to db for project or folder.
        /// </summary>
        /// <param name="parentPath"></param>
        /// <param name="parentId"></param>
        /// <param name="container"></param>
        public void UploadDocuments(string parentPath, int parentId, Container container = Container.Folder)
        {
            string[] files = Directory.GetFiles(parentPath);
            foreach (string fileName in files)
            {
                Document dbDocument = null;
                using (var dbContext = new sliceofpieEntities2()) {
                    string       pathName     = Path.GetFileName(fileName);
                    string[]     parts        = pathName.Split('-');
                    int          id           = int.Parse(parts[0]);
                    string       title        = pathName.Replace(parts[0] + "-", "").Replace(".txt", "");
                    int          hash         = "".GetHashCode();
                    string       revision     = "";
                    bool         isRevision   = false;
                    FileStream   fileStream   = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    StreamReader streamReader = new StreamReader(fileStream);
                    string       line;
                    int          i = 0;
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        if (i == 0)
                        {
                            if (line.Length > 0)
                            {
                                if (line.Substring(0, 3).Equals("rev"))
                                {
                                    isRevision = true;
                                    line       = line.Substring(3);
                                }
                                hash = int.Parse(line);
                            }
                        }
                        else
                        {
                            revision += line + "\n";
                        }
                        i++;
                    }
                    revision = revision.Trim();
                    streamReader.Close();

                    var dbDocuments = from dDocument in dbContext.Documents
                                      where dDocument.Id == id
                                      select dDocument;
                    if (id > 0 && dbDocuments.Count() == 0)
                    {
                        if (Directory.Exists(Path.Combine(parentPath, Helper.GenerateName(id, title)) + ".txt"))
                        {
                            Directory.Move(Path.Combine(parentPath, Helper.GenerateName(id, title)) + ".txt", Path.Combine(parentPath, Helper.GenerateName(0, title)) + ".txt");
                        }
                        id = 0;
                    }
                    if (id > 0)
                    {
                        // Updating document
                        dbDocument       = dbDocuments.First();
                        dbDocument.Title = title;
                        if (container == Container.Project)
                        {
                            dbDocument.ProjectId = parentId;
                            dbDocument.FolderId  = null;
                        }
                        else
                        {
                            dbDocument.ProjectId = null;
                            dbDocument.FolderId  = parentId;
                        }
                        dbDocument.IsMerged = isRevision;
                        if (dbDocument.CurrentHash == hash)
                        {
                            dbDocument.CurrentRevision = revision;
                            dbDocument.CurrentHash     = revision.GetHashCode();
                            UpdateHash(fileName, revision.GetHashCode());
                        }
                        else if (revision.GetHashCode() == hash)
                        {
                            UpdateHash(fileName, (int)dbDocument.CurrentHash);
                        }
                        else
                        {
                            // Handle merge (and conflicts)
                            string       merge        = Merger.Merge(revision, dbDocument.CurrentRevision);
                            FileStream   fs           = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                            StreamWriter streamWriter = new StreamWriter(fs);
                            string[]     lines        = merge.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                            streamWriter.WriteLine("rev" + dbDocument.CurrentHash);
                            foreach (string l in lines)
                            {
                                streamWriter.WriteLine(l);
                            }
                            streamWriter.Flush();
                            streamWriter.Close();
                        }
                    }
                    else
                    {
                        // Creating document
                        dbDocument = new Document {
                            Title           = title,
                            CurrentRevision = revision,
                            CurrentHash     = revision.GetHashCode()
                        };
                        if (container == Container.Project)
                        {
                            dbDocument.ProjectId = parentId;
                            dbDocument.FolderId  = null;
                        }
                        else
                        {
                            dbDocument.ProjectId = null;
                            dbDocument.FolderId  = parentId;
                        }
                        UpdateHash(fileName, revision.GetHashCode());
                        dbContext.Documents.AddObject(dbDocument);
                    }
                    dbContext.SaveChanges();
                }
                // Rename document file
                if (File.Exists(Path.Combine(parentPath, Helper.GenerateName(0, dbDocument.Title + ".txt"))))
                {
                    File.Move(Path.Combine(parentPath, Helper.GenerateName(0, dbDocument.Title + ".txt")), Path.Combine(parentPath, Helper.GenerateName(dbDocument.Id, dbDocument.Title + ".txt")));
                }
                // Create revision
                using (var dbContext = new sliceofpieEntities2()) {
                    var dbRevisions = from dRevision in dbContext.Revisions
                                      where dRevision.DocumentId == dbDocument.Id && dRevision.ContentHash == dbDocument.CurrentHash
                                      select dRevision;
                    if (dbRevisions.Count() == 0)
                    {
                        dbContext.Revisions.AddObject(new Revision {
                            DocumentId  = dbDocument.Id,
                            Content     = dbDocument.CurrentRevision,
                            ContentHash = dbDocument.CurrentHash,
                            Timestamp   = DateTime.Now
                        });
                        dbContext.SaveChanges();
                    }
                }
            }
        }