Exemplo n.º 1
0
        public async Task <File> CreateEmptyFile(
            [FromRoute] Guid repositoryId,
            [FromBody] AccessModifierTypes accessModifierType = AccessModifierTypes.Private)
        {
            var repository = Database.Repository.Include(x => x.Origin).SingleOrDefault(x => x.Id == repositoryId);

            if (repository == null)
            {
                throw new NotFoundException("找不到指定的儲存庫");
            }

            Response.Headers.Add("Access-Control-Allow-Origin",
                                 string.Join(",", repository.Origin.Select(x => x.OriginURI)));

            if (repository.AccessModifier == AccessModifierTypes.Private &&
                !User.IsSuperUser() &&
                repository.UserId != User.Id)
            {
                throw new AuthorizeException();
            }

            return(await Models.EF.File.Create(
                       Database,
                       this.User.Id,
                       repositoryId,
                       new System.IO.MemoryStream(),
                       null,
                       null,
                       accessModifierType));
        }
Exemplo n.º 2
0
        public ClassComposer SetAccessModifier(AccessModifierTypes newType)
        {
            dynamic         twm       = CurrentNode;
            SyntaxTokenList modifiers = twm.Modifiers;

            for (int i = modifiers.Count - 1; i >= 0; i--)
            {
                var m = modifiers.ElementAt(i);
                switch (m.Kind())
                {
                case SyntaxKind.PrivateKeyword:
                case SyntaxKind.ProtectedKeyword:
                case SyntaxKind.InternalKeyword:
                case SyntaxKind.PublicKeyword:
                    modifiers = modifiers.RemoveAt(i);
                    break;
                }
            }

            switch (newType)
            {
            case AccessModifierTypes.PRIVATE:
                modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
                break;

            case AccessModifierTypes.PROTECTED:
                modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword));
                break;

            case AccessModifierTypes.PRIVATE_PROTECTED:
                modifiers = SyntaxFactory.TokenList(modifiers.Concat(new[] { SyntaxFactory.Token(SyntaxKind.PrivateKeyword), SyntaxFactory.Token(SyntaxKind.ProtectedKeyword) }));
                break;

            case AccessModifierTypes.INTERNAL:
                modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.InternalKeyword));
                break;

            case AccessModifierTypes.PROTECTED_INTERNAL:
                modifiers = SyntaxFactory.TokenList(modifiers.Concat(new[] { SyntaxFactory.Token(SyntaxKind.ProtectedKeyword), SyntaxFactory.Token(SyntaxKind.InternalKeyword) }));
                break;

            case AccessModifierTypes.PUBLIC:
                modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
                break;
            }


            SyntaxNode newNode = twm.WithModifiers(modifiers);

            Replace(twm, newNode, null);

            return(this);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 建立新檔案
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="userId">使用者帳號</param>
        /// <param name="repositoryId">儲存庫唯一識別號</param>
        /// <param name="stream">檔案串流</param>
        /// <param name="contentType">檔案ContentType</param>
        /// <param name="name">名稱</param>
        /// <param name="accessModifier">存取限制</param>
        /// <returns>檔案實例</returns>
        public static async Task <File> Create(
            HHStorageContext context,
            string userId,
            Guid repositoryId,
            Stream stream,
            string contentType,
            string name = null,
            AccessModifierTypes accessModifier = AccessModifierTypes.Private
            )
        {
            if (userId == null)
            {
                throw new NotNullException("使用者帳號不該為null");
            }

            if (!context.Repository.Any(x => x.Id == repositoryId && x.UserId == userId))
            {
                throw new NotFoundException("找不到該使用者指定儲存庫");
            }

            var result = new File()
            {
                UserId       = userId,
                RepositoryId = repositoryId,
                ContentType  = contentType
            };

            result.Name           = name;
            result.AccessModifier = accessModifier;
            result.Size           = stream.Length;

            try {
                using (FileStream fileStream = System.IO.File.Create(GetFilePathById(result.Id))) {
                    await stream.CopyToAsync(fileStream);

                    await fileStream.FlushAsync();
                }
            } catch {
                throw new OperationInterruptedException("檔案上傳過程遭到中斷");
            }

            context.File.Add(result);
            await context.SaveChangesAsync();

            return(result);
        }