-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #100
- Loading branch information
Showing
12 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DROP TRIGGER media_change_for_user; | ||
DELETE FUNCTION track_update_user_media(); | ||
DROP TABLE user_stats; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
CREATE TABLE IF NOT EXISTS user_stats ( | ||
user_id TEXT PRIMARY KEY NOT NULL, | ||
uploaded_bytes BIGINT NOT NULL | ||
); | ||
CREATE OR REPLACE FUNCTION track_update_user_media() | ||
RETURNS TRIGGER | ||
LANGUAGE PLPGSQL | ||
AS | ||
$$ | ||
BEGIN | ||
IF TG_OP = 'UPDATE' THEN | ||
INSERT INTO user_stats (user_id, uploaded_bytes) VALUES (NEW.user_id, 0) ON CONFLICT (user_id) DO NOTHING; | ||
INSERT INTO user_stats (user_id, uploaded_bytes) VALUES (OLD.user_id, 0) ON CONFLICT (user_id) DO NOTHING; | ||
|
||
IF NEW.user_id <> OLD.user_id THEN | ||
UPDATE user_stats SET uploaded_bytes = user_stats.uploaded_bytes - OLD.size_bytes WHERE user_stats.user_id = OLD.user_id; | ||
UPDATE user_stats SET uploaded_bytes = user_stats.uploaded_bytes + NEW.size_bytes WHERE user_stats.user_id = NEW.user_id; | ||
ELSIF NEW.size_bytes <> OLD.size_bytes THEN | ||
UPDATE user_stats SET uploaded_bytes = user_stats.uploaded_bytes - OLD.size_bytes + NEW.size_bytes WHERE user_stats.user_id = NEW.user_id; | ||
END IF; | ||
RETURN NEW; | ||
ELSIF TG_OP = 'DELETE' THEN | ||
UPDATE user_stats SET uploaded_bytes = user_stats.uploaded_bytes - OLD.size_bytes WHERE user_stats.user_id = OLD.user_id; | ||
RETURN OLD; | ||
ELSIF TG_OP = 'INSERT' THEN | ||
INSERT INTO user_stats (user_id, uploaded_bytes) VALUES (NEW.user_id, NEW.size_bytes) ON CONFLICT (user_id) DO UPDATE SET uploaded_bytes = user_stats.uploaded_bytes + NEW.size_bytes; | ||
RETURN NEW; | ||
END IF; | ||
END; | ||
$$; | ||
DROP TRIGGER IF EXISTS media_change_for_user ON media; | ||
CREATE TRIGGER media_change_for_user AFTER INSERT OR UPDATE OR DELETE ON media FOR EACH ROW EXECUTE PROCEDURE track_update_user_media(); | ||
|
||
-- Populate the new table | ||
DO $$ | ||
BEGIN | ||
IF ((SELECT COUNT(*) FROM user_stats)) = 0 THEN | ||
INSERT INTO user_stats SELECT user_id, SUM(size_bytes) FROM media GROUP BY user_id; | ||
END IF; | ||
END $$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package quota | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/ryanuber/go-glob" | ||
"github.com/turt2live/matrix-media-repo/common/rcontext" | ||
"github.com/turt2live/matrix-media-repo/storage" | ||
) | ||
|
||
func IsUserWithinQuota(ctx rcontext.RequestContext, userId string) (bool, error) { | ||
if !ctx.Config.Uploads.Quota.Enabled { | ||
return true, nil | ||
} | ||
|
||
db := storage.GetDatabase().GetMetadataStore(ctx) | ||
stat, err := db.GetUserStats(userId) | ||
if err == sql.ErrNoRows { | ||
return true, nil // no stats == within quota | ||
} | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
for _, q := range ctx.Config.Uploads.Quota.UserQuotas { | ||
if glob.Glob(q.Glob, userId) { | ||
if q.MaxBytes == 0 { | ||
return true, nil // infinite quota | ||
} | ||
return stat.UploadedBytes < q.MaxBytes, nil | ||
} | ||
} | ||
|
||
return true, nil // no rules == no quota | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package types | ||
|
||
type UserStats struct { | ||
UserId string | ||
UploadedBytes int64 | ||
} |