Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

#!/usr/bin/env python 

# -*- coding: utf-8 -*- 

 

############################################################################### 

#  Copyright 2013 Kitware Inc. 

# 

#  Licensed under the Apache License, Version 2.0 ( the "License" ); 

#  you may not use this file except in compliance with the License. 

#  You may obtain a copy of the License at 

# 

#    http://www.apache.org/licenses/LICENSE-2.0 

# 

#  Unless required by applicable law or agreed to in writing, software 

#  distributed under the License is distributed on an "AS IS" BASIS, 

#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

#  See the License for the specific language governing permissions and 

#  limitations under the License. 

############################################################################### 

 

import cherrypy 

import datetime 

import os 

 

from .model_base import Model, ValidationException 

from girder.utility import assetstore_utilities 

from girder.constants import AssetstoreType 

 

 

class Assetstore(Model): 

    """ 

    This model represents an assetstore, an abstract repository of Files. 

    """ 

    def initialize(self): 

        self.name = 'assetstore' 

 

    def validate(self, doc): 

        # Ensure no duplicate names 

        q = {'name': doc['name']} 

        if '_id' in doc: 

            q['_id'] = {'$ne': doc['_id']} 

        duplicates = self.find(q, limit=1, fields=['_id']) 

        if duplicates.count() != 0: 

            raise ValidationException('An assetstore with that name already ' 

                                      'exists.', 'name') 

 

        # Name must not be empty 

        if not doc['name']: 

            raise ValidationException('Name must not be empty.', 'name') 

 

        # Filesystem assetstores must have their directory exist and writeable 

        if doc['type'] == AssetstoreType.FILESYSTEM: 

            if not os.path.isabs(doc['root']): 

                raise ValidationException('You must provide an absolute path ' 

                                          'for the root directory.', 'root') 

            if not os.path.isdir(doc['root']): 

                try: 

                    os.makedirs(doc['root']) 

                except OSError: 

                    raise ValidationException('Could not make directory "%s".' 

                                              % doc['root'], 'root') 

            if not os.access(doc['root'], os.W_OK): 

                raise ValidationException('Unable to write into directory "%s".' 

                                          % doc['root'], 'root') 

 

        if doc['type'] == AssetstoreType.GRIDFS: 

            if not doc['db']: 

                raise ValidationException('Database Name must not be empty.', 

                                          'db') 

            if '.' in doc['db'] or ' ' in doc['db']: 

                raise ValidationException('Database Name cannot contain spaces' 

                                          ' or periods.', 'db') 

 

        # If no current assetstore exists yet, set this one as the current. 

        current = self.find({'current': True}, limit=1, fields=['_id']) 

        if current.count() == 0: 

            doc['current'] = True 

        if 'current' not in doc: 

            doc['current'] = False 

 

        # If we are setting this as current, we should unmark all other 

        # assetstores that have the current flag. 

        if doc['current'] is True: 

            self.update({'current': True}, {'$set': {'current': False}}) 

 

        return doc 

 

    def remove(self, assetstore): 

        """ 

        Delete an assetstore. If there are any files within this assetstore, 

        a validation exception is raised. 

 

        :param assetstore: The assetstore document to delete. 

        :type assetstore: dict 

        """ 

        # Delete all folders in the community recursively 

        files = self.model('file').find({ 

            'assetstoreId': assetstore['_id'] 

            }, limit=1) 

        if files.count(True) > 0: 

            raise ValidationException('You may not delete an assetstore that ' 

                                      'contains files.') 

 

        Model.remove(self, assetstore) 

 

    def list(self, limit=50, offset=0, sort=None): 

        """ 

        List all assetstores. 

 

        :param limit: Result limit. 

        :param offset: Result offset. 

        :param sort: The sort structure to pass to pymongo. 

        :returns: List of users. 

        """ 

        cursor = self.find({}, limit=limit, offset=offset, sort=sort) 

        assetstores = [] 

        for assetstore in cursor: 

            adapter = assetstore_utilities.getAssetstoreAdapter(assetstore) 

            assetstore['capacity'] = adapter.capacityInfo() 

            assetstores.append(assetstore) 

 

        return assetstores 

 

    def createFilesystemAssetstore(self, name, root): 

        return self.save({ 

            'type': AssetstoreType.FILESYSTEM, 

            'created': datetime.datetime.now(), 

            'name': name, 

            'root': root 

        }) 

 

    def createGridFsAssetstore(self, name, db): 

        return self.save({ 

            'type': AssetstoreType.GRIDFS, 

            'created': datetime.datetime.now(), 

            'name': name, 

            'db': db 

        }) 

 

    def createS3Assetstore(self, name): 

        raise Exception('S3 assetstore not implemented yet.') 

 

    def getCurrent(self): 

        """ 

        Returns the current assetstore. If none exists, this will raise a 500 

        exception. 

        """ 

        cursor = self.find({'current': True}, limit=1) 

        if cursor.count() == 0: 

            raise Exception('No current assetstore is set.') 

 

        return cursor.next()