示例#1
0
        public ObjectId Upload(string Name, Stream stream)
        {
            var objectId   = ObjectId.GenerateNewId(DateTime.UtcNow);
            var systemName = $"{objectId}_{Name}";
            var fullPath   = storagePath + "/" + systemName;

            if (fileCollection.Find <MongoFile>(new FilterDefinitionBuilder <MongoFile>().Eq(f => f.Path, fullPath)).CountDocuments() == 0)
            {
                var file = new MongoFile()
                {
                    _id        = objectId,
                    PublicId   = Guid.NewGuid(),
                    SystemName = Name,
                    CreatedAt  = DateTime.UtcNow,
                    EditedAt   = DateTime.UtcNow,
                    Path       = fullPath,
                    Length     = stream.Length,
                    Extension  = Path.GetExtension(Name),
                };
                //fileCollection.BulkWrite(model);
                fileCollection.InsertOne(file);
                //  fileCollection.(new FilterDefinitionBuilder<MongoFile>().Eq(f=>f._id,id),file);
            }
            FileStream fs = new FileStream(storagePath + "/" + systemName, FileMode.Create);

            byte[] bytes = new byte[1024];
            int    num   = 0;

            do
            {
                num = stream.Read(bytes, 0, 1024);
                Console.WriteLine(bytes);
                fs.Write(bytes, 0, num);
            } while (num > 0);
            stream.Close();
            fs.Close();
            fs.Dispose();
            fileCollection.UpdateOne(new FilterDefinitionBuilder <MongoFile>().Eq(f => f._id, objectId),
                                     new UpdateDefinitionBuilder <MongoFile>().Set(f => f.Name, Name));


            stream.Close();
            fs.Close();

            return(objectId);
        }
示例#2
0
        private async void Watcher_Created(object sender, FileSystemEventArgs e)
        {
            if (File.GetAttributes(e.FullPath) == FileAttributes.Directory)
            {
            }
            else
            {
                FileInfo info  = new FileInfo(e.FullPath);
                var      array = e.Name.Split('_');
                if (fileCollection.Find <MongoFile>(new FilterDefinitionBuilder <MongoFile>().Eq(f => f._id, new ObjectId(array[0]))).CountDocuments() == 0)
                {
                    var id = new ObjectId(array[0]);

                    var file = new MongoFile()
                    {
                        _id        = id,
                        PublicId   = Guid.NewGuid(),
                        SystemName = e.Name,
                        CreatedAt  = File.GetCreationTimeUtc(e.FullPath),
                        EditedAt   = File.GetLastWriteTimeUtc(e.FullPath),
                        Path       = e.FullPath,
                        Attributes = info.Attributes.ToString(),
                        Length     = info.Length,
                        Extension  = info.Extension
                    };
                    var update = new ObjectUpdateDefinition <MongoFile>(file);

                    var model = new List <WriteModel <MongoFile> >()
                    {
                        new UpdateOneModel <MongoFile>(
                            new FilterDefinitionBuilder <MongoFile>().Eq(f => f._id, id),
                            update
                            )
                        {
                            IsUpsert = true
                        }
                    };
                    //fileCollection.BulkWrite(model);
                    await fileCollection.InsertOneAsync(file);

                    //  fileCollection.(new FilterDefinitionBuilder<MongoFile>().Eq(f=>f._id,id),file);
                }
            }
            Console.WriteLine($"CREATE {e.Name}");
        }