예제 #1
0
        public MediaDb Add(MediaDb entity)
        {
            entity.EntityId = ObjectId.GenerateNewId().ToString();

            Collection.InsertOne(entity);
            return entity;
        }
예제 #2
0
        public MediaViewModel Upload(HttpPostedFile file)
        {
            if (file == null || file.ContentLength == 0)
                return null;

            var path = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + MediaPrefix;
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            var prefix = string.Empty;
            var count = 0;
            while (File.Exists(path + prefix + file.FileName))
            {
                count++;
                prefix = count + "_";
            }

            var filePath = path + prefix + file.FileName;
            try
            {
                file.SaveAs(filePath);
            }
            catch (Exception)
            {
                return null;
            }

            var relativePath = MediaPrefix + prefix + file.FileName;

            var entity = new MediaDb
            {
                Name = file.FileName,
                ContentType = file.ContentType,
                RelativeUrl = relativePath
            };
            var media = _mediaRepository.Add(entity);

            return new MediaViewModel
            {
                Id = media.EntityId,
                Url = relativePath
            };
        }