Exemple #1
0
        /// <summary>
        /// create file
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="buffer"></param>
        /// <param name="ownerId"></param>
        /// <param name="roles"></param>
        /// <returns></returns>
        public IStorageFile Create(string filepath, byte[] buffer, DateTime?modificationDate, Guid?ownerId = null, List <Guid> roles = null)
        {
            if (string.IsNullOrWhiteSpace(filepath))
            {
                throw new ArgumentException("filepath cannot be null or empty");
            }

            //all filepaths are lowercase and all starts with folder separator
            filepath = filepath.ToLowerInvariant();
            if (!filepath.StartsWith(FOLDER_SEPARATOR))
            {
                filepath = FOLDER_SEPARATOR + filepath;
            }

            if (Find(filepath) != null)
            {
                throw new ArgumentException(filepath + ": file already exists");
            }

            BsonDocument metadata = new BsonDocument();

            metadata.Add(new BsonElement("owner_id", ownerId));
            BsonArray rolesArr = new BsonArray();

            if (roles != null && roles.Count > 0)
            {
                foreach (var roleId in roles)
                {
                    rolesArr.Add(BsonValue.Create(roleId));
                }
            }
            metadata.Add(new BsonElement("available_to_roles", rolesArr));

            if (modificationDate == null)
            {
                modificationDate = DateTime.UtcNow;
            }

            using (var stream = gfs.OpenWrite(filepath, new MongoGridFSCreateOptions {
                UploadDate = modificationDate.Value, Metadata = metadata
            }))
            {
                if (buffer != null && buffer.Length > 0)
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }
            return(Find(filepath));
        }
Exemple #2
0
        public Stream OpenStream(string remoteFile, string mimetype)
        {
            _logger.DebugFormat("Open MongoDB By Stream, Id {0}.", remoteFile);

            Stream stream = null;

            try
            {
                MongoGridFSCreateOptions option = new MongoGridFSCreateOptions
                {
                    Id          = remoteFile,
                    UploadDate  = DateTime.Now,
                    ContentType = mimetype,
                };

                MongoGridFS fs = new MongoGridFS(_context.DataBase);
                stream = fs.OpenWrite(remoteFile, option);
                return(stream);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                _logger.Error(ex.StackTrace);
                if (stream != null)
                {
                    stream.Close();
                }
                throw;
            }
        }
Exemple #3
0
        public Stream OpenFile(string virtualPath, bool readOnly = false)
        {
            string fixedPath = FixPath(virtualPath);

            if (!FileExists(fixedPath) && !readOnly)
            {
                InternalWriteFile(fixedPath, new MemoryStream(new byte[0]));
            }

            MongoGridFS gridFs = GetGridFS();

            if (readOnly)
            {
                return(gridFs.OpenRead(fixedPath));
            }

            MongoGridFSFileInfo fileInfo = gridFs.FindOne(fixedPath);

            gridFs.SetMetadata(fileInfo, CreateMetadata(fixedPath, false));

            return(gridFs.OpenWrite(fixedPath));
        }