admin-handlers.go
package manager
import (
"net/http"
"regexp"
"strconv"
"io"
"fmt"
"github.com/030io/whalefs/manager/volume"
"gopkg.in/redis.v2"
"os"
)
var (
postVolumeUrl *regexp.Regexp //提交卷文件正则表达式
postFileUrl *regexp.Regexp //提交文件正则表达式
deleteFileUrl *regexp.Regexp //删除文件url 正则表达式
)
//初始化文件表达式
func init() {
var err error
postVolumeUrl, err = regexp.Compile("/([0-9]*)/")
if err != nil {
panic(err)
}
postFileUrl, err = regexp.Compile("/([0-9]*)/([0-9]*)/(.*)")
if err != nil {
panic(err)
}
deleteFileUrl = postFileUrl
}
type Size interface {
Size() int64
}
//文件管理处理器
func (vm *VolumeManager)adminEntry(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodHead:
vm.publicEntry(w, r)
case http.MethodPost:
if postFileUrl.MatchString(r.URL.Path) {
vm.adminPostFile(w, r)
} else if postVolumeUrl.MatchString(r.URL.Path) {
vm.adminPostVolume(w, r)
} else {
http.Error(w, r.URL.String() + " can't match", http.StatusNotFound)
}
case http.MethodDelete:
if deleteFileUrl.MatchString(r.URL.Path) {
vm.adminDeleteFile(w, r)
} else {
http.Error(w, r.URL.String() + " can't match", http.StatusNotFound)
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
//卷文件管理处理器
func (vm *VolumeManager)adminPostVolume(w http.ResponseWriter, r *http.Request) {
match := postVolumeUrl.FindStringSubmatch(r.URL.Path)
vid, _ := strconv.ParseUint(match[1], 10, 64)
volume, err := volume.NewVolume(vm.DataDir, vid)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
vm.Volumes[vid] = volume
w.WriteHeader(http.StatusCreated)
}
//上传文件处理器
func (vm *VolumeManager)adminPostFile(w http.ResponseWriter, r *http.Request) {
match := postFileUrl.FindStringSubmatch(r.URL.Path)
vid, _ := strconv.ParseUint(match[1], 10, 64)
volume, ok := vm.Volumes[vid]
if !ok {
http.Error(w, "can't find volume", http.StatusNotFound)
return
}
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
var fileSize int64
switch file.(type) {
case *os.File:
s, _ := file.(*os.File).Stat()
fileSize = s.Size()
case Size:
fileSize = file.(Size).Size()
}
fid, _ := strconv.ParseUint(match[2], 10, 64)
file_, err := volume.NewFile(fid, header.Filename, uint64(fileSize))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer func() {
if err != nil {
if err := volume.Delete(file_.Info.Fid, file_.Info.FileName); err != nil {
panic(err)
}
}
}()
n, err := io.Copy(file_, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if n != fileSize {
err = fmt.Errorf("%d != %d", n, fileSize)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
//文件删除
func (vm *VolumeManager)adminDeleteFile(w http.ResponseWriter, r *http.Request) {
match := deleteFileUrl.FindStringSubmatch(r.URL.Path)
vid, _ := strconv.ParseUint(match[1], 10, 64)
volume := vm.Volumes[vid]
if volume == nil {
http.Error(w, "can't find volume", http.StatusNotFound)
return
}
fid, _ := strconv.ParseUint(match[2], 10, 64)
err := volume.Delete(fid, match[3])
if err == redis.Nil {
http.NotFound(w, r)
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusAccepted)
}
}
admin-handlers.go的更多相关文章
- Solr Cloud - SolrCloud
关于 Solr Cloud Zookeeper 入门,介绍 原理 原封不动转自 http://wiki.apache.org/solr/SolrCloud/ ,文章的内存有些过时,但是了解原理. Th ...
- Ansible6:Playbook简单使用【转】
ansbile-playbook是一系列ansible命令的集合,利用yaml 语言编写.playbook命令根据自上而下的顺序依次执行.同时,playbook开创了很多特性,它可以允许你传输某个命令 ...
- Importing/Indexing database (MySQL or SQL Server) in Solr using Data Import Handler--转载
原文地址:https://gist.github.com/maxivak/3e3ee1fca32f3949f052 Install Solr download and install Solr fro ...
- $2015 武汉森果公司web后端开发实习日记----书写是为了更好的思考
找暑期实习,3月份分别投了百度和腾讯的实习简历,都止步于笔试,总结的主要原因有两点:基础知识不扎实,缺乏项目经验.后来到拉勾网等网站上寻找实习,看了很多家,都还是处于观望状态.后来参加了武汉实习吧在大 ...
- ansible小结(八)ansible-playbook简单使用
ansbile-playbook是一系统ansible命令的集合,其利用yaml 语言编写,运行过程,ansbile-playbook命令根据自上而下的顺序依次执行.同时,playbook开创了很多特 ...
- 【Django】--Models 和ORM以及admin配置
Models 数据库的配置 1 django默认支持sqlite,mysql, oracle,postgresql数据库 <1>sqlite django默认使用sqlite的数据库 ...
- elmah - Error Logging Modules and Handlers for ASP.NET - 1 : 初体验
elmah(英文):https://code.google.com/p/elmah/ 写作思路:先看结果,然后再说原理 elmah文章基本内容如下 1.安装 2.基本使用 3.详细配置讲解 ...
- DoesNotExist at /admin/
DoesNotExist at /admin/ User has no account. Request Method: GET Request URL: http://127.0.0.1:8000/ ...
- django 自己编写admin
继上次CRM项目之后 我们发现了django自带admin的强大之处以及灵活性,但是admin在企业中也一样很难做到完全的对接,因此编写自己的后台管理就显得至关重要. 本次自定义admin项目将接着上 ...
- Django admin模块使用search时报错:django.core.exceptions.FieldError: Related Field got invalid lookup: contains
日志如下: <class 'django.core.handlers.wsgi.WSGIRequest'> ------------registered_admins: {'spaceCl ...
随机推荐
- ASP.NET MVC不可或缺的部分——DI(IOC)容器及控制器重构的剖析(DI的实现原理)
IoC框架最本质的东西:反射或者EMIT来实例化对象.然后我们可以加上缓存,或者一些策略来控制对象的生命周期,比如是否是单例对象还是每次都生成一个新的对象. DI实现其实很简单,首先设计类来实现接口, ...
- 遍历输出图片加hover
1. $(".icon a>div").hover(function () { var slls = $(this).attr("class"); sll ...
- Java单例模式(Singleton)以及实现
一. 什么是单例模式 因程序需要,有时我们只需要某个类同时保留一个对象,不希望有更多对象,此时,我们则应考虑单例模式的设计. 二. 单例模式的特点 1. 单例模式只能有一个实例. 2. 单例类必须创建 ...
- C# 发展史
C# 发展史 Intro 本文主要总结介绍C# 每个版本带来的不同的语言特性. C#,读作C Sharp,是微软推出的一种基于.NET平台的.面向对象的高级编程语言.是微软公司在2000年发布的一种新 ...
- Flask构建微电影(二)
第三章.项目分析.搭建目录及模型设计 3.1.前后台项目目录分析 微电影网站 前台模块 后台模块 前台(home) 数据模型:models.py 表单处理:home/forms.py 模板目录:tem ...
- simple_list_item_1 和simple_list_item_2有什么区别???
在安卓系统自带的List View里, 有simple_list_item_1.simple_list_item_2.two_line_list_item等.以下对这些布局进行简要介绍: 1.simp ...
- Git的一些操作
前言 记录一些经常需要用到的命令. 私钥.公钥的生成(默认在C盘用户文件下生成) ssh-keygen -t rsa //rsa加密 拉取远程分支并与本地分支合并 git pull [url] 上述效 ...
- HTTP和SOAP完全就是两个不同的协议
HTTP只负责把数据传送过去,不会管这个数据是XML.HTML.图片.文本文件或者别的什么.而SOAP协议则定义了怎么把一个对象变成XML文本,在远程如何调用等,怎么能够混为一谈. ...
- ExecutorCompletionService分析及使用
当我们通过Executor提交一组并发执行的任务,并且希望在每一个任务完成后能立即得到结果,有两种方式可以采取: 方式一: 通过一个list来保存一组future,然后在循环中轮训这组future,直 ...
- MyBatis xml配置文件详解
http://blog.csdn.net/fenghuibian/article/details/52525671