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 ...
随机推荐
- Unity脚本编程之——协程(Coroutine)
本文翻译自Unity官方文档:https://docs.unity3d.com/Manual/Coroutines.html 专有名词: Coroutine 协程 Alpha 不透明度 当你调用一个函 ...
- python JoinableQueue在生产者消费者项目中的简单应用
class multiprocessing.JoinableQueue([maxsize]) JoinableQueue, a Queue subclass, is a queue which add ...
- C#基础随手笔记之基础操作优化
对数据的查询,删除等基本操作是任何编程语言都会涉及到的基础,因此,研究了一下C#中比较常用的数据操作类型,并顺手做个笔记. List查询时,若是处理比较大的数据则使用HashSet<T>类 ...
- JS(作用域和闭包)
1.对变量提升的理解 1.变量定义(上下文) 2.函数声明 2.说明 this 几种不同的使用场景 常见用法 1.作为构造函数执行 2.作为对象属性执行 3.作为普通函数执行(this === win ...
- oracle 11g杀掉锁的sql
oracle 11g杀掉锁的sql [引用 2013-3-6 17:19:12] 字号:大 中 小 --查询出出现锁的session_idselect session_id from v$lo ...
- FFmpeg备忘录
av_dup_packet函数 av_dup_packet会为destruct指针为av_destruct_packet_nofree的AVPacket新建一个缓冲区,将原有的缓冲区数据拷贝至新缓冲区 ...
- mybatis源码解读(二)——构建Configuration对象
Configuration 对象保存了所有mybatis的配置信息,主要包括: ①. mybatis-configuration.xml 基础配置文件 ②. mapper.xml 映射器配置文件 1. ...
- CDN公共资源
SAE: http://lib.sinaapp.com/ Google: https://developers.google.com/speed/libraries/devguide?hl=zh-CN ...
- ssh运行环境搭建及测试
一.运行环境 1.Spring环境 Spring是一站式开发框架,在SSH中主要有以下作用,就像一个大管家: 控制反转(Inversion of Control):类不再自己进行类创建,而是交给Spr ...
- UE4笔记:利用Widget设计一个切换材质功能
UE4引擎中的Widget蓝图是一个重要的工具,可用于场景中的页面叠加,镜头绑定,场景切换等多处地方,在这里笔者介绍一种利用控件蓝图和场景中物体进行信息交互的方法,直观的体现就是进行物体的材质切换. ...